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"
18 #include <boost/assert.hpp>
24 docstring const from_ascii(char const * ascii)
27 for (char const * c = ascii; *c; ++c) {
28 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
35 docstring const from_ascii(std::string const & ascii)
37 int const len = ascii.length();
38 for (int i = 0; i < len; ++i)
39 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
40 return docstring(ascii.begin(), ascii.end());
44 std::string const to_ascii(docstring const & ucs4)
46 int const len = ucs4.length();
49 for (int i = 0; i < len; ++i) {
50 BOOST_ASSERT(ucs4[i] < 0x80);
51 ascii[i] = static_cast<char>(ucs4[i]);
57 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
59 size_t n = utf8.size();
60 // as utf8 is a multi-byte encoding, there would be at most
66 int maxoutsize = n * 4;
68 // basic_string::data() is not recognized by some old gcc version
69 // so we use &(ucs4[0]) instead.
70 char * outbuf = (char *)(&(ucs4[0]));
71 int bytes = iconv_convert(cd, ucs4_codeset, "UTF-8",
72 utf8.c_str(), n, outbuf, maxoutsize);
74 // adjust to the real converted size
79 docstring const from_utf8(std::string const & utf8)
82 utf8_to_ucs4(utf8, ucs4);
87 std::string const to_utf8(docstring const & ucs4)
89 std::vector<char> const utf8 =
90 ucs4_to_utf8(ucs4.data(), ucs4.size());
91 return std::string(utf8.begin(), utf8.end());
95 bool operator==(lyx::docstring const & l, char const * r)
97 int const len = l.length();
98 for (int i = 0; i < len; ++i) {
99 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
102 if (l[i] != lyx::docstring::value_type(r[i]))
105 return r[len] == '\0';
109 lyx::docstring operator+(lyx::docstring const & l, char const * r)
112 for (char const * c = r; *c; ++c) {
113 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
120 lyx::docstring operator+(char const * l, lyx::docstring const & r)
123 for (char const * c = l; *c; ++c) {
124 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
132 lyx::docstring operator+(lyx::docstring const & l, char r)
134 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
135 return l + lyx::docstring::value_type(r);
139 lyx::docstring operator+(char l, lyx::docstring const & r)
141 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
142 return lyx::docstring::value_type(l) + r;
146 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
148 for (char const * c = r; *c; ++c) {
149 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
156 lyx::docstring & operator+=(lyx::docstring & l, char r)
158 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
165 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
167 // gcc does not have proper locale facets for lyx::char_type if
168 // sizeof(wchar_t) == 2, so we have to implement them on our own.
171 // We get undefined references to these virtual methods. This looks like
172 // a bug in gcc. The implementation here does not do anything useful, since
173 // it is overriden in ascii_ctype_facet.
175 template<> ctype<lyx::char_type>::~ctype() {}
177 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
178 template<> lyx::char_type const *
179 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
180 template<> const lyx::char_type *
181 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
182 template<> const lyx::char_type *
183 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
184 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
185 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
186 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
187 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
188 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
189 template<> const char *
190 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
192 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
193 template<> const lyx::char_type *
194 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
200 class ctype_failure : public std::bad_cast {
202 ctype_failure() throw() : std::bad_cast() {}
203 virtual ~ctype_failure() throw() {}
204 virtual const char* what() const throw()
206 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
211 /// ctype facet for UCS4 characters. The implementation does only support pure
212 /// ASCII, since we do not need anything else for now.
213 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
214 class ascii_ctype_facet : public std::ctype<lyx::char_type>
217 typedef lyx::char_type char_type;
218 typedef wctype_t wmask_type;
219 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
221 M_initialize_ctype();
226 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
228 wmask_type M_wmask[16];
229 wmask_type M_convert_to_wmask(const mask m) const
233 case space: ret = wctype("space"); break;
234 case print: ret = wctype("print"); break;
235 case cntrl: ret = wctype("cntrl"); break;
236 case upper: ret = wctype("upper"); break;
237 case lower: ret = wctype("lower"); break;
238 case alpha: ret = wctype("alpha"); break;
239 case digit: ret = wctype("digit"); break;
240 case punct: ret = wctype("punct"); break;
241 case xdigit: ret = wctype("xdigit"); break;
242 case alnum: ret = wctype("alnum"); break;
243 case graph: ret = wctype("graph"); break;
244 default: ret = wmask_type();
248 void M_initialize_ctype()
251 for (i = 0; i < 128; ++i) {
252 const int c = wctob(i);
256 M_narrow[i] = static_cast<char>(c);
262 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
263 M_widen[i] = btowc(i);
265 for (size_t i = 0; i <= 15; ++i) {
266 M_bit[i] = static_cast<mask>(1 << i);
267 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
270 virtual ~ascii_ctype_facet() {}
271 char_type do_toupper(char_type c) const
274 throw ctype_failure();
275 return toupper(static_cast<int>(c));
277 char_type const * do_toupper(char_type * lo, char_type const * hi) const
281 throw ctype_failure();
282 *lo = toupper(static_cast<int>(*lo));
287 char_type do_tolower(char_type c) const
290 throw ctype_failure();
293 char_type const * do_tolower(char_type * lo, char_type const * hi) const
297 throw ctype_failure();
303 bool do_is(mask m, char_type c) const
306 throw ctype_failure();
307 // The code below works because c is in the ASCII range.
308 // We could not use iswctype() which is designed for a 2byte
309 // whar_t without encoding conversion otherwise.
311 // Generically, 15 (instead of 10) since we don't know the numerical
312 // encoding of the various categories in /usr/include/ctype.h.
313 const size_t bitmasksize = 15;
314 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
315 if (m & M_bit[bitcur] &&
316 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
322 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
324 for (;lo < hi; ++vec, ++lo) {
326 throw ctype_failure();
327 // The code below works because c is in the ASCII range.
328 // We could not use iswctype() which is designed for a 2byte
329 // whar_t without encoding conversion otherwise.
330 // Generically, 15 (instead of 10) since we don't know the numerical
331 // encoding of the various categories in /usr/include/ctype.h.
332 const size_t bitmasksize = 15;
334 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
335 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
341 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
343 while (lo < hi && !this->do_is(m, *lo))
347 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
349 while (lo < hi && this->do_is(m, *lo) != 0)
353 char_type do_widen(char c) const
355 if (static_cast<unsigned char>(c) < 0x80)
357 throw ctype_failure();
359 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
362 if (static_cast<unsigned char>(*lo) >= 0x80)
363 throw ctype_failure();
370 char do_narrow(char_type wc, char) const
373 return static_cast<char>(wc);
374 throw ctype_failure();
376 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
380 *dest = static_cast<char>(*lo);
382 throw ctype_failure();
391 /// class to add our ascii_ctype_facet to the global locale
392 class locale_initializer {
397 std::locale const loc(global, new ascii_ctype_facet);
398 std::locale::global(loc);
405 /// make sure that our ascii_ctype_facet gets used
406 static locale_initializer initializer;