1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <comphelper/string.hxx>
21 #include <cppuhelper/implbase.hxx>
22 #include <com/sun/star/i18n/CharType.hpp>
23 #include <com/sun/star/i18n/XBreakIterator.hpp>
24 #include <com/sun/star/i18n/XCollator.hpp>
25
26 #include <cppunit/TestAssert.h>
27 #include <cppunit/TestFixture.h>
28 #include <cppunit/extensions/HelperMacros.h>
29 #include <cppunit/plugin/TestPlugIn.h>
30 #include <rtl/string.hxx>
31 #include <rtl/ustring.hxx>
32
33 namespace {
34
35 class TestString: public CppUnit::TestFixture
36 {
37 public:
38 void testNatural();
39 void testStripStart();
40 void testStripEnd();
41 void testStrip();
42 void testToken();
43 void testTokenCount();
44 void testDecimalStringToNumber();
45 void testIsdigitAsciiString();
46 void testReverseString();
47 void testSplit();
48 void testRemoveAny();
49
50 CPPUNIT_TEST_SUITE(TestString);
51 CPPUNIT_TEST(testNatural);
52 CPPUNIT_TEST(testStripStart);
53 CPPUNIT_TEST(testStripEnd);
54 CPPUNIT_TEST(testStrip);
55 CPPUNIT_TEST(testToken);
56 CPPUNIT_TEST(testTokenCount);
57 CPPUNIT_TEST(testDecimalStringToNumber);
58 CPPUNIT_TEST(testIsdigitAsciiString);
59 CPPUNIT_TEST(testReverseString);
60 CPPUNIT_TEST(testSplit);
61 CPPUNIT_TEST(testRemoveAny);
62 CPPUNIT_TEST_SUITE_END();
63 };
64
testDecimalStringToNumber()65 void TestString::testDecimalStringToNumber()
66 {
67 OUString s1("1234");
68 CPPUNIT_ASSERT_EQUAL(sal_uInt32(1234), comphelper::string::decimalStringToNumber(s1));
69 s1 += u"\u07C6";
70 CPPUNIT_ASSERT_EQUAL(sal_uInt32(12346), comphelper::string::decimalStringToNumber(s1));
71 // Codepoints on 2 16bits words
72 s1 = u"\U0001D7FE\U0001D7F7"; // MATHEMATICAL MONOSPACE DIGIT EIGHT and ONE
73 CPPUNIT_ASSERT_EQUAL(sal_uInt32(81), comphelper::string::decimalStringToNumber(s1));
74 }
75
testIsdigitAsciiString()76 void TestString::testIsdigitAsciiString()
77 {
78 CPPUNIT_ASSERT_EQUAL(true, comphelper::string::isdigitAsciiString("1234"));
79
80 CPPUNIT_ASSERT_EQUAL(false, comphelper::string::isdigitAsciiString("1A34"));
81
82 CPPUNIT_ASSERT_EQUAL(true, comphelper::string::isdigitAsciiString(""));
83 }
84
85 using namespace ::com::sun::star;
86
87 class testCollator : public cppu::WeakImplHelper< i18n::XCollator >
88 {
89 public:
compareSubstring(const OUString & str1,sal_Int32 off1,sal_Int32 len1,const OUString & str2,sal_Int32 off2,sal_Int32 len2)90 virtual sal_Int32 SAL_CALL compareSubstring(
91 const OUString& str1, sal_Int32 off1, sal_Int32 len1,
92 const OUString& str2, sal_Int32 off2, sal_Int32 len2) override
93 {
94 return str1.copy(off1, len1).compareTo(str2.subView(off2, len2));
95 }
compareString(const OUString & str1,const OUString & str2)96 virtual sal_Int32 SAL_CALL compareString(
97 const OUString& str1,
98 const OUString& str2) override
99 {
100 return str1.compareTo(str2);
101 }
loadDefaultCollator(const lang::Locale &,sal_Int32)102 virtual sal_Int32 SAL_CALL loadDefaultCollator(const lang::Locale&, sal_Int32) override {return 0;}
loadCollatorAlgorithm(const OUString &,const lang::Locale &,sal_Int32)103 virtual sal_Int32 SAL_CALL loadCollatorAlgorithm(const OUString&,
104 const lang::Locale&, sal_Int32) override {return 0;}
loadCollatorAlgorithmWithEndUserOption(const OUString &,const lang::Locale &,const uno::Sequence<sal_Int32> &)105 virtual void SAL_CALL loadCollatorAlgorithmWithEndUserOption(const OUString&,
106 const lang::Locale&, const uno::Sequence< sal_Int32 >&) override {}
listCollatorAlgorithms(const lang::Locale &)107 virtual uno::Sequence< OUString > SAL_CALL listCollatorAlgorithms(const lang::Locale&) override
108 {
109 return uno::Sequence< OUString >();
110 }
listCollatorOptions(const OUString &)111 virtual uno::Sequence< sal_Int32 > SAL_CALL listCollatorOptions(const OUString&) override
112 {
113 return uno::Sequence< sal_Int32 >();
114 }
115 };
116
117 #define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
118
119 class testBreakIterator : public cppu::WeakImplHelper< i18n::XBreakIterator >
120 {
121 public:
nextCharacters(const OUString &,sal_Int32,const lang::Locale &,sal_Int16,sal_Int32,sal_Int32 &)122 virtual sal_Int32 SAL_CALL nextCharacters( const OUString&, sal_Int32,
123 const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& ) override {return -1;}
previousCharacters(const OUString &,sal_Int32,const lang::Locale &,sal_Int16,sal_Int32,sal_Int32 &)124 virtual sal_Int32 SAL_CALL previousCharacters( const OUString&, sal_Int32,
125 const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& ) override {return -1;}
126
previousWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)127 virtual i18n::Boundary SAL_CALL previousWord( const OUString&, sal_Int32,
128 const lang::Locale&, sal_Int16) override
129 { return i18n::Boundary(); }
nextWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)130 virtual i18n::Boundary SAL_CALL nextWord( const OUString&, sal_Int32,
131 const lang::Locale&, sal_Int16) override
132 { return i18n::Boundary(); }
getWordBoundary(const OUString &,sal_Int32,const lang::Locale &,sal_Int16,sal_Bool)133 virtual i18n::Boundary SAL_CALL getWordBoundary( const OUString&, sal_Int32,
134 const lang::Locale&, sal_Int16, sal_Bool ) override
135 { return i18n::Boundary(); }
136
isBeginWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)137 virtual sal_Bool SAL_CALL isBeginWord( const OUString&, sal_Int32,
138 const lang::Locale&, sal_Int16 ) override
139 { return false; }
isEndWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)140 virtual sal_Bool SAL_CALL isEndWord( const OUString&, sal_Int32,
141 const lang::Locale& , sal_Int16 ) override
142 { return false; }
getWordType(const OUString &,sal_Int32,const lang::Locale &)143 virtual sal_Int16 SAL_CALL getWordType( const OUString&, sal_Int32,
144 const lang::Locale& ) override
145 { return 0; }
146
beginOfSentence(const OUString &,sal_Int32,const lang::Locale &)147 virtual sal_Int32 SAL_CALL beginOfSentence( const OUString&, sal_Int32,
148 const lang::Locale& ) override
149 { return 0; }
endOfSentence(const OUString & rText,sal_Int32,const lang::Locale &)150 virtual sal_Int32 SAL_CALL endOfSentence( const OUString& rText, sal_Int32,
151 const lang::Locale& ) override
152 { return rText.getLength(); }
153
getLineBreak(const OUString &,sal_Int32,const lang::Locale &,sal_Int32,const i18n::LineBreakHyphenationOptions &,const i18n::LineBreakUserOptions &)154 virtual i18n::LineBreakResults SAL_CALL getLineBreak( const OUString&, sal_Int32,
155 const lang::Locale&, sal_Int32,
156 const i18n::LineBreakHyphenationOptions&,
157 const i18n::LineBreakUserOptions&) override
158 {
159 return i18n::LineBreakResults();
160 }
161
getScriptType(const OUString &,sal_Int32)162 virtual sal_Int16 SAL_CALL getScriptType( const OUString&, sal_Int32 ) override { return -1; }
beginOfScript(const OUString &,sal_Int32,sal_Int16)163 virtual sal_Int32 SAL_CALL beginOfScript( const OUString&, sal_Int32,
164 sal_Int16 ) override { return -1; }
endOfScript(const OUString &,sal_Int32,sal_Int16)165 virtual sal_Int32 SAL_CALL endOfScript( const OUString&, sal_Int32,
166 sal_Int16 ) override { return -1; }
previousScript(const OUString &,sal_Int32,sal_Int16)167 virtual sal_Int32 SAL_CALL previousScript( const OUString&, sal_Int32,
168 sal_Int16 ) override { return -1; }
nextScript(const OUString &,sal_Int32,sal_Int16)169 virtual sal_Int32 SAL_CALL nextScript( const OUString&, sal_Int32,
170 sal_Int16 ) override { return -1; }
171
beginOfCharBlock(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)172 virtual sal_Int32 SAL_CALL beginOfCharBlock( const OUString&, sal_Int32,
173 const lang::Locale&, sal_Int16 ) override { return -1; }
endOfCharBlock(const OUString & rText,sal_Int32 nStartPos,const lang::Locale &,sal_Int16 CharType)174 virtual sal_Int32 SAL_CALL endOfCharBlock( const OUString& rText, sal_Int32 nStartPos,
175 const lang::Locale&, sal_Int16 CharType ) override
176 {
177 const sal_Unicode *pStr = rText.getStr()+nStartPos;
178 for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
179 {
180 if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
181 return nI;
182 else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
183 return nI;
184 ++pStr;
185 }
186 return -1;
187 }
previousCharBlock(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)188 virtual sal_Int32 SAL_CALL previousCharBlock( const OUString&, sal_Int32,
189 const lang::Locale&, sal_Int16 ) override { return -1; }
nextCharBlock(const OUString & rText,sal_Int32 nStartPos,const lang::Locale &,sal_Int16 CharType)190 virtual sal_Int32 SAL_CALL nextCharBlock( const OUString& rText, sal_Int32 nStartPos,
191 const lang::Locale&, sal_Int16 CharType ) override
192 {
193 const sal_Unicode *pStr = rText.getStr()+nStartPos;
194 for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
195 {
196 if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
197 return nI;
198 else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
199 return nI;
200 ++pStr;
201 }
202 return -1;
203 }
204 };
205
testNatural()206 void TestString::testNatural()
207 {
208 using namespace comphelper::string;
209
210 uno::Reference< i18n::XCollator > xCollator(new testCollator);
211 uno::Reference< i18n::XBreakIterator > xBI(new testBreakIterator);
212
213 // --- Some generic tests to ensure we do not alter original behavior
214 // outside what we want
215 CPPUNIT_ASSERT_EQUAL(
216 static_cast<sal_Int32>(0), compareNatural("ABC", "ABC", xCollator, xBI, lang::Locale())
217 );
218 // Case sensitivity
219 CPPUNIT_ASSERT(
220 compareNatural("ABC", "abc", xCollator, xBI, lang::Locale()) < 0
221 );
222 // Reverse
223 CPPUNIT_ASSERT(
224 compareNatural("abc", "ABC", xCollator, xBI, lang::Locale()) > 0
225 );
226 // First shorter
227 CPPUNIT_ASSERT(
228 compareNatural("alongstring", "alongerstring", xCollator, xBI, lang::Locale()) > 0
229 );
230 // Second shorter
231 CPPUNIT_ASSERT(
232 compareNatural("alongerstring", "alongstring", xCollator, xBI, lang::Locale()) < 0
233 );
234 // -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
235 // That's why we originally made the patch
236 CPPUNIT_ASSERT(
237 compareNatural("Heading 9", "Heading 10", xCollator, xBI, lang::Locale()) < 0
238 );
239 // Original behavior
240 CPPUNIT_ASSERT(
241 OUString("Heading 9").compareTo(u"Heading 10") > 0
242 );
243 CPPUNIT_ASSERT(
244 compareNatural("Heading 10", "Heading 9", xCollator, xBI, lang::Locale()) > 0
245 );
246 // Harder
247 CPPUNIT_ASSERT(
248 compareNatural("July, the 4th", "July, the 10th", xCollator, xBI, lang::Locale()) < 0
249 );
250 CPPUNIT_ASSERT(
251 OUString("July, the 4th").compareTo(u"July, the 10th") > 0
252 );
253 CPPUNIT_ASSERT(
254 compareNatural("July, the 10th", "July, the 4th", xCollator, xBI, lang::Locale()) > 0
255 );
256 // Hardest
257 CPPUNIT_ASSERT(
258 compareNatural("abc08", "abc010", xCollator, xBI, lang::Locale()) < 0
259 );
260 CPPUNIT_ASSERT(
261 OUString("abc08").compareTo(u"abc010") > 0
262 );
263 CPPUNIT_ASSERT(
264 compareNatural("abc010", "abc08", xCollator, xBI, lang::Locale()) > 0
265 );
266 CPPUNIT_ASSERT_EQUAL(
267 static_cast<sal_Int32>(0), compareNatural("apple10apple", "apple10apple", xCollator, xBI, lang::Locale())
268 );
269 }
270
testStripStart()271 void TestString::testStripStart()
272 {
273 OString aIn("abc");
274 OString aOut;
275
276 aOut = ::comphelper::string::stripStart(aIn, 'b');
277 CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
278
279 aOut = ::comphelper::string::stripStart(aIn, 'a');
280 CPPUNIT_ASSERT_EQUAL(OString("bc"), aOut);
281
282 aIn = "aaa";
283 aOut = ::comphelper::string::stripStart(aIn, 'a');
284 CPPUNIT_ASSERT(aOut.isEmpty());
285
286 aIn = "aba";
287 aOut = ::comphelper::string::stripStart(aIn, 'a');
288 CPPUNIT_ASSERT_EQUAL(OString("ba"), aOut);
289 }
290
testStripEnd()291 void TestString::testStripEnd()
292 {
293 OString aIn("abc");
294 OString aOut;
295
296 aOut = ::comphelper::string::stripEnd(aIn, 'b');
297 CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
298
299 aOut = ::comphelper::string::stripEnd(aIn, 'c');
300 CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
301
302 aIn = "aaa";
303 aOut = ::comphelper::string::stripEnd(aIn, 'a');
304 CPPUNIT_ASSERT(aOut.isEmpty());
305
306 aIn = "aba";
307 aOut = ::comphelper::string::stripEnd(aIn, 'a');
308 CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
309 }
310
testStrip()311 void TestString::testStrip()
312 {
313 OString aIn("abc");
314 OString aOut;
315
316 aOut = ::comphelper::string::strip(aIn, 'b');
317 CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
318
319 aOut = ::comphelper::string::strip(aIn, 'c');
320 CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
321
322 aIn = "aaa";
323 aOut = ::comphelper::string::strip(aIn, 'a');
324 CPPUNIT_ASSERT(aOut.isEmpty());
325
326 aIn = "aba";
327 aOut = ::comphelper::string::strip(aIn, 'a');
328 CPPUNIT_ASSERT_EQUAL(OString("b"), aOut);
329 }
330
testToken()331 void TestString::testToken()
332 {
333 OString aIn("10.11.12");
334 OString aOut;
335
336 aOut = aIn.getToken(-1, '.');
337 CPPUNIT_ASSERT(aOut.isEmpty());
338
339 aOut = aIn.getToken(0, '.');
340 CPPUNIT_ASSERT_EQUAL(OString("10"), aOut);
341
342 aOut = aIn.getToken(1, '.');
343 CPPUNIT_ASSERT_EQUAL(OString("11"), aOut);
344
345 aOut = aIn.getToken(2, '.');
346 CPPUNIT_ASSERT_EQUAL(OString("12"), aOut);
347
348 aOut = aIn.getToken(3, '.');
349 CPPUNIT_ASSERT(aOut.isEmpty());
350 }
351
testTokenCount()352 void TestString::testTokenCount()
353 {
354 OString aIn("10.11.12");
355 sal_Int32 nOut;
356
357 nOut = ::comphelper::string::getTokenCount(aIn, '.');
358 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), nOut);
359
360 nOut = ::comphelper::string::getTokenCount(aIn, 'X');
361 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nOut);
362
363 nOut = ::comphelper::string::getTokenCount("", 'X');
364 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), nOut);
365 }
366
testReverseString()367 void TestString::testReverseString()
368 {
369 OString aOut = ::comphelper::string::reverseString("ABC");
370
371 CPPUNIT_ASSERT_EQUAL(OString("CBA"), aOut);
372 }
373
testSplit()374 void TestString::testSplit()
375 {
376 std::vector<OUString> aRet = ::comphelper::string::split("CTRL+ALT+F1", '+');
377 CPPUNIT_ASSERT_EQUAL(size_t(3), aRet.size());
378 CPPUNIT_ASSERT_EQUAL(OUString("CTRL"), aRet[0]);
379 CPPUNIT_ASSERT_EQUAL(OUString("ALT"), aRet[1]);
380 CPPUNIT_ASSERT_EQUAL(OUString("F1"), aRet[2]);
381 }
382
testRemoveAny()383 void TestString::testRemoveAny()
384 {
385 using namespace ::comphelper::string;
386 OUString in("abcAAAbbC");
387 sal_Unicode const test1 [] = { 'a', 0 };
388 CPPUNIT_ASSERT_EQUAL(OUString("bcAAAbbC"), removeAny(in, test1));
389 sal_Unicode const test2 [] = { 0 };
390 CPPUNIT_ASSERT_EQUAL(in, removeAny(in, test2));
391 sal_Unicode const test3 [] = { 'A', 0 };
392 CPPUNIT_ASSERT_EQUAL(OUString("abcbbC"), removeAny(in, test3));
393 sal_Unicode const test4 [] = { 'A', 'a', 0 };
394 CPPUNIT_ASSERT_EQUAL(OUString("bcbbC"), removeAny(in, test4));
395 sal_Unicode const test5 [] = { 'C', 0 };
396 CPPUNIT_ASSERT_EQUAL(OUString("abcAAAbb"), removeAny(in, test5));
397 sal_Unicode const test6 [] = { 'X', 0 };
398 CPPUNIT_ASSERT_EQUAL(in, removeAny(in, test6));
399 sal_Unicode const test7 [] = { 'A', 'B', 'C', 'a', 'b', 'c', 0 };
400 CPPUNIT_ASSERT_EQUAL(OUString(), removeAny(in, test7));
401 }
402
403 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
404
405 }
406
407 CPPUNIT_PLUGIN_IMPLEMENT();
408
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
410