summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2023-05-05 09:46:52 +0200
committerStephan Bergmann <sbergman@redhat.com>2023-05-05 11:30:12 +0200
commit28cc0bff10f5dcec0c7b698ae7ba275845b2cad1 (patch)
treecdcccaa2356b8bea6c5b919faa6cd497a72e2336 /comphelper
parentc1bd421eae5449a005f2ee0f01b3b4e72002296e (diff)
Break comphelper::adjustIndexToStartOfSurrogate out of o3tl::iterateCodePoints
...as what they do is orthogonal (and it turned out that the use case that motivated the addition of o3tl::iterateCodePoints in the first place needs them independently, anyway) Change-Id: Id33901a2f7ac627253654ee6d883305dcf5a456f Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151415 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/qa/string/test_string.cxx20
-rw-r--r--comphelper/source/misc/string.cxx9
2 files changed, 29 insertions, 0 deletions
diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx
index 58f9c3f63c16..974673ca2940 100644
--- a/comphelper/qa/string/test_string.cxx
+++ b/comphelper/qa/string/test_string.cxx
@@ -50,6 +50,7 @@ public:
void testReverseCodePoints();
void testSplit();
void testRemoveAny();
+ void testAdjustIndexToStartOfSurrogate();
CPPUNIT_TEST_SUITE(TestString);
CPPUNIT_TEST(testStripStart);
@@ -63,6 +64,7 @@ public:
CPPUNIT_TEST(testReverseCodePoints);
CPPUNIT_TEST(testSplit);
CPPUNIT_TEST(testRemoveAny);
+ CPPUNIT_TEST(testAdjustIndexToStartOfSurrogate);
CPPUNIT_TEST_SUITE_END();
};
@@ -237,6 +239,24 @@ void TestString::testRemoveAny()
CPPUNIT_ASSERT_EQUAL(OUString(), removeAny(in, test7));
}
+void TestString::testAdjustIndexToStartOfSurrogate() {
+ CPPUNIT_ASSERT_EQUAL(
+ sal_Int32(0),
+ comphelper::string::adjustIndexToStartOfSurrogate("", 0));
+ CPPUNIT_ASSERT_EQUAL(
+ sal_Int32(0),
+ comphelper::string::adjustIndexToStartOfSurrogate(u"\U00010000", 0));
+ CPPUNIT_ASSERT_EQUAL(
+ sal_Int32(0),
+ comphelper::string::adjustIndexToStartOfSurrogate(u"\U00010000", 1));
+ CPPUNIT_ASSERT_EQUAL(
+ sal_Int32(2),
+ comphelper::string::adjustIndexToStartOfSurrogate(u"\U00010000", 2));
+ CPPUNIT_ASSERT_EQUAL(
+ sal_Int32(1),
+ comphelper::string::adjustIndexToStartOfSurrogate(u"\xD800", 1));
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
}
diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx
index da5c8b92c05c..0fdd24c83d7e 100644
--- a/comphelper/source/misc/string.cxx
+++ b/comphelper/source/misc/string.cxx
@@ -19,6 +19,7 @@
#include <sal/config.h>
+#include <cassert>
#include <cstddef>
#include <string_view>
#include <utility>
@@ -679,6 +680,14 @@ OUString sanitizeStringSurrogates(const OUString& rString)
return rString;
}
+sal_Int32 adjustIndexToStartOfSurrogate(OUString const & string, sal_Int32 index) {
+ assert(index >= 0 && index <= string.getLength());
+ return
+ (index > 0 && rtl::isHighSurrogate(string[index - 1])
+ && index < string.getLength() && rtl::isLowSurrogate(string[index]))
+ ? index - 1 : index;
+}
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */