3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "docstring.h"
19 #include <boost/assert.hpp>
25 docstring const from_ascii(char const * ascii)
28 for (char const * c = ascii; *c; ++c) {
29 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
36 docstring const from_ascii(std::string const & ascii)
38 int const len = ascii.length();
39 for (int i = 0; i < len; ++i)
40 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
41 return docstring(ascii.begin(), ascii.end());
45 std::string const to_ascii(docstring const & ucs4)
47 int const len = ucs4.length();
50 for (int i = 0; i < len; ++i) {
51 BOOST_ASSERT(ucs4[i] < 0x80);
52 ascii[i] = static_cast<char>(ucs4[i]);
58 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
60 size_t n = utf8.size();
61 // as utf8 is a multi-byte encoding, there would be at most
67 int maxoutsize = n * 4;
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 = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
73 // adjust to the real converted size
78 docstring const from_utf8(std::string const & utf8)
81 utf8_to_ucs4(utf8, ucs4);
86 std::string const to_utf8(docstring const & ucs4)
88 std::vector<char> const utf8 =
89 ucs4_to_utf8(ucs4.data(), ucs4.size());
90 return std::string(utf8.begin(), utf8.end());
94 bool operator==(lyx::docstring const & l, char const * r)
96 int const len = l.length();
97 for (int i = 0; i < len; ++i) {
98 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
101 if (l[i] != lyx::docstring::value_type(r[i]))
104 return r[len] == '\0';
108 lyx::docstring operator+(lyx::docstring const & l, char const * r)
111 for (char const * c = r; *c; ++c) {
112 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
119 lyx::docstring operator+(char const * l, lyx::docstring const & r)
122 for (char const * c = l; *c; ++c) {
123 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
131 lyx::docstring operator+(lyx::docstring const & l, char r)
133 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
134 return l + lyx::docstring::value_type(r);
138 lyx::docstring operator+(char l, lyx::docstring const & r)
140 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
141 return lyx::docstring::value_type(l) + r;
145 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
147 for (char const * c = r; *c; ++c) {
148 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
155 lyx::docstring & operator+=(lyx::docstring & l, char r)
157 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
164 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
166 // gcc does not have proper locale facets for lyx::char_type if
167 // sizeof(wchar_t) == 2, so we have to implement them on our own.
170 // We get undefined references to these virtual methods. This looks like
171 // a bug in gcc. The implementation here does not do anything useful, since
172 // it is overriden in ascii_ctype_facet.
174 template<> ctype<lyx::char_type>::~ctype() {}
176 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
177 template<> lyx::char_type const *
178 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
179 template<> const lyx::char_type *
180 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
181 template<> const lyx::char_type *
182 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
183 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
184 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
185 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
186 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
187 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
188 template<> const char *
189 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
191 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
192 template<> const lyx::char_type *
193 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
199 class ctype_failure : public std::bad_cast {
201 ctype_failure() throw() : std::bad_cast() {}
202 virtual ~ctype_failure() throw() {}
203 virtual const char* what() const throw()
205 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
210 class num_put_failure : public std::bad_cast {
212 num_put_failure() throw() : std::bad_cast() {}
213 virtual ~num_put_failure() throw() {}
214 virtual const char* what() const throw()
216 return "The num_put locale facet does only support ASCII characters on this platform.";
221 /// ctype facet for UCS4 characters. The implementation does only support pure
222 /// ASCII, since we do not need anything else for now.
223 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
224 class ascii_ctype_facet : public std::ctype<lyx::char_type>
227 typedef lyx::char_type char_type;
228 typedef wctype_t wmask_type;
229 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
231 M_initialize_ctype();
236 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
238 wmask_type M_wmask[16];
239 wmask_type M_convert_to_wmask(const mask m) const
243 case space: ret = wctype("space"); break;
244 case print: ret = wctype("print"); break;
245 case cntrl: ret = wctype("cntrl"); break;
246 case upper: ret = wctype("upper"); break;
247 case lower: ret = wctype("lower"); break;
248 case alpha: ret = wctype("alpha"); break;
249 case digit: ret = wctype("digit"); break;
250 case punct: ret = wctype("punct"); break;
251 case xdigit: ret = wctype("xdigit"); break;
252 case alnum: ret = wctype("alnum"); break;
253 case graph: ret = wctype("graph"); break;
254 default: ret = wmask_type();
258 void M_initialize_ctype()
261 for (i = 0; i < 128; ++i) {
262 const int c = wctob(i);
266 M_narrow[i] = static_cast<char>(c);
272 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
273 M_widen[i] = btowc(i);
275 for (size_t i = 0; i <= 15; ++i) {
276 M_bit[i] = static_cast<mask>(1 << i);
277 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
280 virtual ~ascii_ctype_facet() {}
281 char_type do_toupper(char_type c) const
284 throw ctype_failure();
285 return toupper(static_cast<int>(c));
287 char_type const * do_toupper(char_type * lo, char_type const * hi) const
291 throw ctype_failure();
292 *lo = toupper(static_cast<int>(*lo));
297 char_type do_tolower(char_type c) const
300 throw ctype_failure();
303 char_type const * do_tolower(char_type * lo, char_type const * hi) const
307 throw ctype_failure();
313 bool do_is(mask m, char_type c) const
316 throw ctype_failure();
317 // The code below works because c is in the ASCII range.
318 // We could not use iswctype() which is designed for a 2byte
319 // whar_t without encoding conversion otherwise.
321 // Generically, 15 (instead of 10) since we don't know the numerical
322 // encoding of the various categories in /usr/include/ctype.h.
323 const size_t bitmasksize = 15;
324 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
325 if (m & M_bit[bitcur] &&
326 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
332 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
334 for (;lo < hi; ++vec, ++lo) {
336 throw ctype_failure();
337 // The code below works because c is in the ASCII range.
338 // We could not use iswctype() which is designed for a 2byte
339 // whar_t without encoding conversion otherwise.
340 // Generically, 15 (instead of 10) since we don't know the numerical
341 // encoding of the various categories in /usr/include/ctype.h.
342 const size_t bitmasksize = 15;
344 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
345 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
351 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
353 while (lo < hi && !this->do_is(m, *lo))
357 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
359 while (lo < hi && this->do_is(m, *lo) != 0)
363 char_type do_widen(char c) const
365 if (static_cast<unsigned char>(c) < 0x80)
367 throw ctype_failure();
369 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
372 if (static_cast<unsigned char>(*lo) >= 0x80)
373 throw ctype_failure();
380 char do_narrow(char_type wc, char) const
383 return static_cast<char>(wc);
384 throw ctype_failure();
386 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
390 *dest = static_cast<char>(*lo);
392 throw ctype_failure();
401 /// Facet for outputting numbers to odocstreams as ascii.
402 /// Here we simply need defining the virtual do_put functions.
403 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
405 typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
407 ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
409 /// Facet for converting numbers to ascii strings.
410 class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
413 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
418 do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
421 throw num_put_failure();
424 // 64 is large enough
426 string_num_put_facet f;
427 std::string::const_iterator cit = s.begin();
428 std::string::const_iterator end =
429 f.put(s.begin(), b, fill, v);
430 for (; cit != end; ++cit, ++oit)
438 /// Facet for inputting ascii representations of numbers from idocstreams.
439 /// Here we simply need defining the virtual do_get functions.
440 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
442 typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
444 ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
446 /// Facet for converting ascii representation of numbers to a value.
447 class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
450 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
454 bool isNumpunct(lyx::char_type const c) const
456 /// Only account for the standard numpunct "C" locale facet.
457 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
458 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
459 || c == 'x' || c == 'X');
464 do_get(iter_type iit, iter_type eit, std::ios_base & b,
465 std::ios_base::iostate & err, long & v) const
469 for (int i = 0; iit != eit && isNumpunct(*iit); ++i, ++iit)
470 s[i] = static_cast<char>(*iit);
471 string_num_get_facet f;
472 f.get(s.begin(), s.end(), b, err, v);
479 /// class to add our facets to the global locale
480 class locale_initializer {
485 std::locale const loc1(global, new ascii_ctype_facet);
486 std::locale const loc2(loc1, new ascii_num_put_facet);
487 std::locale const loc3(loc2, new ascii_num_get_facet);
488 std::locale::global(loc3);
495 /// make sure that our facets get used
496 static locale_initializer initializer;