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"
22 #include <boost/assert.hpp>
28 docstring const from_ascii(char const * ascii)
31 for (char const * c = ascii; *c; ++c) {
32 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
39 docstring const from_ascii(std::string const & ascii)
41 int const len = ascii.length();
42 for (int i = 0; i < len; ++i)
43 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
44 return docstring(ascii.begin(), ascii.end());
48 std::string const to_ascii(docstring const & ucs4)
50 int const len = ucs4.length();
53 for (int i = 0; i < len; ++i) {
54 BOOST_ASSERT(ucs4[i] < 0x80);
55 ascii[i] = static_cast<char>(ucs4[i]);
61 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
63 size_t n = utf8.size();
64 // as utf8 is a multi-byte encoding, there would be at most
70 int maxoutsize = n * 4;
71 // basic_string::data() is not recognized by some old gcc version
72 // so we use &(ucs4[0]) instead.
73 char * outbuf = (char *)(&(ucs4[0]));
74 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
76 // adjust to the real converted size
81 docstring const from_utf8(std::string const & utf8)
84 utf8_to_ucs4(utf8, ucs4);
89 std::string const to_utf8(docstring const & ucs4)
91 std::vector<char> const utf8 =
92 ucs4_to_utf8(ucs4.data(), ucs4.size());
93 return std::string(utf8.begin(), utf8.end());
97 docstring const from_local8bit(std::string const & s)
99 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
103 const char* to_local8bit_failure::what() const throw()
105 return "A string could not be converted from unicode to the local 8 bit encoding.";
109 std::string const to_local8bit(docstring const & s)
111 // This conversion can fail, depending on input.
113 return std::string();
114 QByteArray const local = toqstr(s).toLocal8Bit();
115 if (local.size() == 0)
116 throw to_local8bit_failure();
117 return std::string(local.begin(), local.end());
121 docstring const from_filesystem8bit(std::string const & s)
123 QByteArray const encoded(s.c_str(), s.length());
124 return qstring_to_ucs4(QFile::decodeName(encoded));
128 std::string const to_filesystem8bit(docstring const & s)
130 QByteArray const encoded = QFile::encodeName(toqstr(s));
131 return std::string(encoded.begin(), encoded.end());
135 bool operator==(lyx::docstring const & l, char const * r)
137 int const len = l.length();
138 for (int i = 0; i < len; ++i) {
139 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
142 if (l[i] != lyx::docstring::value_type(r[i]))
145 return r[len] == '\0';
149 lyx::docstring operator+(lyx::docstring const & l, char const * r)
152 for (char const * c = r; *c; ++c) {
153 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
160 lyx::docstring operator+(char const * l, lyx::docstring const & r)
163 for (char const * c = l; *c; ++c) {
164 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
172 lyx::docstring operator+(lyx::docstring const & l, char r)
174 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
175 return l + lyx::docstring::value_type(r);
179 lyx::docstring operator+(char l, lyx::docstring const & r)
181 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
182 return lyx::docstring::value_type(l) + r;
186 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
188 for (char const * c = r; *c; ++c) {
189 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
196 lyx::docstring & operator+=(lyx::docstring & l, char r)
198 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
205 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
207 // gcc does not have proper locale facets for lyx::char_type if
208 // sizeof(wchar_t) == 2, so we have to implement them on our own.
211 // We get undefined references to these virtual methods. This looks like
212 // a bug in gcc. The implementation here does not do anything useful, since
213 // it is overriden in ascii_ctype_facet.
215 template<> ctype<lyx::char_type>::~ctype() {}
217 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
218 template<> lyx::char_type const *
219 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
220 template<> const lyx::char_type *
221 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
222 template<> const lyx::char_type *
223 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
224 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
225 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
226 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
227 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
228 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
229 template<> const char *
230 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
232 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
233 template<> const lyx::char_type *
234 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
240 class ctype_failure : public std::bad_cast {
242 ctype_failure() throw() : std::bad_cast() {}
243 virtual ~ctype_failure() throw() {}
244 virtual const char* what() const throw()
246 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
251 class num_put_failure : public std::bad_cast {
253 num_put_failure() throw() : std::bad_cast() {}
254 virtual ~num_put_failure() throw() {}
255 virtual const char* what() const throw()
257 return "The num_put locale facet does only support ASCII characters on this platform.";
262 /// ctype facet for UCS4 characters. The implementation does only support pure
263 /// ASCII, since we do not need anything else for now.
264 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
265 class ascii_ctype_facet : public std::ctype<lyx::char_type>
268 typedef lyx::char_type char_type;
269 typedef wctype_t wmask_type;
270 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
272 M_initialize_ctype();
277 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
279 wmask_type M_wmask[16];
280 wmask_type M_convert_to_wmask(const mask m) const
284 case space: ret = wctype("space"); break;
285 case print: ret = wctype("print"); break;
286 case cntrl: ret = wctype("cntrl"); break;
287 case upper: ret = wctype("upper"); break;
288 case lower: ret = wctype("lower"); break;
289 case alpha: ret = wctype("alpha"); break;
290 case digit: ret = wctype("digit"); break;
291 case punct: ret = wctype("punct"); break;
292 case xdigit: ret = wctype("xdigit"); break;
293 case alnum: ret = wctype("alnum"); break;
294 case graph: ret = wctype("graph"); break;
295 default: ret = wmask_type();
299 void M_initialize_ctype()
302 for (i = 0; i < 128; ++i) {
303 const int c = wctob(i);
307 M_narrow[i] = static_cast<char>(c);
313 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
314 M_widen[i] = btowc(i);
316 for (size_t i = 0; i <= 15; ++i) {
317 M_bit[i] = static_cast<mask>(1 << i);
318 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
321 virtual ~ascii_ctype_facet() {}
322 char_type do_toupper(char_type c) const
325 throw ctype_failure();
326 return toupper(static_cast<int>(c));
328 char_type const * do_toupper(char_type * lo, char_type const * hi) const
332 throw ctype_failure();
333 *lo = toupper(static_cast<int>(*lo));
338 char_type do_tolower(char_type c) const
341 throw ctype_failure();
344 char_type const * do_tolower(char_type * lo, char_type const * hi) const
348 throw ctype_failure();
354 bool do_is(mask m, char_type c) const
357 throw ctype_failure();
358 // The code below works because c is in the ASCII range.
359 // We could not use iswctype() which is designed for a 2byte
360 // whar_t without encoding conversion otherwise.
362 // Generically, 15 (instead of 10) since we don't know the numerical
363 // encoding of the various categories in /usr/include/ctype.h.
364 const size_t bitmasksize = 15;
365 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
366 if (m & M_bit[bitcur] &&
367 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
373 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
375 for (;lo < hi; ++vec, ++lo) {
377 throw ctype_failure();
378 // The code below works because c is in the ASCII range.
379 // We could not use iswctype() which is designed for a 2byte
380 // whar_t without encoding conversion otherwise.
381 // Generically, 15 (instead of 10) since we don't know the numerical
382 // encoding of the various categories in /usr/include/ctype.h.
383 const size_t bitmasksize = 15;
385 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
386 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
392 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
394 while (lo < hi && !this->do_is(m, *lo))
398 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
400 while (lo < hi && this->do_is(m, *lo) != 0)
404 char_type do_widen(char c) const
406 if (static_cast<unsigned char>(c) < 0x80)
408 throw ctype_failure();
410 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
413 if (static_cast<unsigned char>(*lo) >= 0x80)
414 throw ctype_failure();
421 char do_narrow(char_type wc, char) const
424 return static_cast<char>(wc);
425 throw ctype_failure();
427 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
431 *dest = static_cast<char>(*lo);
433 throw ctype_failure();
442 /// Facet for outputting numbers to odocstreams as ascii.
443 /// Here we simply need defining the virtual do_put functions.
444 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
446 typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
448 ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
450 /// Facet for converting numbers to ascii strings.
451 class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
454 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
459 do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
461 return do_put_helper(oit, b, fill, v);
465 do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
467 return do_put_helper(oit, b, fill, v);
471 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
473 return do_put_helper(oit, b, fill, v);
476 #ifdef _GLIBCXX_USE_LONG_LONG
478 do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
480 return do_put_helper(oit, b, fill, v);
484 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
486 return do_put_helper(oit, b, fill, v);
491 do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
493 return do_put_helper(oit, b, fill, v);
497 do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
499 return do_put_helper(oit, b, fill, v);
503 do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
505 return do_put_helper(oit, b, fill, v);
509 template <typename ValueType>
511 do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
514 throw num_put_failure();
516 std::streamsize const sz = b.width() > b.precision() ?
517 b.width() : b.precision();
518 // 64 is large enough, unless width or precision are bigger
519 std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
520 std::string s(wd, '\0');
521 string_num_put_facet f;
522 std::string::const_iterator cit = s.begin();
523 std::string::const_iterator end =
524 f.put(s.begin(), b, fill, v);
525 for (; cit != end; ++cit, ++oit)
533 /// Facet for inputting ascii representations of numbers from idocstreams.
534 /// Here we simply need defining the virtual do_get functions.
535 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
537 typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
539 ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
541 /// Facet for converting ascii representation of numbers to a value.
542 class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
545 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
549 bool isNumpunct(lyx::char_type const c) const
551 /// Only account for the standard numpunct "C" locale facet.
552 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
553 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
554 || c == 'x' || c == 'X');
559 do_get(iter_type iit, iter_type eit, std::ios_base & b,
560 std::ios_base::iostate & err, long & v) const
564 for (; iit != eit && isNumpunct(*iit); ++iit)
565 s += static_cast<char>(*iit);
566 // We add another character, not part of the numpunct facet,
567 // in order to avoid setting the eofbit in the stream state,
568 // which would prevent any further read. The space seems a
571 string_num_get_facet f;
572 f.get(s.begin(), s.end(), b, err, v);
579 /// class to add our facets to the global locale
580 class locale_initializer {
585 std::locale const loc1(global, new ascii_ctype_facet);
586 std::locale const loc2(loc1, new ascii_num_put_facet);
587 std::locale const loc3(loc2, new ascii_num_get_facet);
588 std::locale::global(loc3);
595 /// make sure that our facets get used
596 static locale_initializer initializer;