X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Funicode.cpp;h=6c51e0f0378bd9aa35d9b6c209da8ab670ccedef;hb=2515a3bded01e27dcc3b19502e649f01fff5ace0;hp=114af7dc2e3e7db6e83a78073b4f19de5a7be475;hpb=f212b483355d68e93132fb469814e13335d0886b;p=lyx.git diff --git a/src/support/unicode.cpp b/src/support/unicode.cpp index 114af7dc2e..6c51e0f037 100644 --- a/src/support/unicode.cpp +++ b/src/support/unicode.cpp @@ -23,6 +23,10 @@ #include using std::endl; +using std::map; +using std::make_pair; +using std::string; +using std::vector; namespace { @@ -193,13 +197,13 @@ namespace { template -std::vector +vector iconv_convert(IconvProcessor & processor, InType const * buf, size_t buflen) { if (buflen == 0) - return std::vector(); + return vector(); char const * inbuf = reinterpret_cast(buf); size_t inbytesleft = buflen * sizeof(InType); @@ -212,33 +216,33 @@ iconv_convert(IconvProcessor & processor, if (bytes <= 0) // Conversion failed // FIXME Maybe throw an exception and handle that in the caller? - return std::vector(); + return vector(); RetType const * tmp = reinterpret_cast(out); - return std::vector(tmp, tmp + bytes / sizeof(RetType)); + return vector(tmp, tmp + bytes / sizeof(RetType)); } } // anon namespace -std::vector utf8_to_ucs4(std::vector const & utf8str) +vector utf8_to_ucs4(vector const & utf8str) { if (utf8str.empty()) - return std::vector(); + return vector(); return utf8_to_ucs4(&utf8str[0], utf8str.size()); } -std::vector +vector utf8_to_ucs4(char const * utf8str, size_t ls) { static IconvProcessor processor(ucs4_codeset, "UTF-8"); - return iconv_convert(processor, utf8str, ls); + return iconv_convert(processor, utf8str, ls); } -std::vector +vector utf16_to_ucs4(unsigned short const * s, size_t ls) { static IconvProcessor processor(ucs4_codeset, utf16_codeset); @@ -246,7 +250,7 @@ utf16_to_ucs4(unsigned short const * s, size_t ls) } -std::vector +vector ucs4_to_utf16(char_type const * s, size_t ls) { static IconvProcessor processor(utf16_codeset, ucs4_codeset); @@ -254,53 +258,89 @@ ucs4_to_utf16(char_type const * s, size_t ls) } -std::vector -ucs4_to_utf8(lyx::char_type c) +vector +ucs4_to_utf8(char_type c) { static IconvProcessor processor("UTF-8", ucs4_codeset); return iconv_convert(processor, &c, 1); } -std::vector -ucs4_to_utf8(std::vector const & ucs4str) +vector +ucs4_to_utf8(vector const & ucs4str) { if (ucs4str.empty()) - return std::vector(); + return vector(); return ucs4_to_utf8(&ucs4str[0], ucs4str.size()); } -std::vector -ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls) +vector +ucs4_to_utf8(char_type const * ucs4str, size_t ls) { static IconvProcessor processor("UTF-8", ucs4_codeset); return iconv_convert(processor, ucs4str, ls); } -std::vector -eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding) +vector +eightbit_to_ucs4(char const * s, size_t ls, string const & encoding) { - static std::map processors; + static map processors; if (processors.find(encoding) == processors.end()) { IconvProcessor processor(ucs4_codeset, encoding.c_str()); - processors.insert(std::make_pair(encoding, processor)); + processors.insert(make_pair(encoding, processor)); } return iconv_convert(processors[encoding], s, ls); } -std::vector -ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding) +vector +ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding) { - static std::map processors; + static map processors; if (processors.find(encoding) == processors.end()) { IconvProcessor processor(encoding.c_str(), ucs4_codeset); - processors.insert(std::make_pair(encoding, processor)); + processors.insert(make_pair(encoding, processor)); } return iconv_convert(processors[encoding], ucs4str, ls); } + +char ucs4_to_eightbit(char_type ucs4, string const & encoding) +{ + static map processors; + map::iterator it = processors.find(encoding); + if (it == processors.end()) { + IconvProcessor processor(encoding.c_str(), ucs4_codeset); + it = processors.insert(make_pair(encoding, processor)).first; + } + + char out; + int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1); + if (bytes > 0) + return out; + return 0; +} + + +void ucs4_to_multibytes(char_type ucs4, vector & out, + string const & encoding) +{ + static map processors; + map::iterator it = processors.find(encoding); + if (it == processors.end()) { + IconvProcessor processor(encoding.c_str(), ucs4_codeset); + it = processors.insert(make_pair(encoding, processor)).first; + } + + out.resize(4); + int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4); + if (bytes > 0) + out.resize(bytes); + else + out.clear(); +} + } // namespace lyx