]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
Make it possible to uses non-ascii labelstring, endlabelstring and
[lyx.git] / src / support / lstrings.C
index 578eb85f3941160965c663a025e021009914f59d..939d48531b78e621422d5a2a2ee8bc2fad22d9e9 100644 (file)
@@ -226,6 +226,69 @@ bool isStrDbl(string const & str)
 }
 
 
+namespace {
+
+inline
+bool isHexChar(char_type c)
+{
+       return c == '0' ||
+               c == '1' ||
+               c == '2' ||
+               c == '3' ||
+               c == '4' ||
+               c == '5' ||
+               c == '6' ||
+               c == '7' ||
+               c == '8' ||
+               c == '9' ||
+               c == 'a' || c == 'A' ||
+               c == 'b' || c == 'B' ||
+               c == 'c' || c == 'C' ||
+               c == 'd' || c == 'D' ||
+               c == 'e' || c == 'E' ||
+               c == 'f' || c == 'F';
+}
+
+} // anon namespace
+
+
+bool isHex(docstring const & str)
+{
+       int index = 0;
+
+       if (str.length() > 2 && str[0] == '0' &&
+           (str[1] == 'x' || str[1] == 'X'))
+               index = 2;
+
+       int const len = str.length();
+
+       for (; index < len; ++index) {
+               if (!isHexChar(str[index]))
+                       return false;
+       }
+       return true;
+}
+
+
+int hexToInt(docstring const & str)
+{
+       string s = to_ascii(str);
+       int h;
+       sscanf(s.c_str(), "%x", &h);
+       return h;
+}
+
+
+bool isAscii(docstring const & str)
+{
+       int const len = str.length();
+       for (int i = 0; i < len; ++i)
+               if (str[i] >= 0x80)
+                       return false;
+       return true;
+}
+
+
 char lowercase(char c)
 {
        return char(tolower(c));
@@ -329,6 +392,18 @@ bool prefixIs(string const & a, string const & pre)
 }
 
 
+bool prefixIs(docstring const & a, docstring const & pre)
+{
+       docstring::size_type const prelen = pre.length();
+       docstring::size_type const alen = a.length();
+
+       if (prelen > alen || a.empty())
+               return false;
+       else
+               return a.compare(0, prelen, pre) == 0;
+}
+
+
 bool suffixIs(string const & a, char c)
 {
        if (a.empty()) return false;
@@ -546,19 +621,26 @@ string const rtrim(string const & a, char const * p)
 string const ltrim(string const & a, char const * p)
 {
        BOOST_ASSERT(p);
-
        if (a.empty() || !*p)
                return a;
-
        string::size_type l = a.find_first_not_of(p);
-
        if (l == string::npos)
                return string();
-
        return a.substr(l, string::npos);
 }
 
 
+docstring const ltrim(docstring const & a, char const * p)
+{
+       BOOST_ASSERT(p);
+       if (a.empty() || !*p)
+               return a;
+       size_t l = a.find_first_not_of(from_ascii(p));
+       if (l == docstring::npos)
+               return docstring();
+       return a.substr(l, docstring::npos);
+}
+
 namespace {
 
 template<typename String, typename Char> inline
@@ -622,13 +704,15 @@ string const rsplit(string const & a, string & piece, char delim)
 
 // This function escapes 8-bit characters and other problematic
 // characters that cause problems in latex labels.
-string const escape(string const & lab)
+docstring const escape(docstring const & lab)
 {
-       char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
+       lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
                              '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-       string enc;
-       for (string::size_type i = 0; i < lab.length(); ++i) {
-               unsigned char c= lab[i];
+       docstring enc;
+       for (docstring::size_type i = 0; i < lab.length(); ++i) {
+               lyx::char_type c = lab[i];
+               // FIXME We must change the following algorithm for UCS4
+               // chars, but that will be a file format change.
                if (c >= 128 || c == '=' || c == '%') {
                        enc += '=';
                        enc += hexdigit[c>>4];