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/lstrings.h"
16 #include "support/qstring_helpers.h"
17 #include "support/unicode.h"
24 #include <boost/assert.hpp>
30 docstring const from_ascii(char const * ascii)
33 for (char const * c = ascii; *c; ++c) {
34 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
41 docstring const from_ascii(std::string const & ascii)
43 int const len = ascii.length();
44 for (int i = 0; i < len; ++i)
45 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
46 return docstring(ascii.begin(), ascii.end());
50 std::string const to_ascii(docstring const & ucs4)
52 int const len = ucs4.length();
55 for (int i = 0; i < len; ++i) {
56 BOOST_ASSERT(ucs4[i] < 0x80);
57 ascii[i] = static_cast<char>(ucs4[i]);
63 IconvProcessor & utf8ToUcs4()
65 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
71 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
73 size_t n = utf8.size();
74 // as utf8 is a multi-byte encoding, there would be at most
80 int maxoutsize = n * 4;
81 // basic_string::data() is not recognized by some old gcc version
82 // so we use &(ucs4[0]) instead.
83 char * outbuf = (char *)(&(ucs4[0]));
84 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
86 // adjust to the real converted size
91 docstring const from_utf8(std::string const & utf8)
94 utf8_to_ucs4(utf8, ucs4);
99 std::string const to_utf8(docstring const & ucs4)
101 std::vector<char> const utf8 =
102 ucs4_to_utf8(ucs4.data(), ucs4.size());
103 return std::string(utf8.begin(), utf8.end());
107 docstring const from_local8bit(std::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 std::bad_cast {
116 to_local8bit_failure() throw() : std::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 std::string const to_local8bit(docstring const & s)
127 // This conversion can fail, depending on input.
129 return std::string();
130 QByteArray const local = toqstr(s).toLocal8Bit();
131 if (local.size() == 0)
132 throw to_local8bit_failure();
133 return std::string(local.begin(), local.end());
137 docstring const from_filesystem8bit(std::string const & s)
139 QByteArray const encoded(s.c_str(), s.length());
140 return qstring_to_ucs4(QFile::decodeName(encoded));
144 std::string const to_filesystem8bit(docstring const & s)
146 QByteArray const encoded = QFile::encodeName(toqstr(s));
147 return std::string(encoded.begin(), encoded.end());
151 docstring const normalize_c(docstring const & s)
153 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
157 bool operator==(lyx::docstring const & l, char const * r)
159 lyx::docstring::const_iterator it = l.begin();
160 lyx::docstring::const_iterator end = l.end();
161 for (; it != end; ++it, ++r) {
162 BOOST_ASSERT(static_cast<unsigned char>(*r) < 0x80);
165 if (*it != static_cast<lyx::docstring::value_type>(*r))
172 lyx::docstring operator+(lyx::docstring const & l, char const * r)
175 for (char const * c = r; *c; ++c) {
176 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
183 lyx::docstring operator+(char const * l, lyx::docstring const & r)
186 for (char const * c = l; *c; ++c) {
187 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
195 lyx::docstring operator+(lyx::docstring const & l, char r)
197 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
199 s += docstring::value_type(r);
204 lyx::docstring operator+(char l, lyx::docstring const & r)
206 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
207 return lyx::docstring::value_type(l) + r;
211 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
213 for (char const * c = r; *c; ++c) {
214 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
221 lyx::docstring & operator+=(lyx::docstring & l, char r)
223 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
230 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
232 // gcc does not have proper locale facets for lyx::char_type if
233 // sizeof(wchar_t) == 2, so we have to implement them on our own.
236 // We get undefined references to these virtual methods. This looks like
237 // a bug in gcc. The implementation here does not do anything useful, since
238 // it is overriden in ascii_ctype_facet.
240 template<> ctype<lyx::char_type>::~ctype() {}
242 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
243 template<> lyx::char_type const *
244 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
245 template<> const lyx::char_type *
246 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
247 template<> const lyx::char_type *
248 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
249 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
250 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
251 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
252 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
253 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
254 template<> const char *
255 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
257 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
258 template<> const lyx::char_type *
259 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
265 class ctype_failure : public std::bad_cast {
267 ctype_failure() throw() : std::bad_cast() {}
268 virtual ~ctype_failure() throw() {}
269 virtual const char* what() const throw()
271 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
276 class num_put_failure : public std::bad_cast {
278 num_put_failure() throw() : std::bad_cast() {}
279 virtual ~num_put_failure() throw() {}
280 virtual const char* what() const throw()
282 return "The num_put locale facet does only support ASCII characters on this platform.";
287 /// ctype facet for UCS4 characters. The implementation does only support pure
288 /// ASCII, since we do not need anything else for now.
289 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
290 class ascii_ctype_facet : public std::ctype<lyx::char_type>
293 typedef lyx::char_type char_type;
294 typedef wctype_t wmask_type;
295 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
297 M_initialize_ctype();
302 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
304 wmask_type M_wmask[16];
305 wmask_type M_convert_to_wmask(const mask m) const
309 case space: ret = wctype("space"); break;
310 case print: ret = wctype("print"); break;
311 case cntrl: ret = wctype("cntrl"); break;
312 case upper: ret = wctype("upper"); break;
313 case lower: ret = wctype("lower"); break;
314 case alpha: ret = wctype("alpha"); break;
315 case digit: ret = wctype("digit"); break;
316 case punct: ret = wctype("punct"); break;
317 case xdigit: ret = wctype("xdigit"); break;
318 case alnum: ret = wctype("alnum"); break;
319 case graph: ret = wctype("graph"); break;
320 default: ret = wmask_type();
324 void M_initialize_ctype()
327 for (i = 0; i < 128; ++i) {
328 const int c = wctob(i);
332 M_narrow[i] = static_cast<char>(c);
338 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
339 M_widen[i] = btowc(i);
341 for (size_t i = 0; i <= 15; ++i) {
342 M_bit[i] = static_cast<mask>(1 << i);
343 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
346 virtual ~ascii_ctype_facet() {}
347 char_type do_toupper(char_type c) const
350 throw ctype_failure();
351 return toupper(static_cast<int>(c));
353 char_type const * do_toupper(char_type * lo, char_type const * hi) const
357 throw ctype_failure();
358 *lo = toupper(static_cast<int>(*lo));
363 char_type do_tolower(char_type c) const
366 throw ctype_failure();
369 char_type const * do_tolower(char_type * lo, char_type const * hi) const
373 throw ctype_failure();
379 bool do_is(mask m, char_type c) const
382 throw ctype_failure();
383 // The code below works because c is in the ASCII range.
384 // We could not use iswctype() which is designed for a 2byte
385 // whar_t without encoding conversion otherwise.
387 // Generically, 15 (instead of 10) since we don't know the numerical
388 // encoding of the various categories in /usr/include/ctype.h.
389 const size_t bitmasksize = 15;
390 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
391 if (m & M_bit[bitcur] &&
392 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
398 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
400 for (;lo < hi; ++vec, ++lo) {
402 throw ctype_failure();
403 // The code below works because c is in the ASCII range.
404 // We could not use iswctype() which is designed for a 2byte
405 // whar_t without encoding conversion otherwise.
406 // Generically, 15 (instead of 10) since we don't know the numerical
407 // encoding of the various categories in /usr/include/ctype.h.
408 const size_t bitmasksize = 15;
410 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
411 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
417 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
419 while (lo < hi && !this->do_is(m, *lo))
423 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
425 while (lo < hi && this->do_is(m, *lo) != 0)
429 char_type do_widen(char c) const
431 if (static_cast<unsigned char>(c) < 0x80)
433 throw ctype_failure();
435 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
438 if (static_cast<unsigned char>(*lo) >= 0x80)
439 throw ctype_failure();
446 char do_narrow(char_type wc, char) const
449 return static_cast<char>(wc);
450 throw ctype_failure();
452 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
456 *dest = static_cast<char>(*lo);
458 throw ctype_failure();
467 /// Facet for outputting numbers to odocstreams as ascii.
468 /// Here we simply need defining the virtual do_put functions.
469 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
471 typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
473 ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
475 /// Facet for converting numbers to ascii strings.
476 class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
479 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
484 do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
486 return do_put_helper(oit, b, fill, v);
490 do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
492 return do_put_helper(oit, b, fill, v);
496 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
498 return do_put_helper(oit, b, fill, v);
501 #ifdef _GLIBCXX_USE_LONG_LONG
503 do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
505 return do_put_helper(oit, b, fill, v);
509 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
511 return do_put_helper(oit, b, fill, v);
516 do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
518 return do_put_helper(oit, b, fill, v);
522 do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
524 return do_put_helper(oit, b, fill, v);
528 do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
530 return do_put_helper(oit, b, fill, v);
534 template <typename ValueType>
536 do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
539 throw num_put_failure();
541 std::streamsize const sz = b.width() > b.precision() ?
542 b.width() : b.precision();
543 // 64 is large enough, unless width or precision are bigger
544 std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
545 std::string s(wd, '\0');
546 string_num_put_facet f;
547 std::string::const_iterator cit = s.begin();
548 std::string::const_iterator end =
549 f.put(s.begin(), b, fill, v);
550 for (; cit != end; ++cit, ++oit)
558 /// Facet for inputting ascii representations of numbers from idocstreams.
559 /// Here we simply need defining the virtual do_get functions.
560 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
562 typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
564 ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
566 /// Facet for converting ascii representation of numbers to a value.
567 class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
570 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
573 /// Numpunct facet defining the I/O format.
574 class numpunct_facet : public std::numpunct<char>
577 numpunct_facet() : std::numpunct<char>(1) {}
582 do_get(iter_type iit, iter_type eit, std::ios_base & b,
583 std::ios_base::iostate & err, bool & v) const
585 if (b.flags() & std::ios_base::boolalpha) {
587 lyx::docstring const truename = from_local8bit(p.truename());
588 lyx::docstring const falsename = from_local8bit(p.falsename());
593 size_t const tsize = truename.size();
594 size_t const fsize = falsename.size();
595 for (; iit != eit; ++iit) {
598 bool true_ok = lyx::support::prefixIs(truename, s);
599 bool false_ok = lyx::support::prefixIs(falsename, s);
600 if (!true_ok && !false_ok) {
605 if ((true_ok && n == tsize) ||
606 (false_ok && n == fsize)) {
612 err = std::ios_base::goodbit;
613 v = truename == s ? true : false;
615 err = std::ios_base::failbit;
617 err |= std::ios_base::eofbit;
621 iter_type end = this->do_get(iit, eit, b, err, l);
622 if (!(err & std::ios_base::failbit)) {
628 err |= std::ios_base::failbit;
635 do_get(iter_type iit, iter_type eit, std::ios_base & b,
636 std::ios_base::iostate & err, long & v) const
638 return do_get_integer(iit, eit, b, err, v);
642 do_get(iter_type iit, iter_type eit, std::ios_base & b,
643 std::ios_base::iostate & err, unsigned short & v) const
645 return do_get_integer(iit, eit, b, err, v);
649 do_get(iter_type iit, iter_type eit, std::ios_base & b,
650 std::ios_base::iostate & err, unsigned int & v) const
652 return do_get_integer(iit, eit, b, err, v);
656 do_get(iter_type iit, iter_type eit, std::ios_base & b,
657 std::ios_base::iostate & err, unsigned long & v) const
659 return do_get_integer(iit, eit, b, err, v);
662 #ifdef _GLIBCXX_USE_LONG_LONG
664 do_get(iter_type iit, iter_type eit, std::ios_base & b,
665 std::ios_base::iostate & err, long long & v) const
667 return do_get_integer(iit, eit, b, err, v);
671 do_get(iter_type iit, iter_type eit, std::ios_base & b,
672 std::ios_base::iostate & err, unsigned long long & v) const
674 return do_get_integer(iit, eit, b, err, v);
679 do_get(iter_type iit, iter_type eit, std::ios_base & b,
680 std::ios_base::iostate & err, float & v) const
682 return do_get_float(iit, eit, b, err, v);
686 do_get(iter_type iit, iter_type eit, std::ios_base & b,
687 std::ios_base::iostate & err, double & v) const
689 return do_get_float(iit, eit, b, err, v);
693 do_get(iter_type iit, iter_type eit, std::ios_base & b,
694 std::ios_base::iostate & err, long double & v) const
696 return do_get_float(iit, eit, b, err, v);
700 do_get(iter_type iit, iter_type eit, std::ios_base & b,
701 std::ios_base::iostate & err, void * & v) const
704 iter_type end = do_get_integer(iit, eit, b, err, val);
705 if (!(err & std::ios_base::failbit))
706 v = reinterpret_cast<void *>(val);
711 template <typename ValueType>
713 do_get_integer(iter_type iit, iter_type eit, std::ios_base & b,
714 std::ios_base::iostate & err, ValueType & v) const
718 for (; iit != eit && isNumpunct(*iit); ++iit)
719 s += static_cast<char>(*iit);
720 // We add another character, not part of the numpunct facet,
721 // in order to avoid setting the eofbit in the stream state,
722 // which would prevent any further read. The space seems a
725 string_num_get_facet f;
726 f.get(s.begin(), s.end(), b, err, v);
728 err |= std::ios_base::eofbit;
733 bool isNumpunct(lyx::char_type const c) const
735 /// Only account for the standard numpunct "C" locale facet.
736 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
737 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
738 || c == 'x' || c == 'X');
741 template <typename ValueType>
743 do_get_float(iter_type iit, iter_type eit, std::ios_base & b,
744 std::ios_base::iostate & err, ValueType & v) const
746 // Gather a string of the form
747 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
752 char const dot = p.decimal_point();
753 char const sep = p.thousands_sep();
754 // Get an optional sign
755 if (iit != eit && (*iit == '-' || *iit == '+')) {
756 s += static_cast<char>(*iit);
759 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
760 s += static_cast<char>(*iit);
761 if (iit != eit && *iit == dot) {
764 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
765 s += static_cast<char>(*iit);
766 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
767 s += static_cast<char>(*iit);
769 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
770 s += static_cast<char>(*iit);
774 string_num_get_facet f;
775 f.get(s.begin(), s.end(), b, err, v);
777 err |= std::ios_base::eofbit;
782 bool isDigitOrSep(lyx::char_type const c, char const sep) const
784 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
789 /// class to add our facets to the global locale
790 class locale_initializer {
795 std::locale const loc1(global, new ascii_ctype_facet);
796 std::locale const loc2(loc1, new ascii_num_put_facet);
797 std::locale const loc3(loc2, new ascii_num_get_facet);
798 std::locale::global(loc3);
805 /// make sure that our facets get used
806 static locale_initializer initializer;