]> git.lyx.org Git - lyx.git/blob - src/support/docstring.C
* LyXLex::Pimpl::buff is now a string.
[lyx.git] / src / support / docstring.C
1 /**
2  * \file docstring.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Georg Baum
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "docstring.h"
14 #include "unicode.h"
15
16 #include <locale>
17
18 #include <boost/assert.hpp>
19
20
21 namespace lyx {
22
23
24 docstring const from_ascii(char const * ascii)
25 {
26         docstring s;
27         for (char const * c = ascii; *c; ++c) {
28                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
29                 s.push_back(*c);
30         }
31         return s;
32 }
33
34
35 docstring const from_ascii(std::string const & ascii)
36 {
37         int const len = ascii.length();
38         for (int i = 0; i < len; ++i)
39                 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
40         return docstring(ascii.begin(), ascii.end());
41 }
42
43
44 std::string const to_ascii(docstring const & ucs4)
45 {
46         int const len = ucs4.length();
47         std::string ascii;
48         ascii.resize(len);
49         for (int i = 0; i < len; ++i) {
50                 BOOST_ASSERT(ucs4[i] < 0x80);
51                 ascii[i] = static_cast<char>(ucs4[i]);
52         }
53         return ascii;
54 }
55
56
57 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
58 {
59         size_t n = utf8.size();
60         // as utf8 is a multi-byte encoding, there would be at most
61         // n characters:
62         ucs4.resize(n);
63         if (n == 0)
64                 return;
65
66         int maxoutsize = n * 4;
67         int cd = -1;
68         // basic_string::data() is not recognized by some old gcc version
69         // so we use &(ucs4[0]) instead.
70         char * outbuf = (char *)(&(ucs4[0]));
71         int bytes = iconv_convert(cd, ucs4_codeset, "UTF-8",
72                 utf8.c_str(), n, outbuf, maxoutsize);
73
74         // adjust to the real converted size
75         ucs4.resize(bytes/4);
76 }
77
78
79 docstring const from_utf8(std::string const & utf8)
80 {
81         docstring ucs4;
82         utf8_to_ucs4(utf8, ucs4);
83         return ucs4;
84 }
85
86
87 std::string const to_utf8(docstring const & ucs4)
88 {
89         std::vector<char> const utf8 =
90                 ucs4_to_utf8(ucs4.data(), ucs4.size());
91         return std::string(utf8.begin(), utf8.end());
92 }
93
94
95 bool operator==(lyx::docstring const & l, char const * r)
96 {
97         int const len = l.length();
98         for (int i = 0; i < len; ++i) {
99                 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
100                 if (!r[i])
101                         return false;
102                 if (l[i] != lyx::docstring::value_type(r[i]))
103                         return false;
104         }
105         return r[len] == '\0';
106 }
107
108
109 lyx::docstring operator+(lyx::docstring const & l, char const * r)
110 {
111         lyx::docstring s(l);
112         for (char const * c = r; *c; ++c) {
113                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
114                 s.push_back(*c);
115         }
116         return s;
117 }
118
119
120 lyx::docstring operator+(char const * l, lyx::docstring const & r)
121 {
122         lyx::docstring s;
123         for (char const * c = l; *c; ++c) {
124                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
125                 s.push_back(*c);
126         }
127         s += r;
128         return s;
129 }
130
131
132 lyx::docstring operator+(lyx::docstring const & l, char r)
133 {
134         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
135         return l + lyx::docstring::value_type(r);
136 }
137
138
139 lyx::docstring operator+(char l, lyx::docstring const & r)
140 {
141         BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
142         return lyx::docstring::value_type(l) + r;
143 }
144
145
146 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
147 {
148         for (char const * c = r; *c; ++c) {
149                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
150                 l.push_back(*c);
151         }
152         return l;
153 }
154
155
156 lyx::docstring & operator+=(lyx::docstring & l, char r)
157 {
158         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
159         l.push_back(r);
160         return l;
161 }
162
163 } // namespace lyx
164
165 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
166
167 // gcc does not have proper locale facets for lyx::char_type if
168 // sizeof(wchar_t) == 2, so we have to implement them on our own.
169
170
171 // We get undefined references to these virtual methods. This looks like
172 // a bug in gcc. The implementation here does not do anything useful, since
173 // it is overriden in ascii_ctype_facet.
174 namespace std {
175 template<> ctype<lyx::char_type>::~ctype() {}
176 template<> bool
177 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
178 template<> lyx::char_type const *
179 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
180 template<> const lyx::char_type *
181 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
182 template<> const lyx::char_type *
183 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
184 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
185 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
186 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
187 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
188 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
189 template<> const char *
190 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
191 template<> char
192 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
193 template<> const lyx::char_type *
194 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
195 }
196
197
198 namespace lyx {
199
200 class ctype_failure : public std::bad_cast {
201 public:
202         ctype_failure() throw() : std::bad_cast() {}
203         virtual ~ctype_failure() throw() {}
204         virtual const char* what() const throw()
205         {
206                 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
207         }
208 };
209
210
211 /// ctype facet for UCS4 characters. The implementation does only support pure
212 /// ASCII, since we do not need anything else for now.
213 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
214 class ascii_ctype_facet : public std::ctype<lyx::char_type>
215 {
216 public:
217         typedef lyx::char_type char_type;
218         typedef wctype_t wmask_type;
219         explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
220         {
221                 M_initialize_ctype();
222         }
223 protected:
224         bool       M_narrow_ok;
225         char       M_narrow[128];
226         wint_t     M_widen[1 + static_cast<unsigned char>(-1)];
227         mask       M_bit[16];
228         wmask_type M_wmask[16];
229         wmask_type M_convert_to_wmask(const mask m) const
230         {
231                 wmask_type ret;
232                 switch (m) {
233                         case space:  ret = wctype("space");  break;
234                         case print:  ret = wctype("print");  break;
235                         case cntrl:  ret = wctype("cntrl");  break;
236                         case upper:  ret = wctype("upper");  break;
237                         case lower:  ret = wctype("lower");  break;
238                         case alpha:  ret = wctype("alpha");  break;
239                         case digit:  ret = wctype("digit");  break;
240                         case punct:  ret = wctype("punct");  break;
241                         case xdigit: ret = wctype("xdigit"); break;
242                         case alnum:  ret = wctype("alnum");  break;
243                         case graph:  ret = wctype("graph");  break;
244                         default:     ret = wmask_type();
245                 }
246                 return ret;
247         }
248         void M_initialize_ctype()
249         {
250                 wint_t i;
251                 for (i = 0; i < 128; ++i) {
252                         const int c = wctob(i);
253                         if (c == EOF)
254                                 break;
255                         else
256                                 M_narrow[i] = static_cast<char>(c);
257                 }
258                 if (i == 128)
259                         M_narrow_ok = true;
260                 else
261                         M_narrow_ok = false;
262                 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
263                         M_widen[i] = btowc(i);
264
265                 for (size_t i = 0; i <= 15; ++i) {
266                         M_bit[i] = static_cast<mask>(1 << i);
267                         M_wmask[i] = M_convert_to_wmask(M_bit[i]);
268                 }
269         }
270         virtual ~ascii_ctype_facet() {}
271         char_type do_toupper(char_type c) const
272         {
273                 if (c >= 0x80)
274                         throw ctype_failure();
275                 return toupper(static_cast<int>(c));
276         }
277         char_type const * do_toupper(char_type * lo, char_type const * hi) const
278         {
279                 while (lo < hi) {
280                         if (*lo >= 0x80)
281                                 throw ctype_failure();
282                         *lo = toupper(static_cast<int>(*lo));
283                         ++lo;
284                 }
285                 return hi;
286         }
287         char_type do_tolower(char_type c) const
288         {
289                 if (c >= 0x80)
290                         throw ctype_failure();
291                 return tolower(c);
292         }
293         char_type const * do_tolower(char_type * lo, char_type const * hi) const
294         {
295                 while (lo < hi) {
296                         if (*lo >= 0x80)
297                                 throw ctype_failure();
298                         *lo = tolower(*lo);
299                         ++lo;
300                 }
301                 return hi;
302         }
303         bool do_is(mask m, char_type c) const
304         {
305                 if (c >= 0x80)
306                         throw ctype_failure();
307                 // The code below works because c is in the ASCII range.
308                 // We could not use iswctype() which is designed for a 2byte
309                 // whar_t without encoding conversion otherwise.
310                 bool ret = false;
311                 // Generically, 15 (instead of 10) since we don't know the numerical
312                 // encoding of the various categories in /usr/include/ctype.h.
313                 const size_t bitmasksize = 15;
314                 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
315                         if (m & M_bit[bitcur] &&
316                             iswctype(static_cast<int>(c), M_wmask[bitcur])) {
317                                 ret = true;
318                                 break;
319                         }
320                 return ret;
321         }
322         char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
323         {
324                 for (;lo < hi; ++vec, ++lo) {
325                         if (*lo >= 0x80)
326                                 throw ctype_failure();
327                         // The code below works because c is in the ASCII range.
328                         // We could not use iswctype() which is designed for a 2byte
329                         // whar_t without encoding conversion otherwise.
330                         // Generically, 15 (instead of 10) since we don't know the numerical
331                         // encoding of the various categories in /usr/include/ctype.h.
332                         const size_t bitmasksize = 15;
333                         mask m = 0;
334                         for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
335                                 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
336                                         m |= M_bit[bitcur];
337                         *vec = m;
338                 }
339                 return hi;
340         }
341         char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
342         {
343                 while (lo < hi && !this->do_is(m, *lo))
344                         ++lo;
345                 return lo;
346         }
347         char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
348         {
349                 while (lo < hi && this->do_is(m, *lo) != 0)
350                         ++lo;
351                 return lo;
352         }
353         char_type do_widen(char c) const
354         {
355                 if (static_cast<unsigned char>(c) < 0x80)
356                         return c;
357                 throw ctype_failure();
358         }
359         const char* do_widen(const char* lo, const char* hi, char_type* dest) const
360         {
361                 while (lo < hi) {
362                         if (static_cast<unsigned char>(*lo) >= 0x80)
363                                 throw ctype_failure();
364                         *dest = *lo;
365                         ++lo;
366                         ++dest;
367                 }
368                 return hi;
369         }
370         char do_narrow(char_type wc, char) const
371         {
372                 if (wc < 0x80)
373                         return static_cast<char>(wc);
374                 throw ctype_failure();
375         }
376         const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
377         {
378                 while (lo < hi) {
379                         if (*lo < 0x80)
380                                 *dest = static_cast<char>(*lo);
381                         else
382                                 throw ctype_failure();
383                         ++lo;
384                         ++dest;
385                 }
386                 return hi;
387         }
388 };
389
390
391 /// class to add our ascii_ctype_facet to the global locale
392 class locale_initializer {
393 public:
394         locale_initializer()
395         {
396                 std::locale global;
397                 std::locale const loc(global, new ascii_ctype_facet);
398                 std::locale::global(loc);
399         }
400 };
401
402
403 namespace {
404
405 /// make sure that our ascii_ctype_facet gets used
406 static locale_initializer initializer;
407
408 }
409 }
410 #endif