]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.cpp
add generic helper class for calling functions in gui thread
[lyx.git] / src / support / lstrings.cpp
index ad1b961f77cb28b95b88bd65e2284a98fec19e0d..ed2b9f9cafffc9b6537186a12eaf260bcb5b0c13 100644 (file)
@@ -3,8 +3,9 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Lars Gullik Bjønnes
+ * \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 <cstdio>
 #include <algorithm>
 
 using namespace std;
@@ -89,6 +94,14 @@ bool isLetterChar(char_type c)
 }
 
 
+bool isLower(char_type c)
+{
+       if (!is_utf16(c))
+               return false;
+       return ucs4_to_qchar(c).isLower();
+}
+
+
 bool isAlphaASCII(char_type c)
 {
        return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
@@ -302,6 +315,23 @@ bool isStrDbl(string const & str)
 }
 
 
+bool hasDigit(docstring const & str)
+{
+       if (str.empty())
+               return false;
+
+       docstring::const_iterator cit = str.begin();
+       docstring::const_iterator const end = str.end();
+       for (; cit != end; ++cit) {
+               if (*cit == ' ')
+                       continue;
+               if (isdigit((*cit)))
+                       return true;
+       }
+       return false;
+}
+
+
 static bool isHexChar(char_type c)
 {
        return c == '0' ||
@@ -402,6 +432,16 @@ char_type uppercase(char_type c)
 }
 
 
+bool isLowerCase(char_type ch) {
+       return lowercase(ch) == ch;
+}
+
+
+bool isUpperCase(char_type ch) {
+       return uppercase(ch) == ch;
+}
+
+
 namespace {
 
 // since we cannot use tolower and toupper directly in the
@@ -491,7 +531,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 +553,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 +641,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
@@ -609,6 +676,7 @@ basic_string<Ch> const subst_char(basic_string<Ch> const & a,
        return tmp;
 }
 
+
 /// Substitute all \a oldchar with \a newchar
 docstring const subst_char(docstring const & a,
        docstring::value_type oldchar, docstring::value_type newchar)
@@ -640,6 +708,7 @@ String const subst_string(String const & a,
        return lstr;
 }
 
+
 docstring const subst_string(docstring const & a,
                docstring const & oldstr, docstring const & newstr)
 {
@@ -685,6 +754,19 @@ docstring const subst(docstring const & a,
 }
 
 
+/// Count all occurences of char \a chr inside \a str
+int count_char(docstring const & str, docstring::value_type chr)
+{
+       int count = 0;
+       docstring::const_iterator lit = str.begin();
+       docstring::const_iterator end = str.end();
+       for (; lit != end; ++lit)
+               if ((*lit) == chr)
+                       count++;
+       return count;
+}
+
+
 docstring const trim(docstring const & a, char const * p)
 {
        LASSERT(p, /**/);
@@ -859,6 +941,16 @@ string const rsplit(string const & a, string & piece, char delim)
 }
 
 
+docstring const rsplit(docstring const & a, char_type delim)
+{
+       docstring tmp;
+       size_t i = a.rfind(delim);
+       if (i != string::npos)
+               tmp = a.substr(i + 1);
+       return tmp;
+}
+
+
 docstring const escape(docstring const & lab)
 {
        char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
@@ -888,10 +980,88 @@ docstring const escape(docstring const & lab)
 }
 
 
+namespace {
+
+// this doesn't check whether str is empty, so do that first.
+vector<docstring> wrapToVec(docstring const & str, int ind,
+                           size_t const width)
+{
+       docstring s = trim(str);
+       if (s.empty())
+               return vector<docstring>();
+
+       docstring indent;
+       if (ind < 0) {
+               indent.insert(0, -ind, ' ');
+               ind = 0;
+       } else if (ind > 0)
+               s.insert(0, ind, ' ');
+
+       vector<docstring> retval;
+       while (s.size() > width) {
+               // find the last space within the first 'width' chars
+               size_t const i = s.find_last_of(' ', width - 1);
+               if (i == docstring::npos || i <= size_t(ind)) {
+                       // no space found
+                       s = s.substr(0, width - 3) + "...";
+                       break;
+               }
+               retval.push_back(s.substr(0, i));
+               s = indent + s.substr(i);
+               ind = indent.size();
+       }
+       if (!s.empty())
+               retval.push_back(s);
+       return retval;
+}
+
+}
+
+
+docstring wrap(docstring const & str, int const ind, size_t const width)
+{
+       docstring s = trim(str);
+       if (s.empty())
+               return docstring();
+
+       vector<docstring> const svec = wrapToVec(str, ind, width);
+       return getStringFromVector(svec, from_ascii("\n"));
+}
+
+
+docstring wrapParas(docstring const & str, int const indent,
+                   size_t const width, size_t const maxlines)
+{
+       if (str.empty())
+               return docstring();
+
+       vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
+       vector<docstring> retval;
+
+       vector<docstring>::const_iterator it = pars.begin();
+       vector<docstring>::const_iterator const en = pars.end();
+       for (; it != en; ++it) {
+               vector<docstring> tmp = wrapToVec(*it, indent, width);
+               size_t const nlines = tmp.size();
+               if (nlines == 0)
+                       continue;
+               size_t const curlines = retval.size();
+               if (maxlines > 0 && curlines + nlines >= maxlines) {
+                       tmp.resize(maxlines - curlines - 1);
+                       tmp.push_back(from_ascii("..."));
+               }
+               retval.insert(retval.end(), tmp.begin(), tmp.end());
+               if (maxlines > 0 && retval.size() >= maxlines)
+                       break;
+       }
+       return getStringFromVector(retval, from_ascii("\n"));
+}
+
+
 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 +1078,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);
@@ -923,39 +1093,54 @@ getVectorFromStringT(String const & str, String const & delim)
 #endif
 }
 
+
+template<typename String> const String
+       getStringFromVector(vector<String> const & vec, String const & delim)
+{
+       String str;
+       typename vector<String>::const_iterator it = vec.begin();
+       typename vector<String>::const_iterator en = vec.end();
+       for (; it != en; ++it) {
+               String item = trim(*it);
+               if (item.empty())
+                       continue;
+               if (!str.empty())
+                       str += delim;
+               str += item;
+       }
+       return str;
+}
+
 } // namespace anon
 
 
 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);
 }
 
 
-// the same vice versa
 string const getStringFromVector(vector<string> const & vec,
                                 string const & delim)
 {
-       string str;
-       int i = 0;
-       for (vector<string>::const_iterator it = vec.begin();
-            it != vec.end(); ++it) {
-               string item = trim(*it);
-               if (item.empty())
-                       continue;
-               if (i++ > 0)
-                       str += delim;
-               str += item;
-       }
-       return str;
+       return getStringFromVector<string>(vec, delim);
+}
+
+
+docstring const getStringFromVector(vector<docstring> const & vec,
+                                   docstring const & delim)
+{
+       return getStringFromVector<docstring>(vec, delim);
 }
 
 
@@ -971,27 +1156,6 @@ int findToken(char const * const str[], string const & search_token)
 }
 
 
-docstring const externalLineEnding(docstring const & str)
-{
-#if defined(__APPLE__)
-       // The MAC clipboard uses \r for lineendings, and we use \n
-       return subst(str, '\n', '\r');
-#elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
-       // Windows clipboard uses \r\n for lineendings, and we use \n
-       return subst(str, from_ascii("\n"), from_ascii("\r\n"));
-#else
-       return str;
-#endif
-}
-
-
-docstring const internalLineEnding(docstring const & str)
-{
-       docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
-       return subst(s, '\r', '\n');
-}
-
-
 template<>
 docstring bformat(docstring const & fmt, int arg1)
 {
@@ -1048,6 +1212,17 @@ docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
 }
 
 
+template<>
+docstring bformat(docstring const & fmt, docstring arg1, int arg2)
+{
+       LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
+       LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
+       docstring str = subst(fmt, from_ascii("%1$s"), arg1);
+       str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
+       return subst(str, from_ascii("%%"), from_ascii("%"));
+}
+
+
 template<>
 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
 {