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"
23 #include <boost/assert.hpp>
29 docstring const from_ascii(char const * ascii)
32 for (char const * c = ascii; *c; ++c) {
33 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
40 docstring const from_ascii(std::string const & ascii)
42 int const len = ascii.length();
43 for (int i = 0; i < len; ++i)
44 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
45 return docstring(ascii.begin(), ascii.end());
49 std::string const to_ascii(docstring const & ucs4)
51 int const len = ucs4.length();
54 for (int i = 0; i < len; ++i) {
55 BOOST_ASSERT(ucs4[i] < 0x80);
56 ascii[i] = static_cast<char>(ucs4[i]);
62 IconvProcessor & utf8ToUcs4()
64 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
70 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
72 size_t n = utf8.size();
73 // as utf8 is a multi-byte encoding, there would be at most
79 int maxoutsize = n * 4;
80 // basic_string::data() is not recognized by some old gcc version
81 // so we use &(ucs4[0]) instead.
82 char * outbuf = (char *)(&(ucs4[0]));
83 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
85 // adjust to the real converted size
90 docstring const from_utf8(std::string const & utf8)
93 utf8_to_ucs4(utf8, ucs4);
98 std::string const to_utf8(docstring const & ucs4)
100 std::vector<char> const utf8 =
101 ucs4_to_utf8(ucs4.data(), ucs4.size());
102 return std::string(utf8.begin(), utf8.end());
106 docstring const from_local8bit(std::string const & s)
108 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
112 /// Exception thrown by to_local8bit if the string could not be converted
113 class to_local8bit_failure : public std::bad_cast {
115 to_local8bit_failure() throw() : std::bad_cast() {}
116 virtual ~to_local8bit_failure() throw() {}
117 virtual const char* what() const throw()
119 return "A string could not be converted from unicode to the local 8 bit encoding.";
124 std::string const to_local8bit(docstring const & s)
126 // This conversion can fail, depending on input.
128 return std::string();
129 QByteArray const local = toqstr(s).toLocal8Bit();
130 if (local.size() == 0)
131 throw to_local8bit_failure();
132 return std::string(local.begin(), local.end());
136 docstring const from_filesystem8bit(std::string const & s)
138 QByteArray const encoded(s.c_str(), s.length());
139 return qstring_to_ucs4(QFile::decodeName(encoded));
143 std::string const to_filesystem8bit(docstring const & s)
145 QByteArray const encoded = QFile::encodeName(toqstr(s));
146 return std::string(encoded.begin(), encoded.end());
150 docstring const normalize_c(docstring const & s)
152 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
156 bool operator==(lyx::docstring const & l, char const * r)
158 lyx::docstring::const_iterator it = l.begin();
159 lyx::docstring::const_iterator end = l.end();
160 for (; it != end; ++it, ++r) {
161 BOOST_ASSERT(static_cast<unsigned char>(*r) < 0x80);
164 if (*it != static_cast<lyx::docstring::value_type>(*r))
171 lyx::docstring operator+(lyx::docstring const & l, char const * r)
174 for (char const * c = r; *c; ++c) {
175 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
182 lyx::docstring operator+(char const * l, lyx::docstring const & r)
185 for (char const * c = l; *c; ++c) {
186 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
194 lyx::docstring operator+(lyx::docstring const & l, char r)
196 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
198 s += docstring::value_type(r);
203 lyx::docstring operator+(char l, lyx::docstring const & r)
205 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
206 return lyx::docstring::value_type(l) + r;
210 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
212 for (char const * c = r; *c; ++c) {
213 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
220 lyx::docstring & operator+=(lyx::docstring & l, char r)
222 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
229 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
231 // gcc does not have proper locale facets for lyx::char_type if
232 // sizeof(wchar_t) == 2, so we have to implement them on our own.
235 // We get undefined references to these virtual methods. This looks like
236 // a bug in gcc. The implementation here does not do anything useful, since
237 // it is overriden in ascii_ctype_facet.
239 template<> ctype<lyx::char_type>::~ctype() {}
241 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
242 template<> lyx::char_type const *
243 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
244 template<> const lyx::char_type *
245 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
246 template<> const lyx::char_type *
247 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
248 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
249 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
250 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
251 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
252 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
253 template<> const char *
254 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
256 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
257 template<> const lyx::char_type *
258 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
264 class ctype_failure : public std::bad_cast {
266 ctype_failure() throw() : std::bad_cast() {}
267 virtual ~ctype_failure() throw() {}
268 virtual const char* what() const throw()
270 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
275 class num_put_failure : public std::bad_cast {
277 num_put_failure() throw() : std::bad_cast() {}
278 virtual ~num_put_failure() throw() {}
279 virtual const char* what() const throw()
281 return "The num_put locale facet does only support ASCII characters on this platform.";
286 /// ctype facet for UCS4 characters. The implementation does only support pure
287 /// ASCII, since we do not need anything else for now.
288 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
289 class ascii_ctype_facet : public std::ctype<lyx::char_type>
292 typedef lyx::char_type char_type;
293 typedef wctype_t wmask_type;
294 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
296 M_initialize_ctype();
301 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
303 wmask_type M_wmask[16];
304 wmask_type M_convert_to_wmask(const mask m) const
308 case space: ret = wctype("space"); break;
309 case print: ret = wctype("print"); break;
310 case cntrl: ret = wctype("cntrl"); break;
311 case upper: ret = wctype("upper"); break;
312 case lower: ret = wctype("lower"); break;
313 case alpha: ret = wctype("alpha"); break;
314 case digit: ret = wctype("digit"); break;
315 case punct: ret = wctype("punct"); break;
316 case xdigit: ret = wctype("xdigit"); break;
317 case alnum: ret = wctype("alnum"); break;
318 case graph: ret = wctype("graph"); break;
319 default: ret = wmask_type();
323 void M_initialize_ctype()
326 for (i = 0; i < 128; ++i) {
327 const int c = wctob(i);
331 M_narrow[i] = static_cast<char>(c);
337 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
338 M_widen[i] = btowc(i);
340 for (size_t i = 0; i <= 15; ++i) {
341 M_bit[i] = static_cast<mask>(1 << i);
342 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
345 virtual ~ascii_ctype_facet() {}
346 char_type do_toupper(char_type c) const
349 throw ctype_failure();
350 return toupper(static_cast<int>(c));
352 char_type const * do_toupper(char_type * lo, char_type const * hi) const
356 throw ctype_failure();
357 *lo = toupper(static_cast<int>(*lo));
362 char_type do_tolower(char_type c) const
365 throw ctype_failure();
368 char_type const * do_tolower(char_type * lo, char_type const * hi) const
372 throw ctype_failure();
378 bool do_is(mask m, char_type c) const
381 throw ctype_failure();
382 // The code below works because c is in the ASCII range.
383 // We could not use iswctype() which is designed for a 2byte
384 // whar_t without encoding conversion otherwise.
386 // Generically, 15 (instead of 10) since we don't know the numerical
387 // encoding of the various categories in /usr/include/ctype.h.
388 const size_t bitmasksize = 15;
389 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
390 if (m & M_bit[bitcur] &&
391 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
397 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
399 for (;lo < hi; ++vec, ++lo) {
401 throw ctype_failure();
402 // The code below works because c is in the ASCII range.
403 // We could not use iswctype() which is designed for a 2byte
404 // whar_t without encoding conversion otherwise.
405 // Generically, 15 (instead of 10) since we don't know the numerical
406 // encoding of the various categories in /usr/include/ctype.h.
407 const size_t bitmasksize = 15;
409 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
410 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
416 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
418 while (lo < hi && !this->do_is(m, *lo))
422 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
424 while (lo < hi && this->do_is(m, *lo) != 0)
428 char_type do_widen(char c) const
430 if (static_cast<unsigned char>(c) < 0x80)
432 throw ctype_failure();
434 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
437 if (static_cast<unsigned char>(*lo) >= 0x80)
438 throw ctype_failure();
445 char do_narrow(char_type wc, char) const
448 return static_cast<char>(wc);
449 throw ctype_failure();
451 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
455 *dest = static_cast<char>(*lo);
457 throw ctype_failure();
466 /// Facet for outputting numbers to odocstreams as ascii.
467 /// Here we simply need defining the virtual do_put functions.
468 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
470 typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
472 ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
474 /// Facet for converting numbers to ascii strings.
475 class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
478 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
483 do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
485 return do_put_helper(oit, b, fill, v);
489 do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
491 return do_put_helper(oit, b, fill, v);
495 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
497 return do_put_helper(oit, b, fill, v);
500 #ifdef _GLIBCXX_USE_LONG_LONG
502 do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
504 return do_put_helper(oit, b, fill, v);
508 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
510 return do_put_helper(oit, b, fill, v);
515 do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
517 return do_put_helper(oit, b, fill, v);
521 do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
523 return do_put_helper(oit, b, fill, v);
527 do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
529 return do_put_helper(oit, b, fill, v);
533 template <typename ValueType>
535 do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
538 throw num_put_failure();
540 std::streamsize const sz = b.width() > b.precision() ?
541 b.width() : b.precision();
542 // 64 is large enough, unless width or precision are bigger
543 std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
544 std::string s(wd, '\0');
545 string_num_put_facet f;
546 std::string::const_iterator cit = s.begin();
547 std::string::const_iterator end =
548 f.put(s.begin(), b, fill, v);
549 for (; cit != end; ++cit, ++oit)
557 /// Facet for inputting ascii representations of numbers from idocstreams.
558 /// Here we simply need defining the virtual do_get functions.
559 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
561 typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
563 ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
565 /// Facet for converting ascii representation of numbers to a value.
566 class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
569 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
572 /// Numpunct facet defining the I/O format.
573 class numpunct_facet : public std::numpunct<char>
576 numpunct_facet() : std::numpunct<char>(1) {}
581 do_get(iter_type iit, iter_type eit, std::ios_base & b,
582 std::ios_base::iostate & err, bool & v) const
584 if (b.flags() & std::ios_base::boolalpha) {
586 lyx::docstring const truename = from_local8bit(p.truename());
587 lyx::docstring const falsename = from_local8bit(p.falsename());
592 size_t const tsize = truename.size();
593 size_t const fsize = falsename.size();
594 for (; iit != eit; ++iit) {
597 bool true_ok = lyx::support::prefixIs(truename, s);
598 bool false_ok = lyx::support::prefixIs(falsename, s);
599 if (!true_ok && !false_ok) {
604 if ((true_ok && n == tsize) ||
605 (false_ok && n == fsize)) {
611 err = std::ios_base::goodbit;
612 v = truename == s ? true : false;
614 err = std::ios_base::failbit;
616 err |= std::ios_base::eofbit;
620 iter_type end = this->do_get(iit, eit, b, err, l);
621 if (!(err & std::ios_base::failbit)) {
627 err |= std::ios_base::failbit;
634 do_get(iter_type iit, iter_type eit, std::ios_base & b,
635 std::ios_base::iostate & err, long & v) const
637 return do_get_integer(iit, eit, b, err, v);
641 do_get(iter_type iit, iter_type eit, std::ios_base & b,
642 std::ios_base::iostate & err, unsigned short & v) const
644 return do_get_integer(iit, eit, b, err, v);
648 do_get(iter_type iit, iter_type eit, std::ios_base & b,
649 std::ios_base::iostate & err, unsigned int & v) const
651 return do_get_integer(iit, eit, b, err, v);
655 do_get(iter_type iit, iter_type eit, std::ios_base & b,
656 std::ios_base::iostate & err, unsigned long & v) const
658 return do_get_integer(iit, eit, b, err, v);
661 #ifdef _GLIBCXX_USE_LONG_LONG
663 do_get(iter_type iit, iter_type eit, std::ios_base & b,
664 std::ios_base::iostate & err, long long & v) const
666 return do_get_integer(iit, eit, b, err, v);
670 do_get(iter_type iit, iter_type eit, std::ios_base & b,
671 std::ios_base::iostate & err, unsigned long long & v) const
673 return do_get_integer(iit, eit, b, err, v);
678 do_get(iter_type iit, iter_type eit, std::ios_base & b,
679 std::ios_base::iostate & err, float & v) const
681 return do_get_float(iit, eit, b, err, v);
685 do_get(iter_type iit, iter_type eit, std::ios_base & b,
686 std::ios_base::iostate & err, double & v) const
688 return do_get_float(iit, eit, b, err, v);
692 do_get(iter_type iit, iter_type eit, std::ios_base & b,
693 std::ios_base::iostate & err, long double & v) const
695 return do_get_float(iit, eit, b, err, v);
699 do_get(iter_type iit, iter_type eit, std::ios_base & b,
700 std::ios_base::iostate & err, void * & v) const
703 iter_type end = do_get_integer(iit, eit, b, err, val);
704 if (!(err & std::ios_base::failbit))
705 v = reinterpret_cast<void *>(val);
710 template <typename ValueType>
712 do_get_integer(iter_type iit, iter_type eit, std::ios_base & b,
713 std::ios_base::iostate & err, ValueType & v) const
717 for (; iit != eit && isNumpunct(*iit); ++iit)
718 s += static_cast<char>(*iit);
719 // We add another character, not part of the numpunct facet,
720 // in order to avoid setting the eofbit in the stream state,
721 // which would prevent any further read. The space seems a
724 string_num_get_facet f;
725 f.get(s.begin(), s.end(), b, err, v);
727 err |= std::ios_base::eofbit;
732 bool isNumpunct(lyx::char_type const c) const
734 /// Only account for the standard numpunct "C" locale facet.
735 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
736 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
737 || c == 'x' || c == 'X');
740 template <typename ValueType>
742 do_get_float(iter_type iit, iter_type eit, std::ios_base & b,
743 std::ios_base::iostate & err, ValueType & v) const
745 // Gather a string of the form
746 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
751 char const dot = p.decimal_point();
752 char const sep = p.thousands_sep();
753 // Get an optional sign
754 if (iit != eit && (*iit == '-' || *iit == '+')) {
755 s += static_cast<char>(*iit);
758 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
759 s += static_cast<char>(*iit);
760 if (iit != eit && *iit == dot) {
763 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
764 s += static_cast<char>(*iit);
765 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
766 s += static_cast<char>(*iit);
768 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
769 s += static_cast<char>(*iit);
773 string_num_get_facet f;
774 f.get(s.begin(), s.end(), b, err, v);
776 err |= std::ios_base::eofbit;
781 bool isDigitOrSep(lyx::char_type const c, char const sep) const
783 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
788 /// class to add our facets to the global locale
789 class locale_initializer {
794 std::locale const loc1(global, new ascii_ctype_facet);
795 std::locale const loc2(loc1, new ascii_num_put_facet);
796 std::locale const loc3(loc2, new ascii_num_get_facet);
797 std::locale::global(loc3);
804 /// make sure that our facets get used
805 static locale_initializer initializer;