]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
Small things in my tree.
[lyx.git] / src / support / lstrings.C
index efc8df4165953f2767c41a304fd70f3031589673..3d2a1c00e8ecb55954103e2dd2da7dfb2eeb0d78 100644 (file)
@@ -90,11 +90,59 @@ int  strToInt(string const & str)
                tmpstr = frontStrip(strip(str, ' '), ' ');
                // Do the conversion proper.
                return atoi(tmpstr.c_str());
-       } else
+       } else {
                return 0;
+       }
 }
 
 
+
+///
+bool isStrDbl(string const & str)
+{
+       if (str.empty()) return false;
+       
+       // Remove leading and trailing white space chars.
+       string tmpstr = frontStrip(strip(str, ' '), ' ');
+       if (tmpstr.empty()) return false;
+       //      if (1 < tmpstr.count('.')) return false;
+
+       string::const_iterator cit = tmpstr.begin();
+       bool found_dot(false);
+       if ( (*cit) == '-') ++cit;
+       for (; cit != tmpstr.end(); ++cit) {
+               if (!isdigit((*cit))
+                   && '.' != (*cit)) {
+                       return false;
+               }
+               if ('.' == (*cit)) {
+                       if (found_dot) {
+                               return false;
+                       } else {
+                               found_dot = true;
+                       }
+               }
+       }
+       return true;
+}
+
+///
+double strToDbl(string const & str)
+{
+       string tmpstr;
+
+       if (isStrDbl(str)) {
+               // Remove leading and trailing white space chars.
+               tmpstr = frontStrip(strip(str, ' '), ' ');
+               // Do the conversion proper.
+               return atof(tmpstr.c_str());
+       } else {
+               return 0.0;
+       }
+}
+
+
+
 string lowercase(string const & a)
 {
        string tmp(a);
@@ -185,6 +233,30 @@ bool contains(char const * a, char const * b)
 }
 
 
+bool containsOnly(string const & s, char const * cset)
+{
+       return s.find_first_not_of(cset) == string::npos;
+}
+
+
+bool containsOnly(string const & s, string const & cset)
+{
+       return s.find_first_not_of(cset) == string::npos;
+}
+
+
+bool containsOnly(char const * s, char const * cset)
+{
+       return string(s).find_first_not_of(cset) == string::npos;
+}
+
+
+bool containsOnly(char const * s, string const & cset)
+{
+       return string(s).find_first_not_of(cset) == string::npos;
+}
+
+
 unsigned int countChar(string const & a, char const c)
 {
 #ifdef HAVE_STD_COUNT
@@ -289,7 +361,7 @@ string strip(string const & a, char const c)
        if (i != string::npos) 
                tmp.erase(i + 1, string::npos);
        else
-               tmp.clear(); // only c in the whole string
+               tmp.erase(); // only c in the whole string
        return tmp;
 }
 
@@ -326,7 +398,7 @@ string split(string const & a, string & piece, char delim)
                piece = a.substr(0, i);
                tmp = a.substr(i + 1);
        } else if (i == 0) {
-               piece.clear();
+               piece.erase();
                tmp = a.substr(i + 1);
        } else {
                piece = a;
@@ -354,7 +426,7 @@ string rsplit(string const & a, string & piece, char delim)
                piece = a.substr(0, i);
                tmp = a.substr(i + 1);
        } else { // delimter was not found
-               piece.clear();
+               piece.erase();
        }
        return tmp;
 }