]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.cpp
Maintain plain layout for separating paragraphs when switching layouts (#11936)
[lyx.git] / src / support / lstrings.cpp
index 4a12c34b4daec5fb2767ccec63a4ae4626859305..df356ba9937fa1fcbd93394f482b98d39fbaf356 100644 (file)
@@ -146,7 +146,7 @@ bool isSpace(char_type c)
 {
        if (!is_utf16(c)) {
                // assume that no non-utf16 character is a space
-               // c outside the UCS4 range is catched as well
+               // c outside the UCS4 range is caught as well
                return false;
        }
        QChar const qc = ucs4_to_qchar(c);
@@ -158,12 +158,32 @@ bool isNumber(char_type c)
 {
        if (!is_utf16(c))
                // assume that no non-utf16 character is a numeral
-               // c outside the UCS4 range is catched as well
+               // c outside the UCS4 range is caught as well
                return false;
        return ucs4_to_qchar(c).isNumber();
 }
 
 
+bool isCommonNumberSeparator(char_type c)
+{
+       if (!is_utf16(c))
+               // assume that no non-utf16 character is a numeral
+               // c outside the UCS4 range is caught as well
+               return false;
+       return ucs4_to_qchar(c).direction() == QChar::DirCS;
+}
+
+
+bool isEuropeanNumberTerminator(char_type c)
+{
+       if (!is_utf16(c))
+               // assume that no non-utf16 character is a numeral
+               // c outside the UCS4 range is caught as well
+               return false;
+       return ucs4_to_qchar(c).direction() == QChar::DirET;
+}
+
+
 bool isDigitASCII(char_type c)
 {
        return '0' <= c && c <= '9';
@@ -186,7 +206,7 @@ bool isOpenPunctuation(char_type c)
 {
        if (!is_utf16(c)) {
                // assume that no non-utf16 character is an op
-               // c outside the UCS4 range is catched as well
+               // c outside the UCS4 range is caught as well
                return false;
        }
        QChar const qc = ucs4_to_qchar(c);
@@ -386,13 +406,13 @@ bool isHexChar(char_type c)
 
 bool isHex(docstring const & str)
 {
-       int index = 0;
+       size_t index = 0;
 
        if (str.length() > 2 && str[0] == '0' &&
            (str[1] == 'x' || str[1] == 'X'))
                index = 2;
 
-       int const len = str.length();
+       size_t const len = str.length();
 
        for (; index < len; ++index) {
                if (!isHexChar(str[index]))
@@ -402,10 +422,10 @@ bool isHex(docstring const & str)
 }
 
 
-int hexToInt(docstring const & str)
+unsigned int hexToInt(docstring const & str)
 {
        string s = to_ascii(str);
-       int h;
+       unsigned int h;
        sscanf(s.c_str(), "%x", &h);
        return h;
 }
@@ -413,8 +433,8 @@ int hexToInt(docstring const & str)
 
 bool isAscii(docstring const & str)
 {
-       int const len = str.length();
-       for (int i = 0; i < len; ++i)
+       size_t const len = str.length();
+       for (size_t i = 0; i < len; ++i)
                if (str[i] >= 0x80)
                        return false;
        return true;
@@ -423,8 +443,8 @@ bool isAscii(docstring const & str)
 
 bool isAscii(string const & str)
 {
-       int const len = str.length();
-       for (int i = 0; i < len; ++i)
+       size_t const len = str.length();
+       for (size_t i = 0; i < len; ++i)
                if (static_cast<unsigned char>(str[i]) >= 0x80)
                        return false;
        return true;
@@ -870,7 +890,7 @@ String const subst_string(String const & a,
        size_t const olen = oldstr.length();
        while ((i = lstr.find(oldstr, i)) != string::npos) {
                lstr.replace(i, olen, newstr);
-               i += newstr.length(); // We need to be sure that we dont
+               i += newstr.length(); // We need to be sure that we don't
                // use the same i over and over again.
        }
        return lstr;
@@ -886,7 +906,7 @@ docstring const subst_string(docstring const & a,
        size_t const olen = oldstr.length();
        while ((i = lstr.find(oldstr, i)) != string::npos) {
                lstr.replace(i, olen, newstr);
-               i += newstr.length(); // We need to be sure that we dont
+               i += newstr.length(); // We need to be sure that we don't
                // use the same i over and over again.
        }
        return lstr;
@@ -1213,13 +1233,21 @@ docstring const protectArgument(docstring & arg, char const l,
 }
 
 
-bool truncateWithEllipsis(docstring & str, size_t const len)
+bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
 {
        if (str.size() <= len)
                return false;
-       str.resize(len);
-       if (len > 0)
-               str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
+       if (mid && len > 0) {
+               size_t const hlen = len / 2;
+               docstring suffix = str.substr(str.size() - hlen);
+               str.resize(hlen);
+               str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
+               str += suffix;
+       } else {
+               str.resize(len);
+               if (len > 0)
+                       str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
+       }
        return true;
 }
 
@@ -1433,9 +1461,15 @@ std::string formatFPNumber(double x)
 
 docstring to_percent_encoding(docstring const & in, docstring const & ex)
 {
-       QByteArray input = toqstr(in).toUtf8();
-       QByteArray excludes = toqstr(ex).toUtf8();
-       return qstring_to_ucs4(QString(input.toPercentEncoding(excludes)));
+       QByteArray input = to_utf8(in).c_str();
+       QByteArray excludes = to_utf8(ex).c_str();
+       return from_utf8(string(input.toPercentEncoding(excludes).data()));
+}
+
+
+string from_percent_encoding(string const & in)
+{
+       return QByteArray::fromPercentEncoding(in.c_str()).data();
 }
 
 
@@ -1455,7 +1489,7 @@ docstring bformat(docstring const & fmt, long arg1)
 }
 
 
-#ifdef LYX_USE_LONG_LONG
+#ifdef HAVE_LONG_LONG_INT
 docstring bformat(docstring const & fmt, long long arg1)
 {
        LATTEST(contains(fmt, from_ascii("%1$d")));
@@ -1555,5 +1589,22 @@ docstring bformat(docstring const & fmt,
        return subst(str, from_ascii("%%"), from_ascii("%"));
 }
 
+docstring bformat(docstring const & fmt, docstring const & arg1,
+                                 docstring const & arg2, docstring const & arg3,
+                                 docstring const & arg4, docstring const & arg5)
+{
+       LATTEST(contains(fmt, from_ascii("%1$s")));
+       LATTEST(contains(fmt, from_ascii("%2$s")));
+       LATTEST(contains(fmt, from_ascii("%3$s")));
+       LATTEST(contains(fmt, from_ascii("%4$s")));
+       LATTEST(contains(fmt, from_ascii("%5$s")));
+       docstring str = subst(fmt, from_ascii("%1$s"), arg1);
+       str = subst(str, from_ascii("%2$s"), arg2);
+       str = subst(str, from_ascii("%3$s"), arg3);
+       str = subst(str, from_ascii("%4$s"), arg4);
+       str = subst(str, from_ascii("%5$s"), arg5);
+       return subst(str, from_ascii("%%"), from_ascii("%"));
+}
+
 } // namespace support
 } // namespace lyx