]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
* src/encoding.C (latexChar,read):
[lyx.git] / src / support / lstrings.C
index 973b260d7cc93ef10501efc7c94200ebeb09a883..fe161be9913257702d9375a4be697dae9de562db 100644 (file)
@@ -14,6 +14,7 @@
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/convert.h"
+#include "support/qstring_helpers.h"
 
 #include "debug.h"
 
 #include <algorithm>
 #include <sstream>
 
-#ifdef LIBC_WCTYPE_USES_UCS4
-// We can use the libc ctype functions because we unset the LC_CTYPE
-// category of the current locale in gettext.C
-#include <wctype.h>
-#else
-// Steal some code from somewhere else, e.g. glib (look at gunicode.h)
-// The code that we currently use does not really work.
-#endif
-
-
-using lyx::docstring;
 
 using std::transform;
 using std::string;
@@ -58,28 +48,6 @@ using std::toupper;
 namespace lyx {
 namespace support {
 
-int compare_no_case(string const & s, string const & s2)
-{
-       string::const_iterator p = s.begin();
-       string::const_iterator p2 = s2.begin();
-
-       while (p != s.end() && p2 != s2.end()) {
-               int const lc1 = tolower(*p);
-               int const lc2 = tolower(*p2);
-               if (lc1 != lc2)
-                       return (lc1 < lc2) ? -1 : 1;
-               ++p;
-               ++p2;
-       }
-
-       if (s.size() == s2.size())
-               return 0;
-       if (s.size() < s2.size())
-               return -1;
-       return 1;
-}
-
-
 int compare_no_case(docstring const & s, docstring const & s2)
 {
        docstring::const_iterator p = s.begin();
@@ -158,29 +126,6 @@ int compare_ascii_no_case(docstring const & s, docstring const & s2)
 }
 
 
-int compare_no_case(string const & s, string const & s2, unsigned int len)
-{
-       string::const_iterator p = s.begin();
-       string::const_iterator p2 = s2.begin();
-       unsigned int i = 0;
-       while (i < len && p != s.end() && p2 != s2.end()) {
-               int const lc1 = tolower(*p);
-               int const lc2 = tolower(*p2);
-               if (lc1 != lc2)
-                       return (lc1 < lc2) ? -1 : 1;
-               ++i;
-               ++p;
-               ++p2;
-       }
-
-       if (s.size() >= len && s2.size() >= len)
-               return 0;
-       if (s.size() < s2.size())
-               return -1;
-       return 1;
-}
-
-
 bool isStrInt(string const & str)
 {
        if (str.empty()) return false;
@@ -311,48 +256,33 @@ bool isAscii(docstring const & str)
 
 char lowercase(char c)
 {
+       BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
        return char(tolower(c));
 }
 
 
 char uppercase(char c)
 {
+       BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
        return char(toupper(c));
 }
 
 
-// FIXME UNICODE
-// for lowercase() and uppercase() function below when wchar_t is not used:
-// 1) std::tolower() and std::toupper() are templates that
-// compile fine with char_type. With the test (c >= 256) we
-// do not trust these function to do the right thing with
-// unicode char.
-// 2) these functions use the current locale, which is wrong
-// if it is not latin1 based (latin1 is a subset of UCS4).
-
 char_type lowercase(char_type c)
 {
-#ifdef LIBC_WCTYPE_USES_UCS4
-       return towlower(c);
-#else
-       if (c >= 256)
+       if (!is_utf16(c))
+               // We don't know how to lowercase a non-utf16 char
                return c;
-
-       return tolower(c);
-#endif
+       return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
 }
 
 
 char_type uppercase(char_type c)
 {
-#ifdef LIBC_WCTYPE_USES_UCS4
-       return towupper(c);
-#else
-       if (c >= 256)
+       if (!is_utf16(c))
+               // We don't know how to uppercase a non-utf16 char
                return c;
-
-       return toupper(c);
-#endif
+       return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
 }
 
 
@@ -361,15 +291,21 @@ namespace {
 // since we cannot use std::tolower and std::toupper directly in the
 // calls to std::transform yet, we use these helper clases. (Lgb)
 
-template<typename Char> struct local_lowercase {
-       Char operator()(Char c) const {
-               return tolower(c);
+struct local_lowercase {
+       char_type operator()(char_type c) const {
+               if (!is_utf16(c))
+                       // We don't know how to lowercase a non-utf16 char
+                       return c;
+               return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
        }
 };
 
 struct local_uppercase {
-       char operator()(char c) const {
-               return toupper(c);
+       char_type operator()(char_type c) const {
+               if (!is_utf16(c))
+                       // We don't know how to uppercase a non-utf16 char
+                       return c;
+               return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
        }
 };
 
@@ -381,25 +317,17 @@ template<typename Char> struct local_ascii_lowercase {
 
 } // end of anon namespace
 
-string const lowercase(string const & a)
-{
-       string tmp(a);
-       transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase<char>());
-       return tmp;
-}
-
-
 docstring const lowercase(docstring const & a)
 {
        docstring tmp(a);
-       transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase<char_type>());
+       transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
        return tmp;
 }
 
 
-string const uppercase(string const & a)
+docstring const uppercase(docstring const & a)
 {
-       string tmp(a);
+       docstring tmp(a);
        transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
        return tmp;
 }