[Libreoffice-commits] .: 3 commits - comphelper/inc comphelper/qa comphelper/source tools/inc tools/qa tools/source
Caolán McNamara
caolan at kemper.freedesktop.org
Mon Feb 14 05:24:43 PST 2011
comphelper/inc/comphelper/string.hxx | 38 ++++++++++++++
comphelper/qa/test_string.cxx | 68 ++++++++++++++++++++++++--
comphelper/source/misc/string.cxx | 55 +++++++++++++++++++++
tools/inc/tools/string.hxx | 2
tools/qa/makefile.mk | 3 -
tools/qa/test_strings.cxx | 88 ----------------------------------
tools/qa/urlobj/tools_urlobj_test.cxx | 3 +
tools/source/string/strimp.cxx | 69 --------------------------
8 files changed, 161 insertions(+), 165 deletions(-)
New commits:
commit 65d3e635ba4a0a609b22e9bb223b5a99a8f67106
Author: Caolán McNamara <caolanm at redhat.com>
Date: Mon Feb 14 11:22:58 2011 +0000
move this (cool) natural sort into comphelper
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx
index 46faf87..ca08e82 100644
--- a/comphelper/inc/comphelper/string.hxx
+++ b/comphelper/inc/comphelper/string.hxx
@@ -131,6 +131,44 @@ COMPHELPER_DLLPUBLIC ::rtl::OUString convertCommaSeparated(
COMPHELPER_DLLPUBLIC ::com::sun::star::uno::Sequence< ::rtl::OUString >
convertCommaSeparated( ::rtl::OUString const & i_rString );
+/**
+ Compares two strings using natural order.
+
+ For non digit characters, the comparison use the same algorithm as
+ rtl_str_compare. When a number is encountered during the comparison,
+ natural order is used. Thus, Heading 10 will be considered as greater
+ than Heading 2. Numerical comparison is done using decimal representation.
+
+ Beware that "MyString 001" and "MyString 1" will be considered as equal
+ since leading 0 are meaningless.
+
+ @param str the object to be compared.
+ @return 0 - if both strings are equal
+ < 0 - if this string is less than the string argument
+ > 0 - if this string is greater than the string argument
+*/
+COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const ::rtl::OUString &rLHS, const ::rtl::OUString &rRHS )
+ SAL_THROW(());
+
+/**
+ Compares two strings using natural order.
+
+ For non digit characters, the comparison use the same algorithm as
+ rtl_str_compare. When a number is encountered during the comparison,
+ natural order is used. Thus, Heading 10 will be considered as greater
+ than Heading 2. Numerical comparison is done using decimal representation.
+
+ Beware that "MyString 001" and "MyString 1" will be considered as equal
+ since leading 0 are meaningless.
+
+ @param str the object to be compared.
+ @return 0 - if both strings are equal
+ < 0 - if this string is less than the string argument
+ > 0 - if this string is greater than the string argument
+ */
+COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const ::rtl::OString &rLHS, const ::rtl::OString &rRHS )
+ SAL_THROW(());
+
} }
#endif
diff --git a/comphelper/qa/test_string.cxx b/comphelper/qa/test_string.cxx
index 7f15e96..ab13c88 100644
--- a/comphelper/qa/test_string.cxx
+++ b/comphelper/qa/test_string.cxx
@@ -42,16 +42,20 @@
namespace {
-class TestString: public CppUnit::TestFixture {
+class TestString: public CppUnit::TestFixture
+{
public:
void test();
+ void testNatural();
CPPUNIT_TEST_SUITE(TestString);
CPPUNIT_TEST(test);
+ CPPUNIT_TEST(testNatural);
CPPUNIT_TEST_SUITE_END();
};
-void TestString::test() {
+void TestString::test()
+{
rtl::OUString s1(RTL_CONSTASCII_USTRINGPARAM("foobarbar"));
sal_Int32 n1;
rtl::OUString s2(
@@ -80,10 +84,66 @@ void TestString::test() {
CPPUNIT_ASSERT(n3 == -1);
}
-CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
-
+void TestString::testNatural()
+{
+ using namespace comphelper::string;
+// --- Some generic tests to ensure we do not alter original behavior
+// outside what we want
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC")))) == 0
+ );
+ // Case sensitivity
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc")))) < 0
+ );
+ // Reverse
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC")))) > 0
+ );
+ // First shorter
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongstring"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongerstring")))) > 0
+ );
+ // Second shorter
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongerstring"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongstring")))) < 0
+ );
+// -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
+ // That's why we originally made the patch
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 9"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 10")))) < 0
+ );
+ // Original behavior
+ CPPUNIT_ASSERT(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 9"))).compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 10")))) > 0
+ );
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 10"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 9")))) > 0
+ );
+ // Harder
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 4th"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 10th")))) < 0
+ );
+ CPPUNIT_ASSERT(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 4th"))).compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 10th")))) > 0
+ );
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 10th"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 4th")))) > 0
+ );
+ // Hardest
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc08"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc010")))) < 0
+ );
+ CPPUNIT_ASSERT(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc08"))).compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc010")))) > 0
+ );
+ CPPUNIT_ASSERT(
+ compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc010"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc08")))) > 0
+ );
}
+CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
+}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx
index f0fc39d..63d93dd 100644
--- a/comphelper/source/misc/string.cxx
+++ b/comphelper/source/misc/string.cxx
@@ -123,6 +123,61 @@ rtl::OUString searchAndReplaceAsciiL(
return kws;
}
+#define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
+
+template<typename IMPL_RTL_STRCODE, typename IMPL_RTL_USTRCODE>
+ sal_Int32 SAL_CALL compareNaturalImpl(const IMPL_RTL_STRCODE* pStr1, const IMPL_RTL_STRCODE* pStr2)
+{
+ sal_Int32 nRet;
+ do {
+ while ( ((nRet = ((sal_Int32)(IMPL_RTL_USTRCODE(*pStr1)))-
+ ((sal_Int32)(IMPL_RTL_USTRCODE(*pStr2)))) == 0) &&
+ *pStr2 )
+ {
+ pStr1++;
+ pStr2++;
+ }
+
+ if(*pStr1 && *pStr2)
+ {
+ IMPL_RTL_STRCODE c1 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr1 );
+ IMPL_RTL_STRCODE c2 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr2 );
+ sal_Int64 number1 = 0;
+ sal_Int64 number2 = 0;
+ if(IS_DIGIT(c1) && IS_DIGIT(c2))
+ {
+ do
+ {
+ number1 = number1 * 10 + (c1 - '0');
+ pStr1++;
+ c1 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr1 );
+ } while(IS_DIGIT(c1));
+
+ do
+ {
+ number2 = number2 * 10 + (c2 - '0');
+ pStr2++;
+ c2 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr2 );
+ } while(IS_DIGIT(c2));
+
+ nRet = number1 - number2;
+ }
+ }
+ } while(nRet == 0 && *pStr1 && *pStr2);
+
+ return nRet;
+}
+
+sal_Int32 compareNatural( const ::rtl::OUString & rLHS, const ::rtl::OUString & rRHS ) SAL_THROW(())
+{
+ return compareNaturalImpl<sal_Unicode, sal_Unicode>(rLHS.pData->buffer, rRHS.pData->buffer);
+}
+
+sal_Int32 compareNatural( const ::rtl::OString & rLHS, const ::rtl::OString & rRHS ) SAL_THROW(())
+{
+ return compareNaturalImpl<sal_Char, unsigned char>(rLHS.pData->buffer, rRHS.pData->buffer);
+}
+
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit a871ec81d793c8599f3f40f96155a9196141b76e
Author: Caolán McNamara <caolanm at redhat.com>
Date: Mon Feb 14 11:22:26 2011 +0000
move this to comphelper
diff --git a/tools/inc/tools/string.hxx b/tools/inc/tools/string.hxx
index e6dc3f7..25cb2d4 100644
--- a/tools/inc/tools/string.hxx
+++ b/tools/inc/tools/string.hxx
@@ -301,7 +301,6 @@ public:
xub_StrLen nLen = STRING_LEN ) const;
StringCompare CompareTo( const sal_Char* pCharStr,
xub_StrLen nLen = STRING_LEN ) const;
- StringCompare CompareToNumeric( const ByteString& rStr ) const;
StringCompare CompareIgnoreCaseToAscii( const ByteString& rStr,
xub_StrLen nLen = STRING_LEN ) const;
StringCompare CompareIgnoreCaseToAscii( const sal_Char* pCharStr,
@@ -598,7 +597,6 @@ public:
xub_StrLen nLen = STRING_LEN ) const;
StringCompare CompareToAscii( const sal_Char* pAsciiStr,
xub_StrLen nLen = STRING_LEN ) const;
- StringCompare CompareToNumeric( const UniString& rStr ) const;
StringCompare CompareIgnoreCaseToAscii( const UniString& rStr,
xub_StrLen nLen = STRING_LEN ) const;
StringCompare CompareIgnoreCaseToAscii( const sal_Unicode* pCharStr,
diff --git a/tools/qa/makefile.mk b/tools/qa/makefile.mk
index 06c2cb1..e82b393 100644
--- a/tools/qa/makefile.mk
+++ b/tools/qa/makefile.mk
@@ -39,8 +39,7 @@ SHL1TARGET=test_tools
SHL1OBJS=\
$(SLO)$/pathutils.obj \
$(SLO)$/test_pathutils.obj \
- $(SLO)$/test_reversemap.obj \
- $(SLO)$/test_strings.obj
+ $(SLO)$/test_reversemap.obj
SHL1STDLIBS = $(TOOLSLIB) $(CPPUNITLIB) $(SALLIB)
SHL1VERSIONMAP = version.map
SHL1IMPLIB = i$(SHL1TARGET)
diff --git a/tools/qa/test_strings.cxx b/tools/qa/test_strings.cxx
deleted file mode 100644
index 90a2ce8..0000000
--- a/tools/qa/test_strings.cxx
+++ /dev/null
@@ -1,91 +0,0 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
-#include <cppunit/TestFixture.h>
-#include <cppunit/extensions/HelperMacros.h>
-#include <cppunit/plugin/TestPlugIn.h>
-
-#include <tools/string.hxx>
-
-namespace test {
- namespace unistring {
- /**
- * test::unistring::Compare Perform comparison functions
- * tests on UniString.
- */
- class Compare: public CppUnit::TestFixture
- {
- private:
- /**
- * Performs tests on natural comparison function
- */
- void testCompareToNumeric();
-
- CPPUNIT_TEST_SUITE(Compare);
- CPPUNIT_TEST(testCompareToNumeric);
- CPPUNIT_TEST_SUITE_END();
-
- };
- }
-}
-
-#define US_FROM_STRING(STRING) UniString((STRING), RTL_TEXTENCODING_UTF8)
-
-void test::unistring::Compare::testCompareToNumeric()
-{
-// --- Some generic tests to ensure we do not alter original behavior
-// outside what we want
- CPPUNIT_ASSERT(
- US_FROM_STRING("ABC").CompareToNumeric(US_FROM_STRING("ABC")) == COMPARE_EQUAL
- );
- // Case sensitivity
- CPPUNIT_ASSERT(
- US_FROM_STRING("ABC").CompareToNumeric(US_FROM_STRING("abc")) == COMPARE_LESS
- );
- // Reverse
- CPPUNIT_ASSERT(
- US_FROM_STRING("abc").CompareToNumeric(US_FROM_STRING("ABC")) == COMPARE_GREATER
- );
- // First shorter
- CPPUNIT_ASSERT(
- US_FROM_STRING("alongstring").CompareToNumeric(US_FROM_STRING("alongerstring")) == COMPARE_GREATER
- );
- // Second shorter
- CPPUNIT_ASSERT(
- US_FROM_STRING("alongerstring").CompareToNumeric(US_FROM_STRING("alongstring")) == COMPARE_LESS
- );
-// -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
- // That's why we originally made the patch
- CPPUNIT_ASSERT(
- US_FROM_STRING("Heading 9").CompareToNumeric(US_FROM_STRING("Heading 10")) == COMPARE_LESS
- );
- // Original behavior
- CPPUNIT_ASSERT(
- US_FROM_STRING("Heading 9").CompareTo(US_FROM_STRING("Heading 10")) == COMPARE_GREATER
- );
- CPPUNIT_ASSERT(
- US_FROM_STRING("Heading 10").CompareToNumeric(US_FROM_STRING("Heading 9")) == COMPARE_GREATER
- );
- // Harder
- CPPUNIT_ASSERT(
- US_FROM_STRING("July, the 4th").CompareToNumeric(US_FROM_STRING("July, the 10th")) == COMPARE_LESS
- );
- CPPUNIT_ASSERT(
- US_FROM_STRING("July, the 4th").CompareTo(US_FROM_STRING("July, the 10th")) == COMPARE_GREATER
- );
- CPPUNIT_ASSERT(
- US_FROM_STRING("July, the 10th").CompareToNumeric(US_FROM_STRING("July, the 4th")) == COMPARE_GREATER
- );
- // Hardest
- CPPUNIT_ASSERT(
- US_FROM_STRING("abc08").CompareToNumeric(US_FROM_STRING("abc010")) == COMPARE_LESS
- );
- CPPUNIT_ASSERT(
- US_FROM_STRING("abc08").CompareTo(US_FROM_STRING("abc010")) == COMPARE_GREATER
- );
- CPPUNIT_ASSERT(
- US_FROM_STRING("abc010").CompareToNumeric(US_FROM_STRING("abc08")) == COMPARE_GREATER
- );
-}
-
-CPPUNIT_TEST_SUITE_REGISTRATION(test::unistring::Compare);
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/source/string/strimp.cxx b/tools/source/string/strimp.cxx
index 01f2955..e77a4ac 100644
--- a/tools/source/string/strimp.cxx
+++ b/tools/source/string/strimp.cxx
@@ -30,8 +30,6 @@
// =======================================================================
-#define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
-
static sal_Int32 ImplStringCompare( const STRCODE* pStr1, const STRCODE* pStr2 )
{
sal_Int32 nRet;
@@ -63,55 +61,6 @@ static sal_Int32 ImplStringCompare( const STRCODE* pStr1, const STRCODE* pStr2,
return nRet;
}
-static sal_Int32 ImplStringCompareToNumeric( const STRCODE* pStr1, const STRCODE* pStr2 )
-{
- sal_Int32 nRet = 0;
- do
- {
- while ( ((nRet = ((sal_Int32)((STRCODEU)*pStr1))-
- ((sal_Int32)((STRCODEU)*pStr2))) == 0) &&
- *pStr2 )
- {
- pStr1++;
- pStr2++;
- }
-
- if(*pStr1 && *pStr2)
- {
- STRCODE c1 = ( *pStr1 );
- STRCODE c2 = ( *pStr2 );
- sal_Int64 number1 = 0;
- sal_Int64 number2 = 0;
- if(IS_DIGIT(c1) && IS_DIGIT(c2))
- {
- do
- {
- number1 = number1 * 10 + (c1 - '0');
- pStr1++;
- c1 = ( *pStr1 );
- }
- while(c1 && IS_DIGIT(c1));
-
- do
- {
- number2 = number2 * 10 + (c2 - '0');
- pStr2++;
- c2 = ( *pStr2 );
- }
- while(c2 && IS_DIGIT(c2));
-
- if(number1 != number2)
- {
- nRet = number1 - number2;
- }
- }
- }
- }
- while(nRet == 0 && *pStr1 && *pStr2);
-
- return nRet;
-}
-
// -----------------------------------------------------------------------
static sal_Int32 ImplStringCompareWithoutZero( const STRCODE* pStr1, const STRCODE* pStr2,
@@ -1328,24 +1277,6 @@ StringCompare STRING::CompareTo( const STRCODE* pCharStr, xub_StrLen nLen ) cons
return COMPARE_GREATER;
}
-StringCompare STRING::CompareToNumeric( const STRING& rStr) const
-{
- // ensure arguments' types
- DBG_CHKTHIS( STRING, DBGCHECKSTRING );
- DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
-
- if ( mpData == rStr.mpData )
- return COMPARE_EQUAL;
-
- sal_Int32 nCompare = ImplStringCompareToNumeric( mpData->maStr, rStr.mpData->maStr );
-
- if( nCompare == 0)
- return COMPARE_EQUAL;
- else if(nCompare < 0 )
- return COMPARE_LESS;
- else
- return COMPARE_GREATER;
-}
// -----------------------------------------------------------------------
StringCompare STRING::CompareIgnoreCaseToAscii( const STRING& rStr,
commit dd0e82eccbe2e4eb70c3be66af80f11701f0d20e
Author: Caolán McNamara <caolanm at redhat.com>
Date: Mon Feb 14 09:45:47 2011 +0000
add mode lines to new files
diff --git a/tools/qa/test_strings.cxx b/tools/qa/test_strings.cxx
index b7060f3..90a2ce8 100644
--- a/tools/qa/test_strings.cxx
+++ b/tools/qa/test_strings.cxx
@@ -1,3 +1,4 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>
@@ -86,3 +87,5 @@ void test::unistring::Compare::testCompareToNumeric()
}
CPPUNIT_TEST_SUITE_REGISTRATION(test::unistring::Compare);
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/qa/urlobj/tools_urlobj_test.cxx b/tools/qa/urlobj/tools_urlobj_test.cxx
index 049f770..4b476e7 100644
--- a/tools/qa/urlobj/tools_urlobj_test.cxx
+++ b/tools/qa/urlobj/tools_urlobj_test.cxx
@@ -1,3 +1,4 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Version: MPL 1.1 / GPLv3+ / LGPLv3+
*
@@ -181,3 +182,5 @@
// this macro creates an empty function, which will called by the RegisterAllFunctions()
// to let the user the possibility to also register some functions by hand.
NOADDITIONAL;
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
More information about the Libreoffice-commits
mailing list