]> git.lyx.org Git - lyx.git/blobdiff - src/support/qstring_helpers.h
final compilation: wheel was already invented :(
[lyx.git] / src / support / qstring_helpers.h
index d1525de8f4b9536bfe24af0898c3b72f0e2f4ac7..db98b4e1d6e1942ad1065d503c3e510e2800f66a 100644 (file)
@@ -45,6 +45,14 @@ inline QString const toqstr(std::string const & str)
 }
 
 
+/// Is \p c a valid utf16 char?
+inline bool is_utf16(char_type c)
+{
+       // 0xd800 ... 0xdfff is the range of surrogate pairs.
+       return c < 0xd800 || (c > 0xdfff && c < 0x10000);
+}
+
+
 /**
  * Convert a QChar into a UCS4 character.
  * This is a hack (it does only make sense for the common part of the UCS4
@@ -54,6 +62,7 @@ inline QString const toqstr(std::string const & str)
  */
 inline char_type const qchar_to_ucs4(QChar const & qchar)
 {
+       BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
        return static_cast<char_type>(qchar.unicode());
 }
 
@@ -71,7 +80,7 @@ inline QChar const ucs4_to_qchar(char_type const ucs4)
        // for the ucs2 subrange of unicode. Instead of an assertion we should
        // return some special characters that indicates that its display is
        // not supported.
-       BOOST_ASSERT(ucs4 < 65536);
+       BOOST_ASSERT(is_utf16(ucs4));
        return QChar(static_cast<unsigned short>(ucs4));
 }
 
@@ -94,21 +103,6 @@ QString const toqstr(docstring const & ucs4);
 #endif
 
 
-/**
- * ucs4_to_qstring - convert a UCS4 encoded char_type * into a QString
- *
- * This is a hack for the painter and font metrics and should not be used
- * elsewhere. Since it uses ucs4_to_qchar it has the same limitations.
- */
-inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
-{
-       int i = static_cast<int>(ls);
-       s.resize(i);
-       for (; --i >= 0;)
-               s[i] = ucs4_to_qchar(str[i]);
-}
-
-
 /**
  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
  *