]> git.lyx.org Git - lyx.git/blobdiff - src/support/docstring.cpp
Use empty() to check empty and non-empty'ness not size()
[lyx.git] / src / support / docstring.cpp
index ff716f1e73d1a1c3a384698ae1bfc29f75fc90d0..c937ba3721a1157bd2acff7d0bc754b74b0e614e 100644 (file)
 
 #include "support/docstring.h"
 
+#include "support/lassert.h"
 #include "support/lstrings.h"
 #include "support/qstring_helpers.h"
 #include "support/unicode.h"
 
-#include <locale>
-#include <iostream>
-
 #include <QFile>
 
-#include <boost/assert.hpp>
+#include <locale>
+#include <iostream>
+#include <typeinfo>
 
 using namespace std;
 
-namespace lyx {
+using lyx::support::isHexChar;
 
+namespace lyx {
 
 docstring const from_ascii(char const * ascii)
 {
        docstring s;
-       for (char const * c = ascii; *c; ++c) {
-               BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
-               s.push_back(*c);
+       if (int n = strlen(ascii)) {
+               s.resize(n);
+               char_type *d = &s[0];
+               while (--n >= 0) {
+                       d[n] = ascii[n];
+                       LASSERT(static_cast<unsigned char>(ascii[n]) < 0x80, /**/);
+               }
        }
        return s;
 }
@@ -43,7 +48,7 @@ docstring const from_ascii(string const & ascii)
 {
        int const len = ascii.length();
        for (int i = 0; i < len; ++i)
-               BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
+               LASSERT(static_cast<unsigned char>(ascii[i]) < 0x80, /**/);
        return docstring(ascii.begin(), ascii.end());
 }
 
@@ -54,7 +59,7 @@ string const to_ascii(docstring const & ucs4)
        string ascii;
        ascii.resize(len);
        for (int i = 0; i < len; ++i) {
-               BOOST_ASSERT(ucs4[i] < 0x80);
+               LASSERT(ucs4[i] < 0x80, /**/);
                ascii[i] = static_cast<char>(ucs4[i]);
        }
        return ascii;
@@ -99,8 +104,7 @@ docstring const from_utf8(string const & utf8)
 
 string const to_utf8(docstring const & ucs4)
 {
-       vector<char> const utf8 =
-               ucs4_to_utf8(ucs4.data(), ucs4.size());
+       vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
        return string(utf8.begin(), utf8.end());
 }
 
@@ -129,7 +133,7 @@ string const to_local8bit(docstring const & s)
        if (s.empty())
                return string();
        QByteArray const local = toqstr(s).toLocal8Bit();
-       if (local.size() == 0)
+       if (local.isEmpty())
                throw to_local8bit_failure();
        return string(local.begin(), local.end());
 }
@@ -149,6 +153,22 @@ string const to_filesystem8bit(docstring const & s)
 }
 
 
+string const to_iconv_encoding(docstring const & s, string const & encoding)
+{
+       std::vector<char> const encoded =
+               ucs4_to_eightbit(s.data(), s.length(), encoding);
+       return string(encoded.begin(), encoded.end());
+}
+
+
+docstring const from_iconv_encoding(string const & s, string const & encoding)
+{
+       std::vector<char_type> const ucs4 =
+               eightbit_to_ucs4(s.data(), s.length(), encoding);
+       return docstring(ucs4.begin(), ucs4.end());
+}
+
+
 docstring const normalize_c(docstring const & s)
 {
        return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
@@ -160,7 +180,7 @@ bool operator==(lyx::docstring const & l, char const * r)
        lyx::docstring::const_iterator it = l.begin();
        lyx::docstring::const_iterator end = l.end();
        for (; it != end; ++it, ++r) {
-               BOOST_ASSERT(static_cast<unsigned char>(*r) < 0x80);
+               LASSERT(static_cast<unsigned char>(*r) < 0x80, /**/);
                if (!*r)
                        return false;
                if (*it != static_cast<lyx::docstring::value_type>(*r))
@@ -174,7 +194,7 @@ lyx::docstring operator+(lyx::docstring const & l, char const * r)
 {
        lyx::docstring s(l);
        for (char const * c = r; *c; ++c) {
-               BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
+               LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
                s.push_back(*c);
        }
        return s;
@@ -185,7 +205,7 @@ lyx::docstring operator+(char const * l, lyx::docstring const & r)
 {
        lyx::docstring s;
        for (char const * c = l; *c; ++c) {
-               BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
+               LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
                s.push_back(*c);
        }
        s += r;
@@ -195,7 +215,7 @@ lyx::docstring operator+(char const * l, lyx::docstring const & r)
 
 lyx::docstring operator+(lyx::docstring const & l, char r)
 {
-       BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
+       LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
        docstring s = l;
        s += docstring::value_type(r);
        return s;
@@ -204,7 +224,7 @@ lyx::docstring operator+(lyx::docstring const & l, char r)
 
 lyx::docstring operator+(char l, lyx::docstring const & r)
 {
-       BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
+       LASSERT(static_cast<unsigned char>(l) < 0x80, /**/);
        return lyx::docstring::value_type(l) + r;
 }
 
@@ -212,7 +232,7 @@ lyx::docstring operator+(char l, lyx::docstring const & r)
 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
 {
        for (char const * c = r; *c; ++c) {
-               BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
+               LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
                l.push_back(*c);
        }
        return l;
@@ -221,7 +241,7 @@ lyx::docstring & operator+=(lyx::docstring & l, char const * r)
 
 lyx::docstring & operator+=(lyx::docstring & l, char r)
 {
-       BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
+       LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
        l.push_back(r);
        return l;
 }
@@ -734,9 +754,9 @@ private:
        bool isNumpunct(lyx::char_type const c) const
        {
                /// Only account for the standard numpunct "C" locale facet.
-               return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
-                       || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
-                       || c == 'x' || c == 'X');
+               return c == '-' || c == '+'
+                       || c == 'x' || c == 'X'
+                       || isHexChar(c);
        }
 
        template <typename ValueType>