]> git.lyx.org Git - lyx.git/blobdiff - src/support/docstring.cpp
Let paragraph::requestSpellcheck() consider contained insets
[lyx.git] / src / support / docstring.cpp
index f3a9c01c6bc379ee364e4f8e2f8683a933796317..cbee18a20a4584ce9f41e87eec322d080cb102f6 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 <typeinfo>
-
 #include <QFile>
 
-#include "support/lassert.h"
+//Needed in Ubuntu
+#include <typeinfo>
+#if ! defined(USE_WCHAR_T) && defined(__GNUC__)
+#include <locale>
+#include <iostream>
+#endif
 
 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) {
-               LASSERT(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];
+                       LATTEST(static_cast<unsigned char>(ascii[n]) < 0x80);
+               }
        }
        return s;
 }
@@ -44,7 +51,7 @@ docstring const from_ascii(string const & ascii)
 {
        int const len = ascii.length();
        for (int i = 0; i < len; ++i)
-               LASSERT(static_cast<unsigned char>(ascii[i]) < 0x80, /**/);
+               LATTEST(static_cast<unsigned char>(ascii[i]) < 0x80);
        return docstring(ascii.begin(), ascii.end());
 }
 
@@ -55,21 +62,13 @@ string const to_ascii(docstring const & ucs4)
        string ascii;
        ascii.resize(len);
        for (int i = 0; i < len; ++i) {
-               LASSERT(ucs4[i] < 0x80, /**/);
+               LATTEST(ucs4[i] < 0x80);
                ascii[i] = static_cast<char>(ucs4[i]);
        }
        return ascii;
 }
 
 
-IconvProcessor & utf8ToUcs4()
-{
-       static IconvProcessor iconv(ucs4_codeset, "UTF-8");
-       return iconv;
-}
-
-
-
 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
 {
        size_t n = utf8.size();
@@ -100,8 +99,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());
 }
 
@@ -115,9 +113,9 @@ docstring const from_local8bit(string const & s)
 /// Exception thrown by to_local8bit if the string could not be converted
 class to_local8bit_failure : public bad_cast {
 public:
-       to_local8bit_failure() throw() : bad_cast() {}
-       virtual ~to_local8bit_failure() throw() {}
-       virtual const char* what() const throw()
+       to_local8bit_failure() noexcept : bad_cast() {}
+       virtual ~to_local8bit_failure() noexcept {}
+       const char* what() const noexcept override
        {
                return "A string could not be converted from unicode to the local 8 bit encoding.";
        }
@@ -130,7 +128,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());
 }
@@ -150,6 +148,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));
@@ -161,7 +175,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) {
-               LASSERT(static_cast<unsigned char>(*r) < 0x80, /**/);
+               LASSERT(static_cast<unsigned char>(*r) < 0x80, return false);
                if (!*r)
                        return false;
                if (*it != static_cast<lyx::docstring::value_type>(*r))
@@ -175,7 +189,7 @@ lyx::docstring operator+(lyx::docstring const & l, char const * r)
 {
        lyx::docstring s(l);
        for (char const * c = r; *c; ++c) {
-               LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
+               LASSERT(static_cast<unsigned char>(*c) < 0x80, return l);
                s.push_back(*c);
        }
        return s;
@@ -186,7 +200,7 @@ lyx::docstring operator+(char const * l, lyx::docstring const & r)
 {
        lyx::docstring s;
        for (char const * c = l; *c; ++c) {
-               LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
+               LASSERT(static_cast<unsigned char>(*c) < 0x80, return r);
                s.push_back(*c);
        }
        s += r;
@@ -196,7 +210,7 @@ lyx::docstring operator+(char const * l, lyx::docstring const & r)
 
 lyx::docstring operator+(lyx::docstring const & l, char r)
 {
-       LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
+       LASSERT(static_cast<unsigned char>(r) < 0x80, return l);
        docstring s = l;
        s += docstring::value_type(r);
        return s;
@@ -205,7 +219,7 @@ lyx::docstring operator+(lyx::docstring const & l, char r)
 
 lyx::docstring operator+(char l, lyx::docstring const & r)
 {
-       LASSERT(static_cast<unsigned char>(l) < 0x80, /**/);
+       LASSERT(static_cast<unsigned char>(l) < 0x80, return r);
        return lyx::docstring::value_type(l) + r;
 }
 
@@ -213,7 +227,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) {
-               LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
+               LASSERT(static_cast<unsigned char>(*c) < 0x80, return l);
                l.push_back(*c);
        }
        return l;
@@ -222,7 +236,7 @@ lyx::docstring & operator+=(lyx::docstring & l, char const * r)
 
 lyx::docstring & operator+=(lyx::docstring & l, char r)
 {
-       LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
+       LASSERT(static_cast<unsigned char>(r) < 0x80, return l);
        l.push_back(r);
        return l;
 }
@@ -237,7 +251,7 @@ lyx::docstring & operator+=(lyx::docstring & l, char r)
 
 // We get undefined references to these virtual methods. This looks like
 // a bug in gcc. The implementation here does not do anything useful, since
-// it is overriden in ascii_ctype_facet.
+// it is overridden in ascii_ctype_facet.
 namespace std {
 template<> ctype<lyx::char_type>::~ctype() {}
 template<> bool
@@ -259,16 +273,16 @@ template<> char
 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
 template<> const lyx::char_type *
 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
-}
+} // namespace std
 
 
 namespace lyx {
 
 class ctype_failure : public bad_cast {
 public:
-       ctype_failure() throw() : bad_cast() {}
-       virtual ~ctype_failure() throw() {}
-       virtual const char* what() const throw()
+       ctype_failure() noexcept : bad_cast() {}
+       virtual ~ctype_failure() noexcept {}
+       const char* what() const noexcept override
        {
                return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
        }
@@ -277,9 +291,9 @@ public:
 
 class num_put_failure : public bad_cast {
 public:
-       num_put_failure() throw() : bad_cast() {}
-       virtual ~num_put_failure() throw() {}
-       virtual const char* what() const throw()
+       num_put_failure() noexcept : bad_cast() {}
+       virtual ~num_put_failure() noexcept {}
+       const char* what() const noexcept override
        {
                return "The num_put locale facet does only support ASCII characters on this platform.";
        }
@@ -330,8 +344,7 @@ protected:
                        const int c = wctob(i);
                        if (c == EOF)
                                break;
-                       else
-                               M_narrow[i] = static_cast<char>(c);
+                       M_narrow[i] = static_cast<char>(c);
                }
                if (i == 128)
                        M_narrow_ok = true;
@@ -346,13 +359,13 @@ protected:
                }
        }
        virtual ~ascii_ctype_facet() {}
-       char_type do_toupper(char_type c) const
+       char_type do_toupper(char_type c) const override
        {
                if (c >= 0x80)
                        throw ctype_failure();
                return toupper(static_cast<int>(c));
        }
-       char_type const * do_toupper(char_type * lo, char_type const * hi) const
+       char_type const * do_toupper(char_type * lo, char_type const * hi) const override
        {
                while (lo < hi) {
                        if (*lo >= 0x80)
@@ -362,13 +375,13 @@ protected:
                }
                return hi;
        }
-       char_type do_tolower(char_type c) const
+       char_type do_tolower(char_type c) const override
        {
                if (c >= 0x80)
                        throw ctype_failure();
                return tolower(c);
        }
-       char_type const * do_tolower(char_type * lo, char_type const * hi) const
+       char_type const * do_tolower(char_type * lo, char_type const * hi) const override
        {
                while (lo < hi) {
                        if (*lo >= 0x80)
@@ -378,7 +391,7 @@ protected:
                }
                return hi;
        }
-       bool do_is(mask m, char_type c) const
+       bool do_is(mask m, char_type c) const override
        {
                if (c >= 0x80)
                        throw ctype_failure();
@@ -397,7 +410,7 @@ protected:
                        }
                return ret;
        }
-       char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
+       char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const override
        {
                for (;lo < hi; ++vec, ++lo) {
                        if (*lo >= 0x80)
@@ -416,25 +429,25 @@ protected:
                }
                return hi;
        }
-       char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
+       char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const override
        {
                while (lo < hi && !this->do_is(m, *lo))
                        ++lo;
                return lo;
        }
-       char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
+       char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const override
        {
                while (lo < hi && this->do_is(m, *lo) != 0)
                        ++lo;
                return lo;
        }
-       char_type do_widen(char c) const
+       char_type do_widen(char c) const override
        {
                if (static_cast<unsigned char>(c) < 0x80)
                        return c;
                throw ctype_failure();
        }
-       const char* do_widen(const char* lo, const char* hi, char_type* dest) const
+       const char* do_widen(const char* lo, const char* hi, char_type* dest) const override
        {
                while (lo < hi) {
                        if (static_cast<unsigned char>(*lo) >= 0x80)
@@ -445,13 +458,13 @@ protected:
                }
                return hi;
        }
-       char do_narrow(char_type wc, char) const
+       char do_narrow(char_type wc, char) const override
        {
                if (wc < 0x80)
                        return static_cast<char>(wc);
                throw ctype_failure();
        }
-       const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
+       const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const override
        {
                while (lo < hi) {
                        if (*lo < 0x80)
@@ -483,51 +496,51 @@ public:
 
 protected:
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, bool v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, long v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, long v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 
-#ifdef _GLIBCXX_USE_LONG_LONG
+#ifdef HAVE_LONG_LONG_INT
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, long long v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 #endif
 
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, double v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, double v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, long double v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
 
        iter_type
-       do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
+       do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const override
        {
                return do_put_helper(oit, b, fill, v);
        }
@@ -582,7 +595,7 @@ public:
 protected:
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, bool & v) const
+               ios_base::iostate & err, bool & v) const override
        {
                if (b.flags() & ios_base::boolalpha) {
                        numpunct_facet p;
@@ -612,7 +625,7 @@ protected:
                        }
                        if (ok) {
                                err = ios_base::goodbit;
-                               v = truename == s ? true : false;
+                               v = (truename == s);
                        } else
                                err = ios_base::failbit;
                        if (iit == eit)
@@ -635,43 +648,43 @@ protected:
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, long & v) const
+               ios_base::iostate & err, long & v) const override
        {
                return do_get_integer(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, unsigned short & v) const
+               ios_base::iostate & err, unsigned short & v) const override
        {
                return do_get_integer(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, unsigned int & v) const
+               ios_base::iostate & err, unsigned int & v) const override
        {
                return do_get_integer(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, unsigned long & v) const
+               ios_base::iostate & err, unsigned long & v) const override
        {
                return do_get_integer(iit, eit, b, err, v);
        }
 
-#ifdef _GLIBCXX_USE_LONG_LONG
+#ifdef HAVE_LONG_LONG_INT
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, long long & v) const
+               ios_base::iostate & err, long long & v) const override
        {
                return do_get_integer(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, unsigned long long & v) const
+               ios_base::iostate & err, unsigned long long & v) const override
        {
                return do_get_integer(iit, eit, b, err, v);
        }
@@ -679,28 +692,28 @@ protected:
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, float & v) const
+               ios_base::iostate & err, float & v) const override
        {
                return do_get_float(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, double & v) const
+               ios_base::iostate & err, double & v) const override
        {
                return do_get_float(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, long double & v) const
+               ios_base::iostate & err, long double & v) const override
        {
                return do_get_float(iit, eit, b, err, v);
        }
 
        iter_type
        do_get(iter_type iit, iter_type eit, ios_base & b,
-               ios_base::iostate & err, void * & v) const
+               ios_base::iostate & err, void * & v) const override
        {
                unsigned long val;
                iter_type end = do_get_integer(iit, eit, b, err, val);
@@ -735,9 +748,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>
@@ -749,7 +762,6 @@ private:
                // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
                string s;
                s.reserve(64);
-               char c;
                numpunct_facet p;
                char const dot = p.decimal_point();
                char const sep = p.thousands_sep();
@@ -807,6 +819,6 @@ namespace {
 /// make sure that our facets get used
 static locale_initializer initializer;
 
-}
-}
+} // namespace
+} // namespace lyx
 #endif