1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_String_Utils.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 #*************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 //------------------------------------------------------------------------
35 //------------------------------------------------------------------------
40 //------------------------------------------------------------------------
41 //------------------------------------------------------------------------
44 #include <sal/types.h>
47 #ifndef _RTL_USTRING_H_
48 #include <rtl/ustring.h>
51 #ifndef _RTL_STRING_HXX_
52 #include <rtl/string.hxx>
55 //------------------------------------------------------------------------
56 //------------------------------------------------------------------------
58 #ifndef _RTL_STRING_UTILS_CONST_H_
59 #include <rtl_String_Utils_Const.h>
62 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
67 sal_uInt32
AStringLen( const sal_Char
*pAStr
)
69 sal_uInt32 nStrLen
= 0;
73 const sal_Char
*pTempStr
= pAStr
;
80 nStrLen
= (sal_uInt32
)( pTempStr
- pAStr
);
85 /* disable assignment within condition expression */
87 #pragma warning( disable : 4706 )
89 sal_Char
* cpystr( sal_Char
* dst
, const sal_Char
* src
)
91 const sal_Char
* psrc
= src
;
94 while( *pdst
++ = *psrc
++ );
98 sal_Char
* cpynstr( sal_Char
* dst
, const sal_Char
* src
, sal_uInt32 cnt
)
101 const sal_Char
* psrc
= src
;
102 sal_Char
* pdst
= dst
;
103 sal_uInt32 len
= cnt
;
106 if ( len
>= AStringLen(src
) )
108 return( cpystr( dst
, src
) );
111 // copy string by char
112 for( i
= 0; i
< len
; i
++ )
119 //------------------------------------------------------------------------
120 sal_Bool
cmpstr( const sal_Char
* str1
, const sal_Char
* str2
, sal_uInt32 len
)
122 const sal_Char
* pBuf1
= str1
;
123 const sal_Char
* pBuf2
= str2
;
126 while ( (*pBuf1
== *pBuf2
) && i
< len
)
134 //-----------------------------------------------------------------------
135 sal_Bool
cmpstr( const sal_Char
* str1
, const sal_Char
* str2
)
137 const sal_Char
* pBuf1
= str1
;
138 const sal_Char
* pBuf2
= str2
;
139 sal_Bool res
= sal_True
;
141 while ( (*pBuf1
== *pBuf2
) && *pBuf1
!='\0' && *pBuf2
!= '\0')
146 if (*pBuf1
== '\0' && *pBuf2
== '\0')
152 //------------------------------------------------------------------------
153 sal_Bool
cmpustr( const sal_Unicode
* str1
, const sal_Unicode
* str2
, sal_uInt32 len
)
155 const sal_Unicode
* pBuf1
= str1
;
156 const sal_Unicode
* pBuf2
= str2
;
159 while ( (*pBuf1
== *pBuf2
) && i
< len
)
168 //-----------------------------------------------------------------------
169 sal_Bool
cmpustr( const sal_Unicode
* str1
, const sal_Unicode
* str2
)
171 const sal_Unicode
* pBuf1
= str1
;
172 const sal_Unicode
* pBuf2
= str2
;
173 sal_Bool res
= sal_True
;
175 while ( (*pBuf1
== *pBuf2
) && *pBuf1
!='\0' && *pBuf2
!= '\0')
180 if (*pBuf1
== '\0' && *pBuf2
== '\0')
187 sal_Char
* createName( sal_Char
* dst
, const sal_Char
* meth
, sal_uInt32 cnt
)
189 sal_Char
* pdst
= dst
;
191 sal_Char
* pstr
= nstr
;
192 rtl_str_valueOfInt32( pstr
, cnt
, 10 );
194 cpystr( pdst
, meth
);
195 cpystr( pdst
+ AStringLen(meth
), "_" );
199 cpystr(pdst
+ AStringLen(pdst
), "0" );
203 cpystr(pdst
+ AStringLen(pdst
), "0" );
206 cpystr( pdst
+ AStringLen(pdst
), nstr
);
210 //------------------------------------------------------------------------
211 // testing the method compareTo( const OString & aStr )
212 //------------------------------------------------------------------------
213 void makeComment( char *com
, const char *str1
, const char *str2
,
217 int str1Length
= AStringLen( str1
);
218 const char *sign
= (sgn
== 0) ? " == " : (sgn
> 0) ? " > " : " < " ;
219 cpystr(com
+ str1Length
, sign
);
220 int signLength
= AStringLen(sign
);
221 cpystr(com
+ str1Length
+ signLength
, str2
);
222 com
[str1Length
+ signLength
+ AStringLen(str2
)] = 0;
226 //------------------------------------------------------------------------
228 sal_Bool
AStringToFloatCompare ( const sal_Char
*pStr
,
233 sal_Bool cmp
= sal_False
;
237 ::rtl::OString
aStr(pStr
);
243 actNum
= aStr
.toFloat();
245 if ( abs( (int)(actNum
- expNum
) ) <= eps
)
252 } // AStringToFloatCompare
254 //------------------------------------------------------------------------
256 sal_Bool
AStringToDoubleCompare ( const sal_Char
*pStr
,
261 sal_Bool cmp
= sal_False
;
265 ::rtl::OString
aStr(pStr
);
271 actNum
= aStr
.toDouble();
273 if ( abs( (int)(actNum
- expNum
) ) <= eps
)
280 } // AStringToDoubleCompare
282 //------------------------------------------------------------------------
285 //------------------------------------------------------------------------
287 sal_uInt32
UStringLen( const sal_Unicode
*pUStr
)
289 sal_uInt32 nUStrLen
= 0;
293 const sal_Unicode
*pTempUStr
= pUStr
;
300 nUStrLen
= (sal_uInt32
)( pTempUStr
- pUStr
);
306 //------------------------------------------------------------------------
308 sal_Bool
AStringIsValid( const sal_Char
*pAStr
)
312 sal_uInt32 nLen
= AStringLen( pAStr
);
317 uChar
= (unsigned char)*pAStr
;
326 // Since we are dealing with unsigned integers
327 // we want to make sure that the last number is
344 //------------------------------------------------------------------------
346 sal_Bool
AStringNIsValid( const sal_Char
*pAStr
,
347 const sal_uInt32 nStrLen
350 sal_uInt32 nLen
= nStrLen
;
355 uChar
= (unsigned char)*pAStr
;
364 // Since we are dealing with unsigned integers
365 // we want to make sure that the last number is
381 //------------------------------------------------------------------------
383 static inline sal_Int32
ACharToUCharCompare( const sal_Unicode
*pUStr
,
384 const sal_Char
*pAStr
388 sal_Int32 nUChar
= (sal_Int32
)*pUStr
;
389 sal_Int32 nChar
= (sal_Int32
)((unsigned char)*pAStr
);
391 nCmp
= nUChar
- nChar
;
394 } // ACharToUCharCompare
396 //------------------------------------------------------------------------
398 sal_Int32
AStringToUStringCompare( const sal_Unicode
*pUStr
,
399 const sal_Char
*pAStr
402 sal_Int32 nCmp
= kErrCompareAStringToUString
;
404 if ( ( pUStr
!= NULL
) && ( pAStr
!= NULL
) )
406 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
408 while ( ( nCmp
== 0 ) && ( *pAStr
) )
413 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
418 } // AStringToUStringCompare
420 //------------------------------------------------------------------------
422 sal_Int32
AStringToUStringNCompare( const sal_Unicode
*pUStr
,
423 const sal_Char
*pAStr
,
424 const sal_uInt32 nAStrCount
427 sal_Int32 nCmp
= kErrCompareNAStringToUString
;
429 if ( ( pUStr
!= NULL
) && ( pAStr
!= NULL
) )
431 sal_uInt32 nCount
= nAStrCount
;
433 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
435 while ( ( nCmp
== 0 ) && ( *pAStr
) && ( nCount
) )
440 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
442 // Since we are dealing with unsigned integers
443 // we want to make sure that the last number is
458 } // AStringToUStringNCompare
460 //------------------------------------------------------------------------
462 sal_Int32
AStringToRTLUStringCompare( const rtl_uString
*pRTLUStr
,
463 const sal_Char
*pAStr
466 sal_Int32 nCmp
= kErrCompareAStringToRTLUString
;
468 if ( ( pRTLUStr
!= NULL
) && ( pAStr
!= NULL
) )
470 rtl_uString
*pRTLUStrCopy
= NULL
;
472 rtl_uString_newFromString( &pRTLUStrCopy
, pRTLUStr
);
474 if ( pRTLUStrCopy
!= NULL
)
476 const sal_Unicode
*pUStr
= rtl_uString_getStr( pRTLUStrCopy
);
480 nCmp
= AStringToUStringCompare( pUStr
, pAStr
);
483 rtl_uString_release( pRTLUStrCopy
);
490 } // AStringToRTLUStringCompare
492 //------------------------------------------------------------------------
494 sal_Int32
AStringToRTLUStringNCompare( const rtl_uString
*pRTLUStr
,
495 const sal_Char
*pAStr
,
496 const sal_uInt32 nAStrCount
499 sal_Int32 nCmp
= kErrCompareNAStringToRTLUString
;
501 if ( ( pRTLUStr
!= NULL
) && ( pAStr
!= NULL
) )
503 rtl_uString
*pRTLUStrCopy
= NULL
;
505 rtl_uString_newFromString( &pRTLUStrCopy
, pRTLUStr
);
507 if ( pRTLUStrCopy
!= NULL
)
509 const sal_Unicode
*pUStr
= rtl_uString_getStr( pRTLUStrCopy
);
513 nCmp
= AStringToUStringNCompare( pUStr
, pAStr
, nAStrCount
);
516 rtl_uString_release( pRTLUStrCopy
);
523 } // AStringToRTLUStringNCompare
525 //------------------------------------------------------------------------
527 sal_Bool
AStringToUStringCopy( sal_Unicode
*pDest
,
531 sal_Bool bCopied
= sal_False
;
532 sal_uInt32 nCount
= AStringLen( pSrc
);
533 sal_uInt32 nLen
= nCount
;
535 if ( ( pDest
!= NULL
)
537 && ( AStringNIsValid( pSrc
, nLen
) )
542 *pDest
= (unsigned char)*pSrc
;
547 // Since we are dealing with unsigned integers
548 // we want to make sure that the last number is
568 } // AStringToUStringCopy
570 //------------------------------------------------------------------------
572 sal_Bool
AStringToUStringNCopy( sal_Unicode
*pDest
,
573 const sal_Char
*pSrc
,
574 const sal_uInt32 nSrcLen
577 sal_Bool bCopied
= sal_False
;
578 sal_uInt32 nCount
= nSrcLen
;
579 sal_uInt32 nLen
= nSrcLen
;
581 if ( ( pDest
!= NULL
)
583 && ( AStringNIsValid( pSrc
, nLen
) )
588 *pDest
= (unsigned char)*pSrc
;
593 // Since we are dealing with unsigned integers
594 // we want to make sure that the last number is
614 } // AStringToUStringNCopy
616 //------------------------------------------------------------------------
617 //------------------------------------------------------------------------