[poppler] config.h.cmake ConfigureChecks.cmake poppler/PageLabelInfo.cc poppler/PageLabelInfo_p.h qt5/tests qt6/tests

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Dec 30 21:44:37 UTC 2020


 ConfigureChecks.cmake             |    1 
 config.h.cmake                    |    3 --
 poppler/PageLabelInfo.cc          |    2 -
 poppler/PageLabelInfo_p.h         |   45 +++++++++++++++++++-------------------
 qt5/tests/check_pagelabelinfo.cpp |   10 +-------
 qt6/tests/check_pagelabelinfo.cpp |   10 +-------
 6 files changed, 28 insertions(+), 43 deletions(-)

New commits:
commit cab257727998251bb29e9f150fd5df69d08a2758
Author: Albert Astals Cid <aacid at kde.org>
Date:   Wed Dec 23 19:29:10 2020 +0100

    Stop using std::codecvt_utf16 in fromDecimal
    
    It's deprecated since C++17 and since we only care about numbers we can
    just remove the first byte of the utf16 and treat it as a regular string

diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake
index e672abbe..e0826caf 100644
--- a/ConfigureChecks.cmake
+++ b/ConfigureChecks.cmake
@@ -25,7 +25,6 @@ check_include_files(stdlib.h HAVE_STDLIB_H)
 check_include_files(sys/mman.h HAVE_SYS_MMAN_H)
 check_include_files(sys/stat.h HAVE_SYS_STAT_H)
 check_include_files(unistd.h HAVE_UNISTD_H)
-check_include_file_cxx(codecvt HAVE_CODECVT)
 
 check_function_exists(fseek64 HAVE_FSEEK64)
 check_symbol_exists(fseeko "stdio.h" HAVE_FSEEKO)
diff --git a/config.h.cmake b/config.h.cmake
index 2eaeb13f..1d66c9ba 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -108,9 +108,6 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #cmakedefine HAVE_UNISTD_H 1
 
-/* Define to 1 if you have the <codecvt> header file. */
-#cmakedefine HAVE_CODECVT
-
 /* Define to 1 if you have a big endian machine */
 #cmakedefine WORDS_BIGENDIAN 1
 
diff --git a/poppler/PageLabelInfo.cc b/poppler/PageLabelInfo.cc
index dde5f332..be4f8fbe 100644
--- a/poppler/PageLabelInfo.cc
+++ b/poppler/PageLabelInfo.cc
@@ -128,7 +128,7 @@ bool PageLabelInfo::labelToIndex(GooString *label, int *index) const
 
         switch (interval.style) {
         case Interval::Arabic:
-            std::tie(number, ok) = fromDecimal(str + prefixLen, str + strLen, strUnicode);
+            std::tie(number, ok) = fromDecimal(label->toStr().substr(prefixLen), strUnicode);
             if (ok && number - interval.first < interval.length) {
                 *index = interval.base + number - interval.first;
                 return true;
diff --git a/poppler/PageLabelInfo_p.h b/poppler/PageLabelInfo_p.h
index ada3b0f7..ce6f2fb2 100644
--- a/poppler/PageLabelInfo_p.h
+++ b/poppler/PageLabelInfo_p.h
@@ -21,36 +21,37 @@
 
 #include "config.h"
 
-#ifdef HAVE_CODECVT
-#    include <locale>
-#    include <codecvt>
-#endif
-
 #include "goo/GooString.h"
 #include "Error.h"
 
-static std::pair<int, bool> fromDecimal(const char *const begin, const char *const end, const bool unicode)
+static std::pair<int, bool> fromDecimal(const std::string &str, const bool unicode)
 {
-#ifdef HAVE_CODECVT
-    if (unicode) {
-        std::wstring_convert<std::codecvt_utf16<wchar_t>> converter("", L"");
-        const auto str = converter.from_bytes(begin, end);
-
-        // Skip BOM since wcstol seems unable to handle it.
-        const wchar_t *c_str = str.c_str();
-        if (*c_str == wchar_t { 0xfeff }) {
-            ++c_str;
+    if (unicode && (str.size() % 2 == 0)) {
+        if (GooString::hasUnicodeMarker(str)) {
+            // strip the marker if it is there
+            return fromDecimal(str.substr(2), true /*unicode*/);
         }
 
-        wchar_t *parsed;
-        const int number = std::wcstol(c_str, &parsed, 10);
-        if (parsed >= str.data() + str.size()) {
-            return std::make_pair(number, true);
+        // Since we only care about numbers here, the first byte needs to be
+        // 0 and second will be the actual ascii number, so we're going to reconstruct a
+        // non unicode string that then we will use strtol to "translate"
+        std::string newString;
+        bool allGood = true;
+        for (size_t i = 0; allGood && i < str.size(); i += 2) {
+            if (str[i] == 0) {
+                newString += str[i + 1];
+            } else {
+                allGood = false;
+            }
+        }
+
+        if (allGood) {
+            return fromDecimal(newString, false /*unicode*/);
         }
     }
-#else
-    (void)unicode;
-#endif
+
+    const char *const begin = str.data();
+    const char *const end = begin + str.size();
 
     char *parsed;
     const int number = std::strtol(begin, &parsed, 10);
diff --git a/qt5/tests/check_pagelabelinfo.cpp b/qt5/tests/check_pagelabelinfo.cpp
index 042f1cf9..520a5f9b 100644
--- a/qt5/tests/check_pagelabelinfo.cpp
+++ b/qt5/tests/check_pagelabelinfo.cpp
@@ -23,7 +23,7 @@ private slots:
 void TestPageLabelInfo::testFromDecimal()
 {
     std::string str { "2342" };
-    const auto res = fromDecimal(str.data(), str.data() + str.size(), false);
+    const auto res = fromDecimal(str, false);
     QCOMPARE(res.first, 2342);
     QCOMPARE(res.second, true);
 }
@@ -31,14 +31,8 @@ void TestPageLabelInfo::testFromDecimal()
 void TestPageLabelInfo::testFromDecimalUnicode()
 {
     std::unique_ptr<GooString> str(Poppler::QStringToUnicodeGooString(QString::fromLocal8Bit("2342")));
-    const auto res = fromDecimal(str->c_str(), str->c_str() + str->getLength(), str->hasUnicodeMarker());
-#ifndef HAVE_CODECVT
-    QEXPECT_FAIL("", "unicode text to index fails without codecvt", Continue);
-#endif
+    const auto res = fromDecimal(str->toStr(), str->hasUnicodeMarker());
     QCOMPARE(res.first, 2342);
-#ifndef HAVE_CODECVT
-    QEXPECT_FAIL("", "unicode text to index fails without codecvt", Continue);
-#endif
     QCOMPARE(res.second, true);
 }
 
diff --git a/qt6/tests/check_pagelabelinfo.cpp b/qt6/tests/check_pagelabelinfo.cpp
index 042f1cf9..520a5f9b 100644
--- a/qt6/tests/check_pagelabelinfo.cpp
+++ b/qt6/tests/check_pagelabelinfo.cpp
@@ -23,7 +23,7 @@ private slots:
 void TestPageLabelInfo::testFromDecimal()
 {
     std::string str { "2342" };
-    const auto res = fromDecimal(str.data(), str.data() + str.size(), false);
+    const auto res = fromDecimal(str, false);
     QCOMPARE(res.first, 2342);
     QCOMPARE(res.second, true);
 }
@@ -31,14 +31,8 @@ void TestPageLabelInfo::testFromDecimal()
 void TestPageLabelInfo::testFromDecimalUnicode()
 {
     std::unique_ptr<GooString> str(Poppler::QStringToUnicodeGooString(QString::fromLocal8Bit("2342")));
-    const auto res = fromDecimal(str->c_str(), str->c_str() + str->getLength(), str->hasUnicodeMarker());
-#ifndef HAVE_CODECVT
-    QEXPECT_FAIL("", "unicode text to index fails without codecvt", Continue);
-#endif
+    const auto res = fromDecimal(str->toStr(), str->hasUnicodeMarker());
     QCOMPARE(res.first, 2342);
-#ifndef HAVE_CODECVT
-    QEXPECT_FAIL("", "unicode text to index fails without codecvt", Continue);
-#endif
     QCOMPARE(res.second, true);
 }
 


More information about the poppler mailing list