]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.C
Don't use a global variable for avoiding removal of the system temp dir
[lyx.git] / src / support / lstrings.C
index 324f33e1ee6a7f9d1c7c24f2eef2cace92c984f9..cd498896a72121c745d19b71b2fb5880a11eda51 100644 (file)
@@ -361,8 +361,8 @@ namespace {
 // since we cannot use std::tolower and std::toupper directly in the
 // calls to std::transform yet, we use these helper clases. (Lgb)
 
-struct local_lowercase {
-       char operator()(char c) const {
+template<typename Char> struct local_lowercase {
+       Char operator()(Char c) const {
                return tolower(c);
        }
 };
@@ -373,8 +373,8 @@ struct local_uppercase {
        }
 };
 
-struct local_ascii_lowercase {
-       char operator()(char c) const {
+template<typename Char> struct local_ascii_lowercase {
+       Char operator()(Char c) const {
                return ascii_tolower(c);
        }
 };
@@ -384,10 +384,19 @@ struct local_ascii_lowercase {
 string const lowercase(string const & a)
 {
        string tmp(a);
-       transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
+       transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase<char>());
        return tmp;
 }
 
+
+docstring const lowercase(docstring const & a)
+{
+       docstring tmp(a);
+       transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase<char_type>());
+       return tmp;
+}
+
+
 string const uppercase(string const & a)
 {
        string tmp(a);
@@ -400,7 +409,16 @@ string const ascii_lowercase(string const & a)
 {
        string tmp(a);
        transform(tmp.begin(), tmp.end(), tmp.begin(),
-                 local_ascii_lowercase());
+                 local_ascii_lowercase<char>());
+       return tmp;
+}
+
+
+docstring const ascii_lowercase(docstring const & a)
+{
+       docstring tmp(a);
+       transform(tmp.begin(), tmp.end(), tmp.begin(),
+                 local_ascii_lowercase<char_type>());
        return tmp;
 }
 
@@ -648,6 +666,23 @@ string const rtrim(string const & a, char const * p)
 }
 
 
+docstring const rtrim(docstring const & a, char const * p)
+{
+       BOOST_ASSERT(p);
+
+       if (a.empty() || !*p)
+               return a;
+
+       docstring::size_type r = a.find_last_not_of(from_ascii(p));
+
+       // Is this test really needed? (Lgb)
+       if (r == docstring::npos)
+               return docstring();
+
+       return a.substr(0, r + 1);
+}
+
+
 string const ltrim(string const & a, char const * p)
 {
        BOOST_ASSERT(p);
@@ -761,38 +796,57 @@ docstring const escape(docstring const & lab)
 }
 
 
-/// gives a vector of stringparts which have the delimiter delim
-vector<string> const getVectorFromString(string const & str,
-                                        string const & delim)
+namespace {
+
+template<typename String> vector<String> const
+getVectorFromStringT(String const & str, String const & delim)
 {
 // Lars would like this code to go, but for now his replacement (below)
 // doesn't fullfil the same function. I have, therefore, reactivated the
 // old code for now. Angus 11 Nov 2002.
 #if 1
-       vector<string> vec;
+       vector<String> vec;
        if (str.empty())
                return vec;
-       string keys = rtrim(str);
+       String keys = rtrim(str);
        for(;;) {
-               string::size_type const idx = keys.find(delim);
-               if (idx == string::npos) {
+               typename String::size_type const idx = keys.find(delim);
+               if (idx == String::npos) {
                        vec.push_back(ltrim(keys));
                        break;
                }
-               string const key = trim(keys.substr(0, idx));
+               String const key = trim(keys.substr(0, idx));
                if (!key.empty())
                        vec.push_back(key);
-               string::size_type const start = idx + delim.size();
+               typename String::size_type const start = idx + delim.size();
                keys = keys.substr(start);
        }
        return vec;
 #else
-       boost::char_separator<char> sep(delim.c_str());
-       boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
-       return vector<string>(tokens.begin(), tokens.end());
+       typedef boost::char_separator<typename String::value_type> Separator;
+       typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
+       Separator sep(delim.c_str());
+       Tokenizer tokens(str, sep);
+       return vector<String>(tokens.begin(), tokens.end());
 #endif
 }
 
+}
+
+
+vector<string> const getVectorFromString(string const & str,
+                                         string const & delim)
+{
+       return getVectorFromStringT<string>(str, delim);
+}
+
+
+vector<docstring> const getVectorFromString(docstring const & str,
+                                            docstring const & delim)
+{
+       return getVectorFromStringT<docstring>(str, delim);
+}
+
 
 // the same vice versa
 string const getStringFromVector(vector<string> const & vec,