]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
remove commented HAVE_SSTREAM code
[lyx.git] / src / support / lstrings.C
index 6ac6cbb2931d134151e8990b6487e66c445c3f7c..3d040d78f97d3bfd54dbd44615afdb575540244b 100644 (file)
@@ -1,23 +1,37 @@
+/* This file is part of
+ * ====================================================== 
+ * 
+ *           LyX, The Document Processor
+ *        
+ *           Copyright 1995 Matthias Ettrich
+ *           Copyright 1995-2000 The LyX Team.
+ *
+ * ====================================================== */
+
 #include <config.h>
 
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
 #include <algorithm>
 
-#ifdef __GLIBCPP__
-#include <ctype.h>
-#else
 #include <cctype>
-#endif
 #include <cstdlib>
 
 #include "LString.h"
 #include "lstrings.h"
 #include "LRegex.h"
+#include "LAssert.h"
 
 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.
 using std::tolower;
 using std::toupper;
-
+#endif
        
 int compare_no_case(string const & s, string const & s2)
 {
@@ -75,35 +89,91 @@ bool isStrInt(string const & str)
        
        string::const_iterator cit = tmpstr.begin();
        if ( (*cit) == '-') ++cit;
-       for (; cit != tmpstr.end(); ++cit) {
+       string::const_iterator end = tmpstr.end();
+       for (; cit != end; ++cit) {
                if (!isdigit((*cit))) return false;
        }
        return true;
 }
 
 
-int  strToInt(string const & str)
+int strToInt(string const & str)
 {
-       string tmpstr;
-
        if (isStrInt(str)) {
                // Remove leading and trailing white space chars.
-               tmpstr = frontStrip(strip(str, ' '), ' ');
+               string 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;
+       string::const_iterator end = tmpstr.end();
+       for (; cit != 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)
+{
+       if (isStrDbl(str)) {
+               // Remove leading and trailing white space chars.
+               string tmpstr = frontStrip(strip(str, ' '), ' ');
+               // Do the conversion proper.
+               return atof(tmpstr.c_str());
+       } else {
+               return 0.0;
+       }
+}
+
+
+char lowercase(char c) 
+{ 
+       return tolower(c); 
+}
+
+
+char uppercase(char c) 
+{ 
+       return toupper(c); 
 }
 
 
-string lowercase(string const & a)
+string const lowercase(string const & a)
 {
        string tmp(a);
 //#ifdef __GLIBCPP__
        string::iterator result = tmp.begin();
+       string::iterator end = tmp.end();
        for (string::iterator first = tmp.begin();
-            first != tmp.end(); ++first, ++result) {
-               *result = tolower(*first);
+            first != end; ++first, ++result) {
+               *result = lowercase(*first);
        }
 //#else
 //     transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
@@ -112,14 +182,15 @@ string lowercase(string const & a)
 }
 
 
-string uppercase(string const & a)
+string const uppercase(string const & a)
 {
        string tmp(a);
 //#ifdef __GLIBCPP__
        string::iterator result = tmp.begin();
+       string::iterator end = tmp.end();
        for (string::iterator first = tmp.begin();
-            first != tmp.end(); ++first, ++result) {
-               *result = toupper(*first);
+            first != end; ++first, ++result) {
+               *result = uppercase(*first);
        }
 //#else
 //     transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
@@ -130,11 +201,23 @@ string uppercase(string const & a)
 
 bool prefixIs(string const & a, char const * pre)
 {
+       Assert(pre);
+       
        unsigned int l = strlen(pre);
        if (l > a.length() || a.empty())
                return false;
-       else
+       else {
+#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
+               // Delete this code when the compilers get a bit better.
+               return ::strncmp(a.c_str(), pre, l) == 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(0, l, pre, l) == 0;
+#endif
+       }
 }
 
 
@@ -147,24 +230,40 @@ bool suffixIs(string const & a, char c)
 
 bool suffixIs(string const & a, char const * suf)
 {
-       unsigned int suflen = strlen(suf);
+       Assert(suf);
+       
+       unsigned int const suflen = strlen(suf);
        if (suflen > a.length())
                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);
+               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;
+#endif
        }
 }
 
 
 bool contains(char const * a, string const & b)
 {
-       if (!a || !*a || b.empty()) return false;
+       Assert(a);
+       
+       if (!*a || b.empty()) return false;
        return strstr(a, b.c_str()) != 0;
 }
 
 
 bool contains(string const & a, char const * b)
 {
+       Assert(b);
+       
        if (a.empty())
                return false;
        return a.find(b) != string::npos;
@@ -181,12 +280,44 @@ bool contains(string const & a, string const & b)
 
 bool contains(char const * a, char const * b)
 {
-       if (!a || !b || !*a || !*b) return false;
+       Assert(a && b);
+       
+       if (!*a || !*b) return false;
        return strstr(a, b) != 0;
 }
 
 
-unsigned int countChar(string const & a, char const c)
+bool containsOnly(string const & s, char const * cset)
+{
+       Assert(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)
+{
+       Assert(s && cset);
+       
+       return string(s).find_first_not_of(cset) == string::npos;
+}
+
+
+bool containsOnly(char const * s, string const & cset)
+{
+       Assert(s);
+       
+       return string(s).find_first_not_of(cset) == string::npos;
+}
+
+
+unsigned int countChar(string const & a, char c)
 {
 #ifdef HAVE_STD_COUNT
        return count(a.begin(), a.end(), c);
@@ -200,7 +331,7 @@ unsigned int countChar(string const & a, char const c)
 
 // ale970405+lasgoutt-970425
 // rewritten to use new string (Lgb)
-string token(string const & a, char delim, int n)
+string const token(string const & a, char delim, int n)
 {
        if (a.empty()) return string();
        
@@ -227,7 +358,7 @@ string token(string const & a, char delim, int n)
 int tokenPos(string const & a, char delim, string const & tok)
 {
        int i = 0;
-       string str = a;
+       string str(a);
        string tmptok;
 
        while (!str.empty()) {
@@ -255,20 +386,23 @@ bool regexMatch(string const & a, string const & pattern)
 }
 
 
-string subst(string const & a, char oldchar, char newchar)
+string const subst(string const & a, char oldchar, char newchar)
 {
-       string tmp = a;
+       string tmp(a);
        string::iterator lit = tmp.begin();
-       for(; lit != tmp.end(); ++lit)
+       string::iterator end = tmp.end();
+       for(; lit != end; ++lit)
                if ((*lit) == oldchar)
                        (*lit) = newchar;
        return tmp;
 }
 
 
-string subst(string const & a,
+string const subst(string const & a,
             char const * oldstr, string const & newstr)
 {
+       Assert(oldstr);
+       
        string lstr(a);
        string::size_type i = 0;
        int olen = strlen(oldstr);
@@ -281,24 +415,26 @@ string subst(string const & a,
 }
 
 
-string strip(string const & a, char const c)
+string const strip(string const & a, char c)
 {
        if (a.empty()) return a;
-       string tmp = a;
+       string tmp(a);
        string::size_type i = tmp.find_last_not_of(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);
        else
-               tmp.clear(); // only c in the whole string
+               tmp.erase(); // only c in the whole string
        return tmp;
 }
 
 
-string frontStrip(string const & a, char const * p)
+string const frontStrip(string const & a, char const * p)
 {
-       if (a.empty() || !p || !*p) return a;
-       string tmp = a;
+       Assert(p);
+       
+       if (a.empty() || !*p) return a;
+       string tmp(a);
        string::size_type i = tmp.find_first_not_of(p);
        if (i > 0)
                tmp.erase(0, i);
@@ -306,10 +442,10 @@ string frontStrip(string const & a, char const * p)
 }
 
 
-string frontStrip(string const & a, char const c)
+string const frontStrip(string const & a, char c)
 {
        if (a.empty()) return a;
-       string tmp = a;
+       string tmp(a);
        string::size_type i = tmp.find_first_not_of(c);
        if (i > 0)
                tmp.erase(0, i);
@@ -317,7 +453,7 @@ string frontStrip(string const & a, char const c)
 }
 
 
-string split(string const & a, string & piece, char delim)
+string const split(string const & a, string & piece, char delim)
 {
        string tmp;
        string::size_type i = a.find(delim);
@@ -327,7 +463,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;
@@ -336,7 +472,7 @@ string split(string const & a, string & piece, char delim)
 }
 
 
-string split(string const & a, char delim)
+string const split(string const & a, char delim)
 {
        string tmp;
        string::size_type i = a.find(delim);
@@ -347,7 +483,7 @@ string split(string const & a, char delim)
 
 
 // ale970521
-string rsplit(string const & a, string & piece, char delim)
+string const rsplit(string const & a, string & piece, char delim)
 {
        string tmp;
        string::size_type i = a.rfind(delim);
@@ -355,7 +491,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;
 }