From d1f8c007f060daee7237ea21c2ad2098366540af Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Mon, 26 Feb 2007 18:54:03 +0000 Subject: [PATCH] Get rid of some locale dependant functions * src/output_plaintext.C (writePlaintextParagraph): Use compare_ascii_no_case instead of compare_no_case, since the strings to compare are pure ASCII anyway. * src/frontends/qt4/QPrefsDialog.C (setComboxFont): ditto * src/MenuBackend.C (Menu::checkShortcuts): ditto * src/frontends/qt4/QLImage.C (QLImage::loadableFormats): Use ascii_lowercase instead of lowercase since the input is pure ASCII anyway. * src/tex2lyx/text.C (splitLatexLength): ditto * src/support/lstrings.[Ch] (compare_no_case): Get rid of both std::string variants (lowercase): Get rid of std::string variant (uppercase): Change std::string variant to docstring * src/support/tests/regfiles/lstrings: Add new expected output * src/support/tests/lstrings.C: Add tests for docstring functions git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17371 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/MenuBackend.C | 3 +- src/frontends/qt4/QLImage.C | 6 +-- src/frontends/qt4/QPrefsDialog.C | 8 ++-- src/output_plaintext.C | 3 +- src/support/lstrings.C | 67 +++-------------------------- src/support/lstrings.h | 16 +------ src/support/tests/lstrings.C | 10 ++++- src/support/tests/regfiles/lstrings | 3 ++ src/tex2lyx/text.C | 2 +- 9 files changed, 30 insertions(+), 88 deletions(-) diff --git a/src/MenuBackend.C b/src/MenuBackend.C index 5419f6d8b7..7470912605 100644 --- a/src/MenuBackend.C +++ b/src/MenuBackend.C @@ -50,7 +50,6 @@ namespace lyx { -using support::compare_no_case; using support::compare_ascii_no_case; using support::contains; using support::makeDisplayPath; @@ -401,7 +400,7 @@ void Menu::checkShortcuts() const << "\" does not contain shortcut `" << to_utf8(shortcut) << "'." << endl; for (const_iterator it2 = begin(); it2 != it1 ; ++it2) { - if (!compare_no_case(it2->shortcut(), shortcut)) { + if (!compare_ascii_no_case(it2->shortcut(), shortcut)) { lyxerr << "Menu warning: menu entries " << '"' << to_utf8(it1->fulllabel()) << "\" and \"" << to_utf8(it2->fulllabel()) diff --git a/src/frontends/qt4/QLImage.C b/src/frontends/qt4/QLImage.C index 10ea1b956d..e506a726c7 100644 --- a/src/frontends/qt4/QLImage.C +++ b/src/frontends/qt4/QLImage.C @@ -20,7 +20,7 @@ #include "graphics/GraphicsParams.h" #include "support/filename.h" -#include "support/lstrings.h" // lowercase +#include "support/lstrings.h" // ascii_lowercase #include #include @@ -31,7 +31,7 @@ #include #include -using lyx::support::lowercase; +using lyx::support::ascii_lowercase; using boost::bind; @@ -89,7 +89,7 @@ Image::FormatList QLImage::loadableFormats() lyxerr[Debug::GRAPHICS] << (const char *) *it << ", "; - string ext = lowercase((const char *) *it); + string ext = ascii_lowercase((const char *) *it); // special case if (ext == "jpeg") diff --git a/src/frontends/qt4/QPrefsDialog.C b/src/frontends/qt4/QPrefsDialog.C index 3bc07d6568..ecfdfb4a63 100644 --- a/src/frontends/qt4/QPrefsDialog.C +++ b/src/frontends/qt4/QPrefsDialog.C @@ -56,7 +56,7 @@ #include #include -using lyx::support::compare_no_case; +using lyx::support::compare_ascii_no_case; using lyx::support::os::external_path; using lyx::support::os::external_path_list; using lyx::support::os::internal_path; @@ -110,7 +110,7 @@ void setComboxFont(QComboBox * cb, string const & family, string const & foundry // We count in reverse in order to prefer the Xft foundry for (int i = cb->count() - 1; i >= 0; --i) { pair tmp = parseFontName(fromqstr(cb->itemText(i))); - if (compare_no_case(tmp.first, family) == 0) { + if (compare_ascii_no_case(tmp.first, family) == 0) { cb->setCurrentIndex(i); return; } @@ -122,7 +122,7 @@ void setComboxFont(QComboBox * cb, string const & family, string const & foundry // We count in reverse in order to prefer the Xft foundry for (int i = cb->count() - 1; i >= 0; --i) { pair tmp = parseFontName(fromqstr(cb->itemText(i))); - if (compare_no_case(tmp.first, tmpfam.first) == 0) { + if (compare_ascii_no_case(tmp.first, tmpfam.first) == 0) { cb->setCurrentIndex(i); return; } @@ -156,7 +156,7 @@ void setComboxFont(QComboBox * cb, string const & family, string const & foundry for (int i = 0; i < cb->count(); ++i) { lyxerr << "Looking at " << fromqstr(cb->itemText(i)) << endl; - if (compare_no_case(fromqstr(cb->itemText(i)), + if (compare_ascii_no_case(fromqstr(cb->itemText(i)), default_font_name) == 0) { cb->setCurrentIndex(i); return; diff --git a/src/output_plaintext.C b/src/output_plaintext.C index 375f107782..d3072e4dc8 100644 --- a/src/output_plaintext.C +++ b/src/output_plaintext.C @@ -29,7 +29,6 @@ namespace lyx { using support::ascii_lowercase; using support::compare_ascii_no_case; -using support::compare_no_case; using support::contains; using support::FileName; @@ -91,7 +90,7 @@ void writePlaintextParagraph(Buffer const & buf, // First write the layout string const & tmp = par.layout()->name(); - if (compare_no_case(tmp, "itemize") == 0) { + if (compare_ascii_no_case(tmp, "itemize") == 0) { ltype = 1; ltype_depth = depth + 1; } else if (compare_ascii_no_case(tmp, "enumerate") == 0) { diff --git a/src/support/lstrings.C b/src/support/lstrings.C index f1333eee0b..42b76a5a16 100644 --- a/src/support/lstrings.C +++ b/src/support/lstrings.C @@ -48,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(); @@ -148,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; @@ -335,9 +290,6 @@ namespace { // calls to std::transform yet, we use these helper clases. (Lgb) struct local_lowercase { - char operator()(char c) const { - return tolower(c); - } char_type operator()(char_type c) const { if (!is_utf16(c)) // We don't know how to lowercase a non-utf16 char @@ -347,8 +299,11 @@ struct local_lowercase { }; 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()); } }; @@ -360,14 +315,6 @@ template 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()); - return tmp; -} - - docstring const lowercase(docstring const & a) { docstring tmp(a); @@ -376,9 +323,9 @@ docstring const lowercase(docstring const & a) } -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; } diff --git a/src/support/lstrings.h b/src/support/lstrings.h index 77b4ee5c3c..9c9dbf4d73 100644 --- a/src/support/lstrings.h +++ b/src/support/lstrings.h @@ -24,10 +24,6 @@ namespace lyx { namespace support { -/// Compare \p s and \p s2, ignoring the case. -/// Caution: Depends on the locale -int compare_no_case(std::string const & s, std::string const & s2); - /// Compare \p s and \p s2, ignoring the case. /// Does not depend on the locale. int compare_no_case(docstring const & s, docstring const & s2); @@ -38,10 +34,6 @@ int compare_ascii_no_case(std::string const & s, std::string const & s2); /// Compare \p s and \p s2, ignoring the case of ASCII characters only. int compare_ascii_no_case(docstring const & s, docstring const & s2); -/// Compare the first \p len characters of \p s and \p s2, ignoring the case. -/// Caution: Depends on the locale -int compare_no_case(std::string const & s, std::string const & s2, unsigned int len); - /// inline int compare(char const * a, char const * b) @@ -100,17 +92,13 @@ char_type uppercase(char_type c); std::string const ascii_lowercase(std::string const &); docstring const ascii_lowercase(docstring const &); -/// Changes the case of \p s to lowercase. -/// Caution: Depends on the locale -std::string const lowercase(std::string const & s); - /// Changes the case of \p s to lowercase. /// Does not depend on the locale. docstring const lowercase(docstring const & s); /// Changes the case of \p s to uppercase. -/// Caution: Depends on the locale -std::string const uppercase(std::string const & s); +/// Does not depend on the locale. +docstring const uppercase(docstring const & s); /// Does the string start with this prefix? bool prefixIs(docstring const &, char_type); diff --git a/src/support/tests/lstrings.C b/src/support/tests/lstrings.C index 998deafec0..90bf967885 100644 --- a/src/support/tests/lstrings.C +++ b/src/support/tests/lstrings.C @@ -1,9 +1,12 @@ +#include + #include "../lstrings.h" #include using namespace lyx::support; +using namespace lyx; using namespace std; @@ -13,14 +16,17 @@ namespace lyx { void test_lowercase() { + cout << to_ascii(docstring(1, lowercase(char_type('A')))) << endl; + cout << to_ascii(lowercase(from_ascii("AlLe"))) << endl; cout << lowercase('A') << endl; - cout << lowercase("AlLe") << endl; + cout << ascii_lowercase("AlLe") << endl; } void test_uppercase() { + cout << to_ascii(docstring(1, uppercase(char_type('a')))) << endl; + cout << to_ascii(uppercase(from_ascii("AlLe"))) << endl; cout << uppercase('a') << endl; - cout << uppercase("AlLe") << endl; } int main() diff --git a/src/support/tests/regfiles/lstrings b/src/support/tests/regfiles/lstrings index aeef9c6315..cc2481b504 100644 --- a/src/support/tests/regfiles/lstrings +++ b/src/support/tests/regfiles/lstrings @@ -1,4 +1,7 @@ a alle +a +alle A ALLE +A diff --git a/src/tex2lyx/text.C b/src/tex2lyx/text.C index d28fdea83b..fe9c2595d9 100644 --- a/src/tex2lyx/text.C +++ b/src/tex2lyx/text.C @@ -262,7 +262,7 @@ bool splitLatexLength(string const & len, string & value, string & unit) if (contains(len, '\\')) unit = trim(string(len, i)); else - unit = lyx::support::lowercase(trim(string(len, i))); + unit = support::ascii_lowercase(trim(string(len, i))); return true; }