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