]> git.lyx.org Git - lyx.git/blob - src/support/docstring.h
Add operator += for ASCII C strings and single ASCII chars
[lyx.git] / src / support / docstring.h
1 // -*- C++ -*-
2 /**
3  * \file docstring.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Georg Baum
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LYX_DOCSTRING_H
14 #define LYX_DOCSTRING_H
15
16 #include "support/types.h"
17
18 #include <string>
19
20 namespace lyx {
21
22 /// String type for storing the main text in UCS4 encoding
23 typedef std::basic_string<char_type> docstring;
24
25 /// Creates a docstring from a C string of ASCII characters
26 docstring const from_ascii(char const *);
27
28 /// Creates a docstring from a std::string of ASCII characters
29 docstring const from_ascii(std::string const &);
30
31 /// Creates a std::string of ASCII characters from a docstring
32 std::string const to_ascii(docstring const &);
33
34 /// Creates a docstring from a UTF8 string. This should go eventually.
35 docstring const from_utf8(std::string const &);
36
37 /// Creates a UTF8 string from a docstring. This should go eventually.
38 std::string const to_utf8(docstring const &);
39
40 }
41
42 /// Compare a docstring with a C string of ASCII characters
43 bool operator==(lyx::docstring const &, char const *);
44
45 /// Compare a C string of ASCII characters with a docstring
46 inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; }
47
48 /// Compare a docstring with a C string of ASCII characters
49 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
50
51 /// Compare a C string of ASCII characters with a docstring
52 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
53
54 /// Concatenate a docstring and a C string of ASCII characters
55 lyx::docstring operator+(lyx::docstring const &, char const *);
56
57 /// Concatenate a C string of ASCII characters and a docstring
58 lyx::docstring operator+(char const *, lyx::docstring const &);
59
60 /// Concatenate a docstring and a single ASCII character
61 lyx::docstring operator+(lyx::docstring const & l, char r);
62
63 /// Concatenate a single ASCII character and a docstring
64 lyx::docstring operator+(char l, lyx::docstring const & r);
65
66 /// Append a C string of ASCII characters to a docstring
67 lyx::docstring operator+=(lyx::docstring &, char const *);
68
69 /// Append a single ASCII character to a docstring
70 lyx::docstring operator+=(lyx::docstring & l, char r);
71
72
73 #if SIZEOF_WCHAR_T != 4 && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
74 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
75 namespace std {
76
77 template<typename T> void
78 char_traits<T>::assign(char_type & c1, char_type const & c2)
79 {
80         c1 = c2;
81 }
82
83
84 template<typename T> bool
85 char_traits<T>::eq(char_type const & c1, char_type const & c2)
86 {
87         return c1 == c2;
88 }
89
90
91 template<typename T> bool
92 char_traits<T>::lt(char_type const & c1, char_type const & c2)
93 {
94         return c1 < c2;
95 }
96
97
98 template<typename T> int
99 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
100 {
101         for (std::size_t i = 0; i < n; ++i)
102                 if (lt(s1[i], s2[i]))
103                         return -1;
104                 else if (lt(s2[i], s1[i]))
105                         return 1;
106         return 0;
107 }
108
109
110 template<typename T> std::size_t
111 char_traits<T>::length(char_type const * p)
112 {
113         std::size_t i = 0;
114         while (!eq(p[i], char_type()))
115                 ++i;
116         return i;
117 }
118
119
120 template<typename T> typename char_traits<T>::char_type const *
121 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
122 {
123         for (std::size_t i = 0; i < n; ++i)
124                 if (eq(s[i], a))
125                         return s + i;
126         return 0;
127 }
128
129
130 template<typename T> typename char_traits<T>::char_type *
131 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
132 {
133         return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
134 }
135
136
137 template<typename T> typename char_traits<T>::char_type *
138 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
139 {
140         std::copy(s2, s2 + n, s1);
141         return s1;
142 }
143
144
145 template<typename T> typename char_traits<T>::char_type *
146 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
147 {
148         std::fill_n(s, n, a);
149         return s;
150 }
151
152
153 template<typename T> typename char_traits<T>::char_type
154 char_traits<T>::to_char_type(int_type const & c)
155 {
156         return static_cast<char_type>(c);
157 }
158
159
160 template<typename T> typename char_traits<T>::int_type
161 char_traits<T>::to_int_type(char_type const & c)
162 {
163         return static_cast<int_type>(c);
164 }
165
166
167 template<typename T> bool
168 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
169 {
170         return c1 == c2;
171 }
172
173
174 template<typename T> typename char_traits<T>::int_type
175 char_traits<T>::eof()
176 {
177         return static_cast<int_type>(EOF);
178 }
179
180
181 template<typename T> typename char_traits<T>::int_type
182 char_traits<T>::not_eof(int_type const & c)
183 {
184         return !eq_int_type(c, eof()) ? c : to_int_type(char_type());
185 }
186
187 }
188 #endif
189 #endif