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"
28 using lyx::support::isHexChar;
32 docstring const from_ascii(char const * ascii)
35 if (int n = strlen(ascii)) {
40 LASSERT(static_cast<unsigned char>(ascii[n]) < 0x80, /**/);
47 docstring const from_ascii(string const & ascii)
49 int const len = ascii.length();
50 for (int i = 0; i < len; ++i)
51 LASSERT(static_cast<unsigned char>(ascii[i]) < 0x80, /**/);
52 return docstring(ascii.begin(), ascii.end());
56 string const to_ascii(docstring const & ucs4)
58 int const len = ucs4.length();
61 for (int i = 0; i < len; ++i) {
62 LASSERT(ucs4[i] < 0x80, /**/);
63 ascii[i] = static_cast<char>(ucs4[i]);
69 IconvProcessor & utf8ToUcs4()
71 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
77 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
79 size_t n = utf8.size();
80 // as utf8 is a multi-byte encoding, there would be at most
86 int maxoutsize = n * 4;
87 // basic_string::data() is not recognized by some old gcc version
88 // so we use &(ucs4[0]) instead.
89 char * outbuf = (char *)(&(ucs4[0]));
90 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
92 // adjust to the real converted size
97 docstring const from_utf8(string const & utf8)
100 utf8_to_ucs4(utf8, ucs4);
105 string const to_utf8(docstring const & ucs4)
107 vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
108 return string(utf8.begin(), utf8.end());
112 docstring const from_local8bit(string const & s)
114 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
118 /// Exception thrown by to_local8bit if the string could not be converted
119 class to_local8bit_failure : public bad_cast {
121 to_local8bit_failure() throw() : bad_cast() {}
122 virtual ~to_local8bit_failure() throw() {}
123 virtual const char* what() const throw()
125 return "A string could not be converted from unicode to the local 8 bit encoding.";
130 string const to_local8bit(docstring const & s)
132 // This conversion can fail, depending on input.
135 QByteArray const local = toqstr(s).toLocal8Bit();
136 if (local.size() == 0)
137 throw to_local8bit_failure();
138 return string(local.begin(), local.end());
142 docstring const from_filesystem8bit(string const & s)
144 QByteArray const encoded(s.c_str(), s.length());
145 return qstring_to_ucs4(QFile::decodeName(encoded));
149 string const to_filesystem8bit(docstring const & s)
151 QByteArray const encoded = QFile::encodeName(toqstr(s));
152 return string(encoded.begin(), encoded.end());
156 string const to_iconv_encoding(docstring const & s, string const & encoding)
158 std::vector<char> const encoded =
159 ucs4_to_eightbit(s.data(), s.length(), encoding);
160 return string(encoded.begin(), encoded.end());
164 docstring const from_iconv_encoding(string const & s, string const & encoding)
166 std::vector<char_type> const ucs4 =
167 eightbit_to_ucs4(s.data(), s.length(), encoding);
168 return docstring(ucs4.begin(), ucs4.end());
172 docstring const normalize_c(docstring const & s)
174 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
178 bool operator==(lyx::docstring const & l, char const * r)
180 lyx::docstring::const_iterator it = l.begin();
181 lyx::docstring::const_iterator end = l.end();
182 for (; it != end; ++it, ++r) {
183 LASSERT(static_cast<unsigned char>(*r) < 0x80, /**/);
186 if (*it != static_cast<lyx::docstring::value_type>(*r))
193 lyx::docstring operator+(lyx::docstring const & l, char const * r)
196 for (char const * c = r; *c; ++c) {
197 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
204 lyx::docstring operator+(char const * l, lyx::docstring const & r)
207 for (char const * c = l; *c; ++c) {
208 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
216 lyx::docstring operator+(lyx::docstring const & l, char r)
218 LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
220 s += docstring::value_type(r);
225 lyx::docstring operator+(char l, lyx::docstring const & r)
227 LASSERT(static_cast<unsigned char>(l) < 0x80, /**/);
228 return lyx::docstring::value_type(l) + r;
232 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
234 for (char const * c = r; *c; ++c) {
235 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
242 lyx::docstring & operator+=(lyx::docstring & l, char r)
244 LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
251 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
253 // gcc does not have proper locale facets for lyx::char_type if
254 // sizeof(wchar_t) == 2, so we have to implement them on our own.
257 // We get undefined references to these virtual methods. This looks like
258 // a bug in gcc. The implementation here does not do anything useful, since
259 // it is overriden in ascii_ctype_facet.
261 template<> ctype<lyx::char_type>::~ctype() {}
263 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
264 template<> lyx::char_type const *
265 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
266 template<> const lyx::char_type *
267 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
268 template<> const lyx::char_type *
269 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
270 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
271 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
272 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
273 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
274 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
275 template<> const char *
276 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
278 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
279 template<> const lyx::char_type *
280 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
286 class ctype_failure : public bad_cast {
288 ctype_failure() throw() : bad_cast() {}
289 virtual ~ctype_failure() throw() {}
290 virtual const char* what() const throw()
292 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
297 class num_put_failure : public bad_cast {
299 num_put_failure() throw() : bad_cast() {}
300 virtual ~num_put_failure() throw() {}
301 virtual const char* what() const throw()
303 return "The num_put locale facet does only support ASCII characters on this platform.";
308 /// ctype facet for UCS4 characters. The implementation does only support pure
309 /// ASCII, since we do not need anything else for now.
310 /// The code is partly stolen from ctype<wchar_t> from gcc.
311 class ascii_ctype_facet : public ctype<lyx::char_type>
314 typedef lyx::char_type char_type;
315 typedef wctype_t wmask_type;
316 explicit ascii_ctype_facet(size_t refs = 0) : ctype<char_type>(refs)
318 M_initialize_ctype();
323 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
325 wmask_type M_wmask[16];
326 wmask_type M_convert_to_wmask(const mask m) const
330 case space: ret = wctype("space"); break;
331 case print: ret = wctype("print"); break;
332 case cntrl: ret = wctype("cntrl"); break;
333 case upper: ret = wctype("upper"); break;
334 case lower: ret = wctype("lower"); break;
335 case alpha: ret = wctype("alpha"); break;
336 case digit: ret = wctype("digit"); break;
337 case punct: ret = wctype("punct"); break;
338 case xdigit: ret = wctype("xdigit"); break;
339 case alnum: ret = wctype("alnum"); break;
340 case graph: ret = wctype("graph"); break;
341 default: ret = wmask_type();
345 void M_initialize_ctype()
348 for (i = 0; i < 128; ++i) {
349 const int c = wctob(i);
353 M_narrow[i] = static_cast<char>(c);
359 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
360 M_widen[i] = btowc(i);
362 for (size_t i = 0; i <= 15; ++i) {
363 M_bit[i] = static_cast<mask>(1 << i);
364 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
367 virtual ~ascii_ctype_facet() {}
368 char_type do_toupper(char_type c) const
371 throw ctype_failure();
372 return toupper(static_cast<int>(c));
374 char_type const * do_toupper(char_type * lo, char_type const * hi) const
378 throw ctype_failure();
379 *lo = toupper(static_cast<int>(*lo));
384 char_type do_tolower(char_type c) const
387 throw ctype_failure();
390 char_type const * do_tolower(char_type * lo, char_type const * hi) const
394 throw ctype_failure();
400 bool do_is(mask m, char_type c) const
403 throw ctype_failure();
404 // The code below works because c is in the ASCII range.
405 // We could not use iswctype() which is designed for a 2byte
406 // whar_t without encoding conversion otherwise.
408 // Generically, 15 (instead of 10) since we don't know the numerical
409 // encoding of the various categories in /usr/include/ctype.h.
410 const size_t bitmasksize = 15;
411 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
412 if (m & M_bit[bitcur] &&
413 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
419 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
421 for (;lo < hi; ++vec, ++lo) {
423 throw ctype_failure();
424 // The code below works because c is in the ASCII range.
425 // We could not use iswctype() which is designed for a 2byte
426 // whar_t without encoding conversion otherwise.
427 // Generically, 15 (instead of 10) since we don't know the numerical
428 // encoding of the various categories in /usr/include/ctype.h.
429 const size_t bitmasksize = 15;
431 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
432 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
438 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
440 while (lo < hi && !this->do_is(m, *lo))
444 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
446 while (lo < hi && this->do_is(m, *lo) != 0)
450 char_type do_widen(char c) const
452 if (static_cast<unsigned char>(c) < 0x80)
454 throw ctype_failure();
456 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
459 if (static_cast<unsigned char>(*lo) >= 0x80)
460 throw ctype_failure();
467 char do_narrow(char_type wc, char) const
470 return static_cast<char>(wc);
471 throw ctype_failure();
473 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
477 *dest = static_cast<char>(*lo);
479 throw ctype_failure();
488 /// Facet for outputting numbers to odocstreams as ascii.
489 /// Here we simply need defining the virtual do_put functions.
490 class ascii_num_put_facet : public num_put<lyx::char_type, ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
492 typedef ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
494 ascii_num_put_facet(size_t refs = 0) : num_put<lyx::char_type, iter_type>(refs) {}
496 /// Facet for converting numbers to ascii strings.
497 class string_num_put_facet : public num_put<char, basic_string<char>::iterator>
500 string_num_put_facet() : num_put<char, basic_string<char>::iterator>(1) {}
505 do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
507 return do_put_helper(oit, b, fill, v);
511 do_put(iter_type oit, ios_base & b, char_type fill, long v) const
513 return do_put_helper(oit, b, fill, v);
517 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
519 return do_put_helper(oit, b, fill, v);
522 #ifdef _GLIBCXX_USE_LONG_LONG
524 do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
526 return do_put_helper(oit, b, fill, v);
530 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
532 return do_put_helper(oit, b, fill, v);
537 do_put(iter_type oit, ios_base & b, char_type fill, double v) const
539 return do_put_helper(oit, b, fill, v);
543 do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
545 return do_put_helper(oit, b, fill, v);
549 do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
551 return do_put_helper(oit, b, fill, v);
555 template <typename ValueType>
557 do_put_helper(iter_type oit, ios_base & b, char_type fill, ValueType v) const
560 throw num_put_failure();
562 streamsize const sz = b.width() > b.precision() ?
563 b.width() : b.precision();
564 // 64 is large enough, unless width or precision are bigger
565 streamsize const wd = (sz > 56 ? sz : 56) + 8;
567 string_num_put_facet f;
568 string::const_iterator cit = s.begin();
569 string::const_iterator end =
570 f.put(s.begin(), b, fill, v);
571 for (; cit != end; ++cit, ++oit)
579 /// Facet for inputting ascii representations of numbers from idocstreams.
580 /// Here we simply need defining the virtual do_get functions.
581 class ascii_num_get_facet : public num_get<lyx::char_type, istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
583 typedef istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
585 ascii_num_get_facet(size_t refs = 0) : num_get<lyx::char_type, iter_type>(refs) {}
587 /// Facet for converting ascii representation of numbers to a value.
588 class string_num_get_facet : public num_get<char, basic_string<char>::iterator>
591 string_num_get_facet() : num_get<char, basic_string<char>::iterator>(1) {}
594 /// Numpunct facet defining the I/O format.
595 class numpunct_facet : public numpunct<char>
598 numpunct_facet() : numpunct<char>(1) {}
603 do_get(iter_type iit, iter_type eit, ios_base & b,
604 ios_base::iostate & err, bool & v) const
606 if (b.flags() & ios_base::boolalpha) {
608 lyx::docstring const truename = from_local8bit(p.truename());
609 lyx::docstring const falsename = from_local8bit(p.falsename());
614 size_t const tsize = truename.size();
615 size_t const fsize = falsename.size();
616 for (; iit != eit; ++iit) {
619 bool true_ok = support::prefixIs(truename, s);
620 bool false_ok = support::prefixIs(falsename, s);
621 if (!true_ok && !false_ok) {
626 if ((true_ok && n == tsize) ||
627 (false_ok && n == fsize)) {
633 err = ios_base::goodbit;
634 v = truename == s ? true : false;
636 err = ios_base::failbit;
638 err |= ios_base::eofbit;
642 iter_type end = this->do_get(iit, eit, b, err, l);
643 if (!(err & ios_base::failbit)) {
649 err |= ios_base::failbit;
656 do_get(iter_type iit, iter_type eit, ios_base & b,
657 ios_base::iostate & err, long & v) const
659 return do_get_integer(iit, eit, b, err, v);
663 do_get(iter_type iit, iter_type eit, ios_base & b,
664 ios_base::iostate & err, unsigned short & v) const
666 return do_get_integer(iit, eit, b, err, v);
670 do_get(iter_type iit, iter_type eit, ios_base & b,
671 ios_base::iostate & err, unsigned int & v) const
673 return do_get_integer(iit, eit, b, err, v);
677 do_get(iter_type iit, iter_type eit, ios_base & b,
678 ios_base::iostate & err, unsigned long & v) const
680 return do_get_integer(iit, eit, b, err, v);
683 #ifdef _GLIBCXX_USE_LONG_LONG
685 do_get(iter_type iit, iter_type eit, ios_base & b,
686 ios_base::iostate & err, long long & v) const
688 return do_get_integer(iit, eit, b, err, v);
692 do_get(iter_type iit, iter_type eit, ios_base & b,
693 ios_base::iostate & err, unsigned long long & v) const
695 return do_get_integer(iit, eit, b, err, v);
700 do_get(iter_type iit, iter_type eit, ios_base & b,
701 ios_base::iostate & err, float & v) const
703 return do_get_float(iit, eit, b, err, v);
707 do_get(iter_type iit, iter_type eit, ios_base & b,
708 ios_base::iostate & err, double & v) const
710 return do_get_float(iit, eit, b, err, v);
714 do_get(iter_type iit, iter_type eit, ios_base & b,
715 ios_base::iostate & err, long double & v) const
717 return do_get_float(iit, eit, b, err, v);
721 do_get(iter_type iit, iter_type eit, ios_base & b,
722 ios_base::iostate & err, void * & v) const
725 iter_type end = do_get_integer(iit, eit, b, err, val);
726 if (!(err & ios_base::failbit))
727 v = reinterpret_cast<void *>(val);
732 template <typename ValueType>
734 do_get_integer(iter_type iit, iter_type eit, ios_base & b,
735 ios_base::iostate & err, ValueType & v) const
739 for (; iit != eit && isNumpunct(*iit); ++iit)
740 s += static_cast<char>(*iit);
741 // We add another character, not part of the numpunct facet,
742 // in order to avoid setting the eofbit in the stream state,
743 // which would prevent any further read. The space seems a
746 string_num_get_facet f;
747 f.get(s.begin(), s.end(), b, err, v);
749 err |= ios_base::eofbit;
754 bool isNumpunct(lyx::char_type const c) const
756 /// Only account for the standard numpunct "C" locale facet.
757 return c == '-' || c == '+'
758 || c == 'x' || c == 'X'
762 template <typename ValueType>
764 do_get_float(iter_type iit, iter_type eit, ios_base & b,
765 ios_base::iostate & err, ValueType & v) const
767 // Gather a string of the form
768 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
773 char const dot = p.decimal_point();
774 char const sep = p.thousands_sep();
775 // Get an optional sign
776 if (iit != eit && (*iit == '-' || *iit == '+')) {
777 s += static_cast<char>(*iit);
780 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
781 s += static_cast<char>(*iit);
782 if (iit != eit && *iit == dot) {
785 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
786 s += static_cast<char>(*iit);
787 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
788 s += static_cast<char>(*iit);
790 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
791 s += static_cast<char>(*iit);
795 string_num_get_facet f;
796 f.get(s.begin(), s.end(), b, err, v);
798 err |= ios_base::eofbit;
803 bool isDigitOrSep(lyx::char_type const c, char const sep) const
805 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
810 /// class to add our facets to the global locale
811 class locale_initializer {
816 locale const loc1(global, new ascii_ctype_facet);
817 locale const loc2(loc1, new ascii_num_put_facet);
818 locale const loc3(loc2, new ascii_num_get_facet);
819 locale::global(loc3);
826 /// make sure that our facets get used
827 static locale_initializer initializer;