]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
several small patches
[lyx.git] / src / support / lstrings.C
index 77ca7f0662fc4638d5114458144d4f6ffa9ef848..c270def0cea9f7b80bcea10a7d694bbd871970a3 100644 (file)
@@ -95,6 +95,23 @@ bool isStrInt(string const & str)
 }
 
 
+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;
+       }
+       return true;
+}
+
+
 int strToInt(string const & str)
 {
        if (isStrInt(str)) {
@@ -108,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;
@@ -228,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)
@@ -252,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
        }
 }