]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
small cleanup, doxygen, formatting changes
[lyx.git] / src / support / lstrings.C
index 270af081b9172d8f60377ebf6d9f8cc999810452..b934e77e654eafdd88d24d38c685408010a2079b 100644 (file)
@@ -26,9 +26,7 @@
 
 using std::count;
 using std::transform;
-#ifndef __GLIBCPP__
-// The new glibstdc++-v3 has not worked out all the quirks regarding cctype
-// yet. So currently it failes if the to using lines below are stated.
+#ifndef CXX_GLOBAL_CSTD
 using std::tolower;
 using std::toupper;
 #endif
@@ -36,7 +34,6 @@ using std::toupper;
 
 int compare_no_case(string const & s, string const & s2)
 {
-       // ANSI C
        string::const_iterator p = s.begin();
        string::const_iterator p2 = s2.begin();
 
@@ -48,7 +45,7 @@ int compare_no_case(string const & s, string const & s2)
                ++p;
                ++p2;
        }
-       
+
        if (s.size() == s2.size())
                return 0;
        if (s.size() < s2.size())
@@ -71,7 +68,8 @@ int compare_no_case(string const & s, string const & s2, unsigned int len)
                ++p;
                ++p2;
        }
-       if (s.size() == s2.size())
+
+       if (s.size() >= len && s2.size() >= len)
                return 0;
        if (s.size() < s2.size())
                return -1;
@@ -88,7 +86,24 @@ bool isStrInt(string const & str)
        if (tmpstr.empty()) return false;
        
        string::const_iterator cit = tmpstr.begin();
-       if ( (*cit) == '-') ++cit;
+       if ((*cit) == '-') ++cit;
+       string::const_iterator end = tmpstr.end();
+       for (; cit != end; ++cit) {
+               if (!isdigit((*cit))) return false;
+       }
+       return true;
+}
+
+
+bool isStrUnsignedInt(string const & str)
+{
+       if (str.empty()) return false;
+       
+       // Remove leading and trailing white space chars.
+       string const tmpstr = frontStrip(strip(str, ' '), ' ');
+       if (tmpstr.empty()) return false;
+       
+       string::const_iterator cit = tmpstr.begin();
        string::const_iterator end = tmpstr.end();
        for (; cit != end; ++cit) {
                if (!isdigit((*cit))) return false;
@@ -110,6 +125,19 @@ int strToInt(string const & str)
 }
 
 
+unsigned int strToUnsignedInt(string const & str)
+{
+       if (isStrUnsignedInt(str)) {
+               // Remove leading and trailing white space chars.
+               string const tmpstr = frontStrip(strip(str, ' '), ' ');
+               // Do the conversion proper.
+               return lyx::atoi(tmpstr);
+       } else {
+               return 0;
+       }
+}
+
+
 bool isStrDbl(string const & str)
 {
        if (str.empty()) return false;
@@ -121,7 +149,7 @@ bool isStrDbl(string const & str)
 
        string::const_iterator cit = tmpstr.begin();
        bool found_dot(false);
-       if ( (*cit) == '-') ++cit;
+       if ((*cit) == '-') ++cit;
        string::const_iterator end = tmpstr.end();
        for (; cit != end; ++cit) {
                if (!isdigit((*cit))
@@ -177,7 +205,7 @@ string const lowercase(string const & a)
        }
 #else
        // We want to use this one. (Lgb)
-       transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
+       transform(tmp.begin(), tmp.end(), tmp.begin(), lowercase);
 #endif
        return tmp;
 }
@@ -195,7 +223,7 @@ string const uppercase(string const & a)
        }
 #else
        // We want to use this one. (Lgb)
-       transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
+       transform(tmp.begin(), tmp.end(), tmp.begin(), uppercase);
 #endif
        return tmp;
 }
@@ -230,7 +258,7 @@ bool prefixIs(string const & a, string const & pre)
        string::size_type const prelen = pre.length();
        string::size_type const alen = a.length();
        
-       if (prelen < alen || a.empty())
+       if (prelen > alen || a.empty())
                return false;
        else {
 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
@@ -254,19 +282,21 @@ bool suffixIs(string const & a, char const * suf)
        Assert(suf);
        
        size_t const suflen = strlen(suf);
-       if (suflen > a.length())
+       string::size_type const alen = a.length();
+       
+       if (suflen > alen)
                return false;
        else {
 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
                // Delete this code when the compilers get a bit better.
-               string tmp(a, a.length() - suflen);
+               string tmp(a, alen - suflen);
                return ::strncmp(tmp.c_str(), suf, suflen) == 0;
 #else
                // This is the code that we really want to use
                // but until gcc ships with a basic_string that
                // implements std::string correctly we have to
                // use the code above.
-               return a.compare(a.length() - suflen, suflen, suf) == 0;
+               return a.compare(alen - suflen, suflen, suf) == 0;
 #endif
        }
 }
@@ -314,6 +344,14 @@ bool contains(string const & a, string const & b)
 }
 
 
+bool contains(string const & a, char b)
+{
+       if (a.empty())
+               return false;
+       return a.find(b) != string::npos;
+}
+
+
 bool contains(char const * a, char const * b)
 {
        Assert(a && b);
@@ -427,7 +465,7 @@ string const subst(string const & a, char oldchar, char newchar)
        string tmp(a);
        string::iterator lit = tmp.begin();
        string::iterator end = tmp.end();
-       for(; lit != end; ++lit)
+       for (; lit != end; ++lit)
                if ((*lit) == oldchar)
                        (*lit) = newchar;
        return tmp;
@@ -474,6 +512,13 @@ string const strip(string const & a, char c)
        if (i == a.length() - 1) return tmp; // no c's at end of a
        if (i != string::npos) 
                tmp.erase(i + 1, string::npos);
+#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
+       /// Needed for broken string::find_last_not_of
+       else if (tmp[0] != c) {
+               if (a.length() == 1) return tmp;
+               tmp.erase(1, string::npos);
+       }
+#endif
        else
                tmp.erase(); // only c in the whole string
        return tmp;