]> git.lyx.org Git - features.git/blobdiff - src/support/lstrings.cpp
Fix for bug 5053. There are still some serious problems here, though, as (a) "unknown...
[features.git] / src / support / lstrings.cpp
index ad1b961f77cb28b95b88bd65e2284a98fec19e0d..2bd5f4ea95c606954ca82639b5a3be6726947906 100644 (file)
@@ -5,6 +5,7 @@
  *
  * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
+ * \author Dekel Tsur
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "support/textutils.h"
 
 #include <boost/tokenizer.hpp>
-#include "support/assert.h"
+#include "support/lassert.h"
+
+#include <QString>
+#include <QVector>
 
 #include <algorithm>
 
@@ -491,7 +495,8 @@ bool prefixIs(docstring const & a, docstring const & pre)
 
 bool suffixIs(string const & a, char c)
 {
-       if (a.empty()) return false;
+       if (a.empty()) 
+               return false;
        return a[a.length() - 1] == c;
 }
 
@@ -512,6 +517,14 @@ bool suffixIs(string const & a, string const & suf)
 }
 
 
+bool suffixIs(docstring const & a, docstring const & suf)
+{
+       size_t const suflen = suf.length();
+       size_t const alen = a.length();
+       return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
+}
+
+
 bool containsOnly(string const & s, string const & cset)
 {
        return s.find_first_not_of(cset) == string::npos;
@@ -592,6 +605,24 @@ int tokenPos(string const & a, char delim, string const & tok)
 }
 
 
+// this could probably be faster and/or cleaner, but it seems to work (JMarc)
+// rewritten to use new string (Lgb)
+int tokenPos(docstring const & a, char_type delim, docstring const & tok)
+{
+       int i = 0;
+       docstring str = a;
+       docstring tmptok;
+
+       while (!str.empty()) {
+               str = split(str, tmptok, delim);
+               if (tok == tmptok)
+                       return i;
+               ++i;
+       }
+       return -1;
+}
+
+
 namespace {
 
 /// Substitute all \a oldchar with \a newchar
@@ -891,7 +922,7 @@ docstring const escape(docstring const & lab)
 namespace {
 
 template<typename String> vector<String> const
-getVectorFromStringT(String const & str, String const & delim)
+getVectorFromStringT(String const & str, String const & delim, bool keepempty)
 {
 // Lars would like this code to go, but for now his replacement (below)
 // doesn't fullfil the same function. I have, therefore, reactivated the
@@ -908,7 +939,7 @@ getVectorFromStringT(String const & str, String const & delim)
                        break;
                }
                String const key = trim(keys.substr(0, idx));
-               if (!key.empty())
+               if (!key.empty() || keepempty)
                        vec.push_back(key);
                size_t const start = idx + delim.size();
                keys = keys.substr(start);
@@ -927,16 +958,18 @@ getVectorFromStringT(String const & str, String const & delim)
 
 
 vector<string> const getVectorFromString(string const & str,
-                                        string const & delim)
+                                        string const & delim,
+                                        bool keepempty)
 {
-       return getVectorFromStringT<string>(str, delim);
+       return getVectorFromStringT<string>(str, delim, keepempty);
 }
 
 
 vector<docstring> const getVectorFromString(docstring const & str,
-                                           docstring const & delim)
+                                           docstring const & delim,
+                                           bool keepempty)
 {
-       return getVectorFromStringT<docstring>(str, delim);
+       return getVectorFromStringT<docstring>(str, delim, keepempty);
 }