]> git.lyx.org Git - lyx.git/blob - src/support/docstring.h
Fix build with GNU libstdc++ C++11 ABI
[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/strfwd.h"
17
18 #include <string>
19
20 namespace lyx {
21
22 /// Creates a docstring from a C string of ASCII characters
23 docstring const from_ascii(char const *);
24
25 /// Creates a docstring from a std::string of ASCII characters
26 docstring const from_ascii(std::string const &);
27
28 /// Creates a std::string of ASCII characters from a docstring
29 std::string const to_ascii(docstring const &);
30
31 /// Creates a docstring from a UTF8 string. This should go eventually.
32 docstring const from_utf8(std::string const &);
33
34 /// Creates a UTF8 string from a docstring. This should go eventually.
35 std::string const to_utf8(docstring const &);
36
37 /// convert \p s from the encoding of the locale to ucs4.
38 docstring const from_local8bit(std::string const & s);
39
40 /**
41  * Convert \p s from ucs4 to the encoding of the locale.
42  * This may fail and throw an exception, the caller is expected to act
43  * appropriately.
44  */
45 std::string const to_local8bit(docstring const & s);
46
47 /// convert \p s from the encoding of the file system to ucs4.
48 docstring const from_filesystem8bit(std::string const & s);
49
50 /// convert \p s from ucs4 to the encoding of the file system.
51 std::string const to_filesystem8bit(docstring const & s);
52
53 /// convert \p s from ucs4 to the \p encoding.
54 std::string const to_iconv_encoding(docstring const & s,
55                                     std::string const & encoding);
56
57 /// convert \p s from \p encoding to ucs4.
58 docstring const from_iconv_encoding(std::string const & s,
59                                     std::string const & encoding);
60
61 /// normalize \p s to precomposed form c
62 docstring const normalize_c(docstring const & s);
63
64 /// Compare a docstring with a C string of ASCII characters
65 bool operator==(docstring const &, char const *);
66
67 /// Compare a C string of ASCII characters with a docstring
68 inline bool operator==(char const * l, docstring const & r) { return r == l; }
69
70 /// Compare a docstring with a C string of ASCII characters
71 inline bool operator!=(docstring const & l, char const * r) { return !(l == r); }
72
73 /// Compare a C string of ASCII characters with a docstring
74 inline bool operator!=(char const * l, docstring const & r) { return !(r == l); }
75
76 /// Concatenate a docstring and a C string of ASCII characters
77 docstring operator+(docstring const &, char const *);
78
79 /// Concatenate a C string of ASCII characters and a docstring
80 docstring operator+(char const *, docstring const &);
81
82 /// Concatenate a docstring and a single ASCII character
83 docstring operator+(docstring const & l, char r);
84
85 /// Concatenate a single ASCII character and a docstring
86 docstring operator+(char l, docstring const & r);
87
88 /// Append a C string of ASCII characters to a docstring
89 docstring & operator+=(docstring &, char const *);
90
91 /// Append a single ASCII character to a docstring
92 docstring & operator+=(docstring & l, char r);
93
94 } // namespace lyx
95
96
97 #if ! defined(USE_WCHAR_T) && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
98 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
99 namespace std {
100
101 template<typename T> void
102 char_traits<T>::assign(char_type & c1, char_type const & c2)
103 {
104         c1 = c2;
105 }
106
107
108 template<typename T> bool
109 char_traits<T>::eq(char_type const & c1, char_type const & c2)
110 {
111         return c1 == c2;
112 }
113
114
115 template<typename T> bool
116 char_traits<T>::lt(char_type const & c1, char_type const & c2)
117 {
118         return c1 < c2;
119 }
120
121
122 template<typename T> int
123 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
124 {
125         for (std::size_t i = 0; i < n; ++i)
126                 if (lt(s1[i], s2[i]))
127                         return -1;
128                 else if (lt(s2[i], s1[i]))
129                         return 1;
130         return 0;
131 }
132
133
134 template<typename T> std::size_t
135 char_traits<T>::length(char_type const * p)
136 {
137         std::size_t i = 0;
138         while (!eq(p[i], char_type()))
139                 ++i;
140         return i;
141 }
142
143
144 template<typename T> typename char_traits<T>::char_type const *
145 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
146 {
147         for (std::size_t i = 0; i < n; ++i)
148                 if (eq(s[i], a))
149                         return s + i;
150         return 0;
151 }
152
153
154 template<typename T> typename char_traits<T>::char_type *
155 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
156 {
157         return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
158 }
159
160
161 template<typename T> typename char_traits<T>::char_type *
162 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
163 {
164         std::copy(s2, s2 + n, s1);
165         return s1;
166 }
167
168
169 template<typename T> typename char_traits<T>::char_type *
170 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
171 {
172         std::fill_n(s, n, a);
173         return s;
174 }
175
176
177 template<typename T> typename char_traits<T>::char_type
178 char_traits<T>::to_char_type(int_type const & c)
179 {
180         return static_cast<char_type>(c);
181 }
182
183
184 template<typename T> typename char_traits<T>::int_type
185 char_traits<T>::to_int_type(char_type const & c)
186 {
187         return static_cast<int_type>(c);
188 }
189
190
191 template<typename T> bool
192 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
193 {
194         return c1 == c2;
195 }
196
197
198 template<typename T> typename char_traits<T>::int_type
199 char_traits<T>::eof()
200 {
201         return static_cast<int_type>(EOF);
202 }
203
204
205 template<typename T> typename char_traits<T>::int_type
206 char_traits<T>::not_eof(int_type const & c)
207 {
208         return !eq_int_type(c, eof()) ? c : to_int_type(char_type());
209 }
210
211 }
212 #endif
213 #endif