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