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 "support/docstring.h"
15 #include "support/lassert.h"
16 #include "support/lstrings.h"
17 #include "support/qstring_helpers.h"
18 #include "support/unicode.h"
24 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
31 using lyx::support::isHexChar;
35 docstring const from_ascii(char const * ascii)
38 if (int n = strlen(ascii)) {
43 LATTEST(static_cast<unsigned char>(ascii[n]) < 0x80);
50 docstring const from_ascii(string const & ascii)
52 int const len = ascii.length();
53 for (int i = 0; i < len; ++i)
54 LATTEST(static_cast<unsigned char>(ascii[i]) < 0x80);
55 return docstring(ascii.begin(), ascii.end());
59 string const to_ascii(docstring const & ucs4)
61 int const len = ucs4.length();
64 for (int i = 0; i < len; ++i) {
65 LATTEST(ucs4[i] < 0x80);
66 ascii[i] = static_cast<char>(ucs4[i]);
72 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
74 size_t n = utf8.size();
75 // as utf8 is a multi-byte encoding, there would be at most
81 int maxoutsize = n * 4;
82 // basic_string::data() is not recognized by some old gcc version
83 // so we use &(ucs4[0]) instead.
84 char * outbuf = (char *)(&(ucs4[0]));
85 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
87 // adjust to the real converted size
92 docstring const from_utf8(string const & utf8)
95 utf8_to_ucs4(utf8, ucs4);
100 string const to_utf8(docstring const & ucs4)
102 vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
103 return string(utf8.begin(), utf8.end());
107 docstring const from_local8bit(string const & s)
109 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
113 /// Exception thrown by to_local8bit if the string could not be converted
114 class to_local8bit_failure : public bad_cast {
116 to_local8bit_failure() throw() : bad_cast() {}
117 virtual ~to_local8bit_failure() throw() {}
118 virtual const char* what() const throw()
120 return "A string could not be converted from unicode to the local 8 bit encoding.";
125 string const to_local8bit(docstring const & s)
127 // This conversion can fail, depending on input.
130 QByteArray const local = toqstr(s).toLocal8Bit();
132 throw to_local8bit_failure();
133 return string(local.begin(), local.end());
137 docstring const from_filesystem8bit(string const & s)
139 QByteArray const encoded(s.c_str(), s.length());
140 return qstring_to_ucs4(QFile::decodeName(encoded));
144 string const to_filesystem8bit(docstring const & s)
146 QByteArray const encoded = QFile::encodeName(toqstr(s));
147 return string(encoded.begin(), encoded.end());
151 string const to_iconv_encoding(docstring const & s, string const & encoding)
153 std::vector<char> const encoded =
154 ucs4_to_eightbit(s.data(), s.length(), encoding);
155 return string(encoded.begin(), encoded.end());
159 docstring const from_iconv_encoding(string const & s, string const & encoding)
161 std::vector<char_type> const ucs4 =
162 eightbit_to_ucs4(s.data(), s.length(), encoding);
163 return docstring(ucs4.begin(), ucs4.end());
167 docstring const normalize_c(docstring const & s)
169 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
173 bool operator==(lyx::docstring const & l, char const * r)
175 lyx::docstring::const_iterator it = l.begin();
176 lyx::docstring::const_iterator end = l.end();
177 for (; it != end; ++it, ++r) {
178 LASSERT(static_cast<unsigned char>(*r) < 0x80, return false);
181 if (*it != static_cast<lyx::docstring::value_type>(*r))
188 lyx::docstring operator+(lyx::docstring const & l, char const * r)
191 for (char const * c = r; *c; ++c) {
192 LASSERT(static_cast<unsigned char>(*c) < 0x80, return l);
199 lyx::docstring operator+(char const * l, lyx::docstring const & r)
202 for (char const * c = l; *c; ++c) {
203 LASSERT(static_cast<unsigned char>(*c) < 0x80, return r);
211 lyx::docstring operator+(lyx::docstring const & l, char r)
213 LASSERT(static_cast<unsigned char>(r) < 0x80, return l);
215 s += docstring::value_type(r);
220 lyx::docstring operator+(char l, lyx::docstring const & r)
222 LASSERT(static_cast<unsigned char>(l) < 0x80, return r);
223 return lyx::docstring::value_type(l) + r;
227 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
229 for (char const * c = r; *c; ++c) {
230 LASSERT(static_cast<unsigned char>(*c) < 0x80, return l);
237 lyx::docstring & operator+=(lyx::docstring & l, char r)
239 LASSERT(static_cast<unsigned char>(r) < 0x80, return l);
246 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
248 // gcc does not have proper locale facets for lyx::char_type if
249 // sizeof(wchar_t) == 2, so we have to implement them on our own.
252 // We get undefined references to these virtual methods. This looks like
253 // a bug in gcc. The implementation here does not do anything useful, since
254 // it is overriden in ascii_ctype_facet.
256 template<> ctype<lyx::char_type>::~ctype() {}
258 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
259 template<> lyx::char_type const *
260 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
261 template<> const lyx::char_type *
262 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
263 template<> const lyx::char_type *
264 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
265 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
266 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
267 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
268 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
269 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
270 template<> const char *
271 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
273 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
274 template<> const lyx::char_type *
275 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
281 class ctype_failure : public bad_cast {
283 ctype_failure() throw() : bad_cast() {}
284 virtual ~ctype_failure() throw() {}
285 virtual const char* what() const throw()
287 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
292 class num_put_failure : public bad_cast {
294 num_put_failure() throw() : bad_cast() {}
295 virtual ~num_put_failure() throw() {}
296 virtual const char* what() const throw()
298 return "The num_put locale facet does only support ASCII characters on this platform.";
303 /// ctype facet for UCS4 characters. The implementation does only support pure
304 /// ASCII, since we do not need anything else for now.
305 /// The code is partly stolen from ctype<wchar_t> from gcc.
306 class ascii_ctype_facet : public ctype<lyx::char_type>
309 typedef lyx::char_type char_type;
310 typedef wctype_t wmask_type;
311 explicit ascii_ctype_facet(size_t refs = 0) : ctype<char_type>(refs)
313 M_initialize_ctype();
318 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
320 wmask_type M_wmask[16];
321 wmask_type M_convert_to_wmask(const mask m) const
325 case space: ret = wctype("space"); break;
326 case print: ret = wctype("print"); break;
327 case cntrl: ret = wctype("cntrl"); break;
328 case upper: ret = wctype("upper"); break;
329 case lower: ret = wctype("lower"); break;
330 case alpha: ret = wctype("alpha"); break;
331 case digit: ret = wctype("digit"); break;
332 case punct: ret = wctype("punct"); break;
333 case xdigit: ret = wctype("xdigit"); break;
334 case alnum: ret = wctype("alnum"); break;
335 case graph: ret = wctype("graph"); break;
336 default: ret = wmask_type();
340 void M_initialize_ctype()
343 for (i = 0; i < 128; ++i) {
344 const int c = wctob(i);
348 M_narrow[i] = static_cast<char>(c);
354 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
355 M_widen[i] = btowc(i);
357 for (size_t i = 0; i <= 15; ++i) {
358 M_bit[i] = static_cast<mask>(1 << i);
359 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
362 virtual ~ascii_ctype_facet() {}
363 char_type do_toupper(char_type c) const
366 throw ctype_failure();
367 return toupper(static_cast<int>(c));
369 char_type const * do_toupper(char_type * lo, char_type const * hi) const
373 throw ctype_failure();
374 *lo = toupper(static_cast<int>(*lo));
379 char_type do_tolower(char_type c) const
382 throw ctype_failure();
385 char_type const * do_tolower(char_type * lo, char_type const * hi) const
389 throw ctype_failure();
395 bool do_is(mask m, char_type c) const
398 throw ctype_failure();
399 // The code below works because c is in the ASCII range.
400 // We could not use iswctype() which is designed for a 2byte
401 // whar_t without encoding conversion otherwise.
403 // Generically, 15 (instead of 10) since we don't know the numerical
404 // encoding of the various categories in /usr/include/ctype.h.
405 const size_t bitmasksize = 15;
406 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
407 if (m & M_bit[bitcur] &&
408 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
414 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
416 for (;lo < hi; ++vec, ++lo) {
418 throw ctype_failure();
419 // The code below works because c is in the ASCII range.
420 // We could not use iswctype() which is designed for a 2byte
421 // whar_t without encoding conversion otherwise.
422 // Generically, 15 (instead of 10) since we don't know the numerical
423 // encoding of the various categories in /usr/include/ctype.h.
424 const size_t bitmasksize = 15;
426 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
427 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
433 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
435 while (lo < hi && !this->do_is(m, *lo))
439 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
441 while (lo < hi && this->do_is(m, *lo) != 0)
445 char_type do_widen(char c) const
447 if (static_cast<unsigned char>(c) < 0x80)
449 throw ctype_failure();
451 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
454 if (static_cast<unsigned char>(*lo) >= 0x80)
455 throw ctype_failure();
462 char do_narrow(char_type wc, char) const
465 return static_cast<char>(wc);
466 throw ctype_failure();
468 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
472 *dest = static_cast<char>(*lo);
474 throw ctype_failure();
483 /// Facet for outputting numbers to odocstreams as ascii.
484 /// Here we simply need defining the virtual do_put functions.
485 class ascii_num_put_facet : public num_put<lyx::char_type, ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
487 typedef ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
489 ascii_num_put_facet(size_t refs = 0) : num_put<lyx::char_type, iter_type>(refs) {}
491 /// Facet for converting numbers to ascii strings.
492 class string_num_put_facet : public num_put<char, basic_string<char>::iterator>
495 string_num_put_facet() : num_put<char, basic_string<char>::iterator>(1) {}
500 do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
502 return do_put_helper(oit, b, fill, v);
506 do_put(iter_type oit, ios_base & b, char_type fill, long v) const
508 return do_put_helper(oit, b, fill, v);
512 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
514 return do_put_helper(oit, b, fill, v);
517 #ifdef _GLIBCXX_USE_LONG_LONG
519 do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
521 return do_put_helper(oit, b, fill, v);
525 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
527 return do_put_helper(oit, b, fill, v);
532 do_put(iter_type oit, ios_base & b, char_type fill, double v) const
534 return do_put_helper(oit, b, fill, v);
538 do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
540 return do_put_helper(oit, b, fill, v);
544 do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
546 return do_put_helper(oit, b, fill, v);
550 template <typename ValueType>
552 do_put_helper(iter_type oit, ios_base & b, char_type fill, ValueType v) const
555 throw num_put_failure();
557 streamsize const sz = b.width() > b.precision() ?
558 b.width() : b.precision();
559 // 64 is large enough, unless width or precision are bigger
560 streamsize const wd = (sz > 56 ? sz : 56) + 8;
562 string_num_put_facet f;
563 string::const_iterator cit = s.begin();
564 string::const_iterator end =
565 f.put(s.begin(), b, fill, v);
566 for (; cit != end; ++cit, ++oit)
574 /// Facet for inputting ascii representations of numbers from idocstreams.
575 /// Here we simply need defining the virtual do_get functions.
576 class ascii_num_get_facet : public num_get<lyx::char_type, istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
578 typedef istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
580 ascii_num_get_facet(size_t refs = 0) : num_get<lyx::char_type, iter_type>(refs) {}
582 /// Facet for converting ascii representation of numbers to a value.
583 class string_num_get_facet : public num_get<char, basic_string<char>::iterator>
586 string_num_get_facet() : num_get<char, basic_string<char>::iterator>(1) {}
589 /// Numpunct facet defining the I/O format.
590 class numpunct_facet : public numpunct<char>
593 numpunct_facet() : numpunct<char>(1) {}
598 do_get(iter_type iit, iter_type eit, ios_base & b,
599 ios_base::iostate & err, bool & v) const
601 if (b.flags() & ios_base::boolalpha) {
603 lyx::docstring const truename = from_local8bit(p.truename());
604 lyx::docstring const falsename = from_local8bit(p.falsename());
609 size_t const tsize = truename.size();
610 size_t const fsize = falsename.size();
611 for (; iit != eit; ++iit) {
614 bool true_ok = support::prefixIs(truename, s);
615 bool false_ok = support::prefixIs(falsename, s);
616 if (!true_ok && !false_ok) {
621 if ((true_ok && n == tsize) ||
622 (false_ok && n == fsize)) {
628 err = ios_base::goodbit;
629 v = truename == s ? true : false;
631 err = ios_base::failbit;
633 err |= ios_base::eofbit;
637 iter_type end = this->do_get(iit, eit, b, err, l);
638 if (!(err & ios_base::failbit)) {
644 err |= ios_base::failbit;
651 do_get(iter_type iit, iter_type eit, ios_base & b,
652 ios_base::iostate & err, long & v) const
654 return do_get_integer(iit, eit, b, err, v);
658 do_get(iter_type iit, iter_type eit, ios_base & b,
659 ios_base::iostate & err, unsigned short & v) const
661 return do_get_integer(iit, eit, b, err, v);
665 do_get(iter_type iit, iter_type eit, ios_base & b,
666 ios_base::iostate & err, unsigned int & v) const
668 return do_get_integer(iit, eit, b, err, v);
672 do_get(iter_type iit, iter_type eit, ios_base & b,
673 ios_base::iostate & err, unsigned long & v) const
675 return do_get_integer(iit, eit, b, err, v);
678 #ifdef _GLIBCXX_USE_LONG_LONG
680 do_get(iter_type iit, iter_type eit, ios_base & b,
681 ios_base::iostate & err, long long & v) const
683 return do_get_integer(iit, eit, b, err, v);
687 do_get(iter_type iit, iter_type eit, ios_base & b,
688 ios_base::iostate & err, unsigned long long & v) const
690 return do_get_integer(iit, eit, b, err, v);
695 do_get(iter_type iit, iter_type eit, ios_base & b,
696 ios_base::iostate & err, float & v) const
698 return do_get_float(iit, eit, b, err, v);
702 do_get(iter_type iit, iter_type eit, ios_base & b,
703 ios_base::iostate & err, double & v) const
705 return do_get_float(iit, eit, b, err, v);
709 do_get(iter_type iit, iter_type eit, ios_base & b,
710 ios_base::iostate & err, long double & v) const
712 return do_get_float(iit, eit, b, err, v);
716 do_get(iter_type iit, iter_type eit, ios_base & b,
717 ios_base::iostate & err, void * & v) const
720 iter_type end = do_get_integer(iit, eit, b, err, val);
721 if (!(err & ios_base::failbit))
722 v = reinterpret_cast<void *>(val);
727 template <typename ValueType>
729 do_get_integer(iter_type iit, iter_type eit, ios_base & b,
730 ios_base::iostate & err, ValueType & v) const
734 for (; iit != eit && isNumpunct(*iit); ++iit)
735 s += static_cast<char>(*iit);
736 // We add another character, not part of the numpunct facet,
737 // in order to avoid setting the eofbit in the stream state,
738 // which would prevent any further read. The space seems a
741 string_num_get_facet f;
742 f.get(s.begin(), s.end(), b, err, v);
744 err |= ios_base::eofbit;
749 bool isNumpunct(lyx::char_type const c) const
751 /// Only account for the standard numpunct "C" locale facet.
752 return c == '-' || c == '+'
753 || c == 'x' || c == 'X'
757 template <typename ValueType>
759 do_get_float(iter_type iit, iter_type eit, ios_base & b,
760 ios_base::iostate & err, ValueType & v) const
762 // Gather a string of the form
763 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
767 char const dot = p.decimal_point();
768 char const sep = p.thousands_sep();
769 // Get an optional sign
770 if (iit != eit && (*iit == '-' || *iit == '+')) {
771 s += static_cast<char>(*iit);
774 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
775 s += static_cast<char>(*iit);
776 if (iit != eit && *iit == dot) {
779 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
780 s += static_cast<char>(*iit);
781 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
782 s += static_cast<char>(*iit);
784 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
785 s += static_cast<char>(*iit);
789 string_num_get_facet f;
790 f.get(s.begin(), s.end(), b, err, v);
792 err |= ios_base::eofbit;
797 bool isDigitOrSep(lyx::char_type const c, char const sep) const
799 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
804 /// class to add our facets to the global locale
805 class locale_initializer {
810 locale const loc1(global, new ascii_ctype_facet);
811 locale const loc2(loc1, new ascii_num_put_facet);
812 locale const loc3(loc2, new ascii_num_get_facet);
813 locale::global(loc3);
820 /// make sure that our facets get used
821 static locale_initializer initializer;