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 sal_uInt32 utf16String[] = { 0x1D7FE /* 8 */, 0x1D7F7 /* 1 */};
73 s1 = OUString(utf16String, 2);
74 CPPUNIT_ASSERT_EQUAL(sal_uInt32(81), comphelper::string::decimalStringToNumber(s1));
75 }
76
testIsdigitAsciiString()77 void TestString::testIsdigitAsciiString()
78 {
79 OString s1("1234");
80 CPPUNIT_ASSERT_EQUAL(true, comphelper::string::isdigitAsciiString(s1));
81
82 OString s2("1A34");
83 CPPUNIT_ASSERT_EQUAL(false, comphelper::string::isdigitAsciiString(s2));
84
85 OString s3;
86 CPPUNIT_ASSERT_EQUAL(true, comphelper::string::isdigitAsciiString(s3));
87 }
88
89 using namespace ::com::sun::star;
90
91 class testCollator : public cppu::WeakImplHelper< i18n::XCollator >
92 {
93 public:
compareSubstring(const OUString & str1,sal_Int32 off1,sal_Int32 len1,const OUString & str2,sal_Int32 off2,sal_Int32 len2)94 virtual sal_Int32 SAL_CALL compareSubstring(
95 const OUString& str1, sal_Int32 off1, sal_Int32 len1,
96 const OUString& str2, sal_Int32 off2, sal_Int32 len2) override
97 {
98 return str1.copy(off1, len1).compareTo(str2.copy(off2, len2));
99 }
compareString(const OUString & str1,const OUString & str2)100 virtual sal_Int32 SAL_CALL compareString(
101 const OUString& str1,
102 const OUString& str2) override
103 {
104 return str1.compareTo(str2);
105 }
loadDefaultCollator(const lang::Locale &,sal_Int32)106 virtual sal_Int32 SAL_CALL loadDefaultCollator(const lang::Locale&, sal_Int32) override {return 0;}
loadCollatorAlgorithm(const OUString &,const lang::Locale &,sal_Int32)107 virtual sal_Int32 SAL_CALL loadCollatorAlgorithm(const OUString&,
108 const lang::Locale&, sal_Int32) override {return 0;}
loadCollatorAlgorithmWithEndUserOption(const OUString &,const lang::Locale &,const uno::Sequence<sal_Int32> &)109 virtual void SAL_CALL loadCollatorAlgorithmWithEndUserOption(const OUString&,
110 const lang::Locale&, const uno::Sequence< sal_Int32 >&) override {}
listCollatorAlgorithms(const lang::Locale &)111 virtual uno::Sequence< OUString > SAL_CALL listCollatorAlgorithms(const lang::Locale&) override
112 {
113 return uno::Sequence< OUString >();
114 }
listCollatorOptions(const OUString &)115 virtual uno::Sequence< sal_Int32 > SAL_CALL listCollatorOptions(const OUString&) override
116 {
117 return uno::Sequence< sal_Int32 >();
118 }
119 };
120
121 #define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
122
123 class testBreakIterator : public cppu::WeakImplHelper< i18n::XBreakIterator >
124 {
125 public:
nextCharacters(const OUString &,sal_Int32,const lang::Locale &,sal_Int16,sal_Int32,sal_Int32 &)126 virtual sal_Int32 SAL_CALL nextCharacters( const OUString&, sal_Int32,
127 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 &)128 virtual sal_Int32 SAL_CALL previousCharacters( const OUString&, sal_Int32,
129 const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& ) override {return -1;}
130
previousWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)131 virtual i18n::Boundary SAL_CALL previousWord( const OUString&, sal_Int32,
132 const lang::Locale&, sal_Int16) override
133 { return i18n::Boundary(); }
nextWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)134 virtual i18n::Boundary SAL_CALL nextWord( const OUString&, sal_Int32,
135 const lang::Locale&, sal_Int16) override
136 { return i18n::Boundary(); }
getWordBoundary(const OUString &,sal_Int32,const lang::Locale &,sal_Int16,sal_Bool)137 virtual i18n::Boundary SAL_CALL getWordBoundary( const OUString&, sal_Int32,
138 const lang::Locale&, sal_Int16, sal_Bool ) override
139 { return i18n::Boundary(); }
140
isBeginWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)141 virtual sal_Bool SAL_CALL isBeginWord( const OUString&, sal_Int32,
142 const lang::Locale&, sal_Int16 ) override
143 { return false; }
isEndWord(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)144 virtual sal_Bool SAL_CALL isEndWord( const OUString&, sal_Int32,
145 const lang::Locale& , sal_Int16 ) override
146 { return false; }
getWordType(const OUString &,sal_Int32,const lang::Locale &)147 virtual sal_Int16 SAL_CALL getWordType( const OUString&, sal_Int32,
148 const lang::Locale& ) override
149 { return 0; }
150
beginOfSentence(const OUString &,sal_Int32,const lang::Locale &)151 virtual sal_Int32 SAL_CALL beginOfSentence( const OUString&, sal_Int32,
152 const lang::Locale& ) override
153 { return 0; }
endOfSentence(const OUString & rText,sal_Int32,const lang::Locale &)154 virtual sal_Int32 SAL_CALL endOfSentence( const OUString& rText, sal_Int32,
155 const lang::Locale& ) override
156 { return rText.getLength(); }
157
getLineBreak(const OUString &,sal_Int32,const lang::Locale &,sal_Int32,const i18n::LineBreakHyphenationOptions &,const i18n::LineBreakUserOptions &)158 virtual i18n::LineBreakResults SAL_CALL getLineBreak( const OUString&, sal_Int32,
159 const lang::Locale&, sal_Int32,
160 const i18n::LineBreakHyphenationOptions&,
161 const i18n::LineBreakUserOptions&) override
162 {
163 return i18n::LineBreakResults();
164 }
165
getScriptType(const OUString &,sal_Int32)166 virtual sal_Int16 SAL_CALL getScriptType( const OUString&, sal_Int32 ) override { return -1; }
beginOfScript(const OUString &,sal_Int32,sal_Int16)167 virtual sal_Int32 SAL_CALL beginOfScript( const OUString&, sal_Int32,
168 sal_Int16 ) override { return -1; }
endOfScript(const OUString &,sal_Int32,sal_Int16)169 virtual sal_Int32 SAL_CALL endOfScript( const OUString&, sal_Int32,
170 sal_Int16 ) override { return -1; }
previousScript(const OUString &,sal_Int32,sal_Int16)171 virtual sal_Int32 SAL_CALL previousScript( const OUString&, sal_Int32,
172 sal_Int16 ) override { return -1; }
nextScript(const OUString &,sal_Int32,sal_Int16)173 virtual sal_Int32 SAL_CALL nextScript( const OUString&, sal_Int32,
174 sal_Int16 ) override { return -1; }
175
beginOfCharBlock(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)176 virtual sal_Int32 SAL_CALL beginOfCharBlock( const OUString&, sal_Int32,
177 const lang::Locale&, sal_Int16 ) override { return -1; }
endOfCharBlock(const OUString & rText,sal_Int32 nStartPos,const lang::Locale &,sal_Int16 CharType)178 virtual sal_Int32 SAL_CALL endOfCharBlock( const OUString& rText, sal_Int32 nStartPos,
179 const lang::Locale&, sal_Int16 CharType ) override
180 {
181 const sal_Unicode *pStr = rText.getStr()+nStartPos;
182 for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
183 {
184 if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
185 return nI;
186 else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
187 return nI;
188 ++pStr;
189 }
190 return -1;
191 }
previousCharBlock(const OUString &,sal_Int32,const lang::Locale &,sal_Int16)192 virtual sal_Int32 SAL_CALL previousCharBlock( const OUString&, sal_Int32,
193 const lang::Locale&, sal_Int16 ) override { return -1; }
nextCharBlock(const OUString & rText,sal_Int32 nStartPos,const lang::Locale &,sal_Int16 CharType)194 virtual sal_Int32 SAL_CALL nextCharBlock( const OUString& rText, sal_Int32 nStartPos,
195 const lang::Locale&, sal_Int16 CharType ) override
196 {
197 const sal_Unicode *pStr = rText.getStr()+nStartPos;
198 for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
199 {
200 if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
201 return nI;
202 else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
203 return nI;
204 ++pStr;
205 }
206 return -1;
207 }
208 };
209
testNatural()210 void TestString::testNatural()
211 {
212 using namespace comphelper::string;
213
214 uno::Reference< i18n::XCollator > xCollator(new testCollator);
215 uno::Reference< i18n::XBreakIterator > xBI(new testBreakIterator);
216
217 // --- Some generic tests to ensure we do not alter original behavior
218 // outside what we want
219 CPPUNIT_ASSERT_EQUAL(
220 static_cast<sal_Int32>(0), compareNatural("ABC", "ABC", xCollator, xBI, lang::Locale())
221 );
222 // Case sensitivity
223 CPPUNIT_ASSERT(
224 compareNatural("ABC", "abc", xCollator, xBI, lang::Locale()) < 0
225 );
226 // Reverse
227 CPPUNIT_ASSERT(
228 compareNatural("abc", "ABC", xCollator, xBI, lang::Locale()) > 0
229 );
230 // First shorter
231 CPPUNIT_ASSERT(
232 compareNatural("alongstring", "alongerstring", xCollator, xBI, lang::Locale()) > 0
233 );
234 // Second shorter
235 CPPUNIT_ASSERT(
236 compareNatural("alongerstring", "alongstring", xCollator, xBI, lang::Locale()) < 0
237 );
238 // -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
239 // That's why we originally made the patch
240 CPPUNIT_ASSERT(
241 compareNatural("Heading 9", "Heading 10", xCollator, xBI, lang::Locale()) < 0
242 );
243 // Original behavior
244 CPPUNIT_ASSERT(
245 OUString("Heading 9").compareTo("Heading 10") > 0
246 );
247 CPPUNIT_ASSERT(
248 compareNatural("Heading 10", "Heading 9", xCollator, xBI, lang::Locale()) > 0
249 );
250 // Harder
251 CPPUNIT_ASSERT(
252 compareNatural("July, the 4th", "July, the 10th", xCollator, xBI, lang::Locale()) < 0
253 );
254 CPPUNIT_ASSERT(
255 OUString("July, the 4th").compareTo("July, the 10th") > 0
256 );
257 CPPUNIT_ASSERT(
258 compareNatural("July, the 10th", "July, the 4th", xCollator, xBI, lang::Locale()) > 0
259 );
260 // Hardest
261 CPPUNIT_ASSERT(
262 compareNatural("abc08", "abc010", xCollator, xBI, lang::Locale()) < 0
263 );
264 CPPUNIT_ASSERT(
265 OUString("abc08").compareTo("abc010") > 0
266 );
267 CPPUNIT_ASSERT(
268 compareNatural("abc010", "abc08", xCollator, xBI, lang::Locale()) > 0
269 );
270 CPPUNIT_ASSERT_EQUAL(
271 static_cast<sal_Int32>(0), compareNatural("apple10apple", "apple10apple", xCollator, xBI, lang::Locale())
272 );
273 }
274
testStripStart()275 void TestString::testStripStart()
276 {
277 OString aIn("abc");
278 OString aOut;
279
280 aOut = ::comphelper::string::stripStart(aIn, 'b');
281 CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
282
283 aOut = ::comphelper::string::stripStart(aIn, 'a');
284 CPPUNIT_ASSERT_EQUAL(OString("bc"), aOut);
285
286 aIn = "aaa";
287 aOut = ::comphelper::string::stripStart(aIn, 'a');
288 CPPUNIT_ASSERT(aOut.isEmpty());
289
290 aIn = "aba";
291 aOut = ::comphelper::string::stripStart(aIn, 'a');
292 CPPUNIT_ASSERT_EQUAL(OString("ba"), aOut);
293 }
294
testStripEnd()295 void TestString::testStripEnd()
296 {
297 OString aIn("abc");
298 OString aOut;
299
300 aOut = ::comphelper::string::stripEnd(aIn, 'b');
301 CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
302
303 aOut = ::comphelper::string::stripEnd(aIn, 'c');
304 CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
305
306 aIn = "aaa";
307 aOut = ::comphelper::string::stripEnd(aIn, 'a');
308 CPPUNIT_ASSERT(aOut.isEmpty());
309
310 aIn = "aba";
311 aOut = ::comphelper::string::stripEnd(aIn, 'a');
312 CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
313 }
314
testStrip()315 void TestString::testStrip()
316 {
317 OString aIn("abc");
318 OString aOut;
319
320 aOut = ::comphelper::string::strip(aIn, 'b');
321 CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
322
323 aOut = ::comphelper::string::strip(aIn, 'c');
324 CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
325
326 aIn = "aaa";
327 aOut = ::comphelper::string::strip(aIn, 'a');
328 CPPUNIT_ASSERT(aOut.isEmpty());
329
330 aIn = "aba";
331 aOut = ::comphelper::string::strip(aIn, 'a');
332 CPPUNIT_ASSERT_EQUAL(OString("b"), aOut);
333 }
334
testToken()335 void TestString::testToken()
336 {
337 OString aIn("10.11.12");
338 OString aOut;
339
340 aOut = aIn.getToken(-1, '.');
341 CPPUNIT_ASSERT(aOut.isEmpty());
342
343 aOut = aIn.getToken(0, '.');
344 CPPUNIT_ASSERT_EQUAL(OString("10"), aOut);
345
346 aOut = aIn.getToken(1, '.');
347 CPPUNIT_ASSERT_EQUAL(OString("11"), aOut);
348
349 aOut = aIn.getToken(2, '.');
350 CPPUNIT_ASSERT_EQUAL(OString("12"), aOut);
351
352 aOut = aIn.getToken(3, '.');
353 CPPUNIT_ASSERT(aOut.isEmpty());
354 }
355
testTokenCount()356 void TestString::testTokenCount()
357 {
358 OString aIn("10.11.12");
359 sal_Int32 nOut;
360
361 nOut = ::comphelper::string::getTokenCount(aIn, '.');
362 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), nOut);
363
364 nOut = ::comphelper::string::getTokenCount(aIn, 'X');
365 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nOut);
366
367 nOut = ::comphelper::string::getTokenCount(OString(), 'X');
368 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), nOut);
369 }
370
testReverseString()371 void TestString::testReverseString()
372 {
373 OString aIn("ABC");
374 OString aOut = ::comphelper::string::reverseString(aIn);
375
376 CPPUNIT_ASSERT_EQUAL(OString("CBA"), aOut);
377 }
378
testSplit()379 void TestString::testSplit()
380 {
381 OUString aIn("CTRL+ALT+F1");
382 std::vector<OUString> aRet = ::comphelper::string::split(aIn, '+');
383 CPPUNIT_ASSERT_EQUAL(size_t(3), aRet.size());
384 CPPUNIT_ASSERT_EQUAL(OUString("CTRL"), aRet[0]);
385 CPPUNIT_ASSERT_EQUAL(OUString("ALT"), aRet[1]);
386 CPPUNIT_ASSERT_EQUAL(OUString("F1"), aRet[2]);
387 }
388
testRemoveAny()389 void TestString::testRemoveAny()
390 {
391 using namespace ::comphelper::string;
392 OUString in("abcAAAbbC");
393 sal_Unicode const test1 [] = { 'a', 0 };
394 CPPUNIT_ASSERT_EQUAL(OUString("bcAAAbbC"), removeAny(in, test1));
395 sal_Unicode const test2 [] = { 0 };
396 CPPUNIT_ASSERT_EQUAL(in, removeAny(in, test2));
397 sal_Unicode const test3 [] = { 'A', 0 };
398 CPPUNIT_ASSERT_EQUAL(OUString("abcbbC"), removeAny(in, test3));
399 sal_Unicode const test4 [] = { 'A', 'a', 0 };
400 CPPUNIT_ASSERT_EQUAL(OUString("bcbbC"), removeAny(in, test4));
401 sal_Unicode const test5 [] = { 'C', 0 };
402 CPPUNIT_ASSERT_EQUAL(OUString("abcAAAbb"), removeAny(in, test5));
403 sal_Unicode const test6 [] = { 'X', 0 };
404 CPPUNIT_ASSERT_EQUAL(in, removeAny(in, test6));
405 sal_Unicode const test7 [] = { 'A', 'B', 'C', 'a', 'b', 'c', 0 };
406 CPPUNIT_ASSERT_EQUAL(OUString(), removeAny(in, test7));
407 }
408
409 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
410
411 }
412
413 CPPUNIT_PLUGIN_IMPLEMENT();
414
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
416