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"
14 #include "qstring_helpers.h"
20 #include <boost/assert.hpp>
26 docstring const from_ascii(char const * ascii)
29 for (char const * c = ascii; *c; ++c) {
30 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
37 docstring const from_ascii(std::string const & ascii)
39 int const len = ascii.length();
40 for (int i = 0; i < len; ++i)
41 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
42 return docstring(ascii.begin(), ascii.end());
46 std::string const to_ascii(docstring const & ucs4)
48 int const len = ucs4.length();
51 for (int i = 0; i < len; ++i) {
52 BOOST_ASSERT(ucs4[i] < 0x80);
53 ascii[i] = static_cast<char>(ucs4[i]);
59 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
61 size_t n = utf8.size();
62 // as utf8 is a multi-byte encoding, there would be at most
68 int maxoutsize = n * 4;
69 // basic_string::data() is not recognized by some old gcc version
70 // so we use &(ucs4[0]) instead.
71 char * outbuf = (char *)(&(ucs4[0]));
72 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
74 // adjust to the real converted size
79 docstring const from_utf8(std::string const & utf8)
82 utf8_to_ucs4(utf8, ucs4);
87 std::string const to_utf8(docstring const & ucs4)
89 std::vector<char> const utf8 =
90 ucs4_to_utf8(ucs4.data(), ucs4.size());
91 return std::string(utf8.begin(), utf8.end());
95 docstring const from_local8bit(std::string const & s)
97 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
101 const char* to_local8bit_failure::what() const throw()
103 return "A string could not be converted from unicode to the local 8 bit encoding.";
107 std::string const to_local8bit(docstring const & s)
109 // This conversion can fail, depending on input.
111 return std::string();
112 QByteArray const local = toqstr(s).toLocal8Bit();
113 if (local.size() == 0)
114 throw to_local8bit_failure();
115 return std::string(local.begin(), local.end());
119 bool operator==(lyx::docstring const & l, char const * r)
121 int const len = l.length();
122 for (int i = 0; i < len; ++i) {
123 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
126 if (l[i] != lyx::docstring::value_type(r[i]))
129 return r[len] == '\0';
133 lyx::docstring operator+(lyx::docstring const & l, char const * r)
136 for (char const * c = r; *c; ++c) {
137 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
144 lyx::docstring operator+(char const * l, lyx::docstring const & r)
147 for (char const * c = l; *c; ++c) {
148 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
156 lyx::docstring operator+(lyx::docstring const & l, char r)
158 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
159 return l + lyx::docstring::value_type(r);
163 lyx::docstring operator+(char l, lyx::docstring const & r)
165 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
166 return lyx::docstring::value_type(l) + r;
170 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
172 for (char const * c = r; *c; ++c) {
173 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
180 lyx::docstring & operator+=(lyx::docstring & l, char r)
182 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
189 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
191 // gcc does not have proper locale facets for lyx::char_type if
192 // sizeof(wchar_t) == 2, so we have to implement them on our own.
195 // We get undefined references to these virtual methods. This looks like
196 // a bug in gcc. The implementation here does not do anything useful, since
197 // it is overriden in ascii_ctype_facet.
199 template<> ctype<lyx::char_type>::~ctype() {}
201 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
202 template<> lyx::char_type const *
203 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
204 template<> const lyx::char_type *
205 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
206 template<> const lyx::char_type *
207 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
208 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
209 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
210 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
211 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
212 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
213 template<> const char *
214 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
216 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
217 template<> const lyx::char_type *
218 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
224 class ctype_failure : public std::bad_cast {
226 ctype_failure() throw() : std::bad_cast() {}
227 virtual ~ctype_failure() throw() {}
228 virtual const char* what() const throw()
230 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
235 class num_put_failure : public std::bad_cast {
237 num_put_failure() throw() : std::bad_cast() {}
238 virtual ~num_put_failure() throw() {}
239 virtual const char* what() const throw()
241 return "The num_put locale facet does only support ASCII characters on this platform.";
246 /// ctype facet for UCS4 characters. The implementation does only support pure
247 /// ASCII, since we do not need anything else for now.
248 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
249 class ascii_ctype_facet : public std::ctype<lyx::char_type>
252 typedef lyx::char_type char_type;
253 typedef wctype_t wmask_type;
254 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
256 M_initialize_ctype();
261 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
263 wmask_type M_wmask[16];
264 wmask_type M_convert_to_wmask(const mask m) const
268 case space: ret = wctype("space"); break;
269 case print: ret = wctype("print"); break;
270 case cntrl: ret = wctype("cntrl"); break;
271 case upper: ret = wctype("upper"); break;
272 case lower: ret = wctype("lower"); break;
273 case alpha: ret = wctype("alpha"); break;
274 case digit: ret = wctype("digit"); break;
275 case punct: ret = wctype("punct"); break;
276 case xdigit: ret = wctype("xdigit"); break;
277 case alnum: ret = wctype("alnum"); break;
278 case graph: ret = wctype("graph"); break;
279 default: ret = wmask_type();
283 void M_initialize_ctype()
286 for (i = 0; i < 128; ++i) {
287 const int c = wctob(i);
291 M_narrow[i] = static_cast<char>(c);
297 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
298 M_widen[i] = btowc(i);
300 for (size_t i = 0; i <= 15; ++i) {
301 M_bit[i] = static_cast<mask>(1 << i);
302 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
305 virtual ~ascii_ctype_facet() {}
306 char_type do_toupper(char_type c) const
309 throw ctype_failure();
310 return toupper(static_cast<int>(c));
312 char_type const * do_toupper(char_type * lo, char_type const * hi) const
316 throw ctype_failure();
317 *lo = toupper(static_cast<int>(*lo));
322 char_type do_tolower(char_type c) const
325 throw ctype_failure();
328 char_type const * do_tolower(char_type * lo, char_type const * hi) const
332 throw ctype_failure();
338 bool do_is(mask m, char_type c) const
341 throw ctype_failure();
342 // The code below works because c is in the ASCII range.
343 // We could not use iswctype() which is designed for a 2byte
344 // whar_t without encoding conversion otherwise.
346 // Generically, 15 (instead of 10) since we don't know the numerical
347 // encoding of the various categories in /usr/include/ctype.h.
348 const size_t bitmasksize = 15;
349 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
350 if (m & M_bit[bitcur] &&
351 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
357 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
359 for (;lo < hi; ++vec, ++lo) {
361 throw ctype_failure();
362 // The code below works because c is in the ASCII range.
363 // We could not use iswctype() which is designed for a 2byte
364 // whar_t without encoding conversion otherwise.
365 // Generically, 15 (instead of 10) since we don't know the numerical
366 // encoding of the various categories in /usr/include/ctype.h.
367 const size_t bitmasksize = 15;
369 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
370 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
376 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
378 while (lo < hi && !this->do_is(m, *lo))
382 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
384 while (lo < hi && this->do_is(m, *lo) != 0)
388 char_type do_widen(char c) const
390 if (static_cast<unsigned char>(c) < 0x80)
392 throw ctype_failure();
394 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
397 if (static_cast<unsigned char>(*lo) >= 0x80)
398 throw ctype_failure();
405 char do_narrow(char_type wc, char) const
408 return static_cast<char>(wc);
409 throw ctype_failure();
411 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
415 *dest = static_cast<char>(*lo);
417 throw ctype_failure();
426 /// Facet for outputting numbers to odocstreams as ascii.
427 /// Here we simply need defining the virtual do_put functions.
428 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
430 typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
432 ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
434 /// Facet for converting numbers to ascii strings.
435 class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
438 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
443 do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
445 return do_put_helper(oit, b, fill, v);
449 do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
451 return do_put_helper(oit, b, fill, v);
455 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
457 return do_put_helper(oit, b, fill, v);
460 #ifdef _GLIBCXX_USE_LONG_LONG
462 do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
464 return do_put_helper(oit, b, fill, v);
468 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
470 return do_put_helper(oit, b, fill, v);
475 do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
477 return do_put_helper(oit, b, fill, v);
481 do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
483 return do_put_helper(oit, b, fill, v);
487 do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
489 return do_put_helper(oit, b, fill, v);
493 template <typename ValueType>
495 do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
498 throw num_put_failure();
500 std::streamsize const sz = b.width() > b.precision() ?
501 b.width() : b.precision();
502 // 64 is large enough, unless width or precision are bigger
503 std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
504 std::string s(wd, '\0');
505 string_num_put_facet f;
506 std::string::const_iterator cit = s.begin();
507 std::string::const_iterator end =
508 f.put(s.begin(), b, fill, v);
509 for (; cit != end; ++cit, ++oit)
517 /// Facet for inputting ascii representations of numbers from idocstreams.
518 /// Here we simply need defining the virtual do_get functions.
519 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
521 typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
523 ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
525 /// Facet for converting ascii representation of numbers to a value.
526 class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
529 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
533 bool isNumpunct(lyx::char_type const c) const
535 /// Only account for the standard numpunct "C" locale facet.
536 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
537 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
538 || c == 'x' || c == 'X');
543 do_get(iter_type iit, iter_type eit, std::ios_base & b,
544 std::ios_base::iostate & err, long & v) const
548 for (; iit != eit && isNumpunct(*iit); ++iit)
549 s += static_cast<char>(*iit);
550 // We add another character, not part of the numpunct facet,
551 // in order to avoid setting the eofbit in the stream state,
552 // which would prevent any further read. The space seems a
555 string_num_get_facet f;
556 f.get(s.begin(), s.end(), b, err, v);
563 /// class to add our facets to the global locale
564 class locale_initializer {
569 std::locale const loc1(global, new ascii_ctype_facet);
570 std::locale const loc2(loc1, new ascii_num_put_facet);
571 std::locale const loc3(loc2, new ascii_num_get_facet);
572 std::locale::global(loc3);
579 /// make sure that our facets get used
580 static locale_initializer initializer;