diff options
author | Sébastien Le Ray <sebastien-libreoffice@orniz.org> | 2011-02-10 16:53:29 +0100 |
---|---|---|
committer | Cédric Bosdonnat <cedricbosdo@openoffice.org> | 2011-02-10 16:56:30 +0100 |
commit | b5d135810fad8e8309a5572159173fd4ffa5c66e (patch) | |
tree | bfeb68f0bf1fe6a316fe89dd3fe76b35bfe3d865 /tools/source/string/strimp.cxx | |
parent | 298b9df14ba23b43db3691d299191d01f3815fc3 (diff) |
Add a CompareToNumeric to ByteString & UniString
Diffstat (limited to 'tools/source/string/strimp.cxx')
-rw-r--r-- | tools/source/string/strimp.cxx | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tools/source/string/strimp.cxx b/tools/source/string/strimp.cxx index 72e6fcdea593..54efd7b66182 100644 --- a/tools/source/string/strimp.cxx +++ b/tools/source/string/strimp.cxx @@ -30,6 +30,8 @@ // ======================================================================= +#define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57))) + static sal_Int32 ImplStringCompare( const STRCODE* pStr1, const STRCODE* pStr2 ) { sal_Int32 nRet; @@ -61,6 +63,55 @@ 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, @@ -1277,6 +1328,24 @@ 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, |