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 sal_Char
* cpystr( sal_Char
* dst
, const sal_Char
* src
)
87 const sal_Char
* psrc
= src
;
90 while( *pdst
++ = *psrc
++ );
94 sal_Char
* cpynstr( sal_Char
* dst
, const sal_Char
* src
, sal_uInt32 cnt
)
97 const sal_Char
* psrc
= src
;
102 if ( len
>= AStringLen(src
) )
104 return( cpystr( dst
, src
) );
107 // copy string by char
108 for( i
= 0; i
< len
; i
++ )
115 //------------------------------------------------------------------------
116 sal_Bool
cmpstr( const sal_Char
* str1
, const sal_Char
* str2
, sal_uInt32 len
)
118 const sal_Char
* pBuf1
= str1
;
119 const sal_Char
* pBuf2
= str2
;
122 while ( (*pBuf1
== *pBuf2
) && i
< len
)
130 //------------------------------------------------------------------------
131 sal_Bool
cmpustr( const sal_Unicode
* str1
, const sal_Unicode
* str2
, sal_uInt32 len
)
133 const sal_Unicode
* pBuf1
= str1
;
134 const sal_Unicode
* pBuf2
= str2
;
137 while ( (*pBuf1
== *pBuf2
) && i
< len
)
146 //-----------------------------------------------------------------------
147 sal_Bool
cmpustr( const sal_Unicode
* str1
, const sal_Unicode
* str2
)
149 const sal_Unicode
* pBuf1
= str1
;
150 const sal_Unicode
* pBuf2
= str2
;
151 sal_Bool res
= sal_True
;
153 while ( (*pBuf1
== *pBuf2
) && *pBuf1
!='\0' && *pBuf2
!= '\0')
158 if (*pBuf1
== '\0' && *pBuf2
== '\0')
165 sal_Char
* createName( sal_Char
* dst
, const sal_Char
* meth
, sal_uInt32 cnt
)
167 sal_Char
* pdst
= dst
;
169 sal_Char
* pstr
= nstr
;
170 rtl_str_valueOfInt32( pstr
, cnt
, 10 );
172 cpystr( pdst
, meth
);
173 cpystr( pdst
+ AStringLen(meth
), "_" );
177 cpystr(pdst
+ AStringLen(pdst
), "0" );
181 cpystr(pdst
+ AStringLen(pdst
), "0" );
184 cpystr( pdst
+ AStringLen(pdst
), nstr
);
188 //------------------------------------------------------------------------
189 // testing the method compareTo( const OString & aStr )
190 //------------------------------------------------------------------------
191 void makeComment( char *com
, const char *str1
, const char *str2
,
195 int str1Length
= AStringLen( str1
);
196 const char *sign
= (sgn
== 0) ? " == " : (sgn
> 0) ? " > " : " < " ;
197 cpystr(com
+ str1Length
, sign
);
198 int signLength
= AStringLen(sign
);
199 cpystr(com
+ str1Length
+ signLength
, str2
);
200 com
[str1Length
+ signLength
+ AStringLen(str2
)] = 0;
204 //------------------------------------------------------------------------
206 sal_Bool
AStringToFloatCompare ( const sal_Char
*pStr
,
211 sal_Bool cmp
= sal_False
;
215 ::rtl::OString
aStr(pStr
);
221 actNum
= aStr
.toFloat();
223 if ( abs( (int)(actNum
- expNum
) ) <= eps
)
230 } // AStringToFloatCompare
232 //------------------------------------------------------------------------
234 sal_Bool
AStringToDoubleCompare ( const sal_Char
*pStr
,
239 sal_Bool cmp
= sal_False
;
243 ::rtl::OString
aStr(pStr
);
249 actNum
= aStr
.toDouble();
251 if ( abs( (int)(actNum
- expNum
) ) <= eps
)
258 } // AStringToDoubleCompare
260 //------------------------------------------------------------------------
263 //------------------------------------------------------------------------
265 sal_uInt32
UStringLen( const sal_Unicode
*pUStr
)
267 sal_uInt32 nUStrLen
= 0;
271 const sal_Unicode
*pTempUStr
= pUStr
;
278 nUStrLen
= (sal_uInt32
)( pTempUStr
- pUStr
);
284 //------------------------------------------------------------------------
286 sal_Bool
AStringIsValid( const sal_Char
*pAStr
)
290 sal_uInt32 nLen
= AStringLen( pAStr
);
293 while ( ( nLen
>= 0 ) && ( *pAStr
) )
295 uChar
= (unsigned char)*pAStr
;
304 // Since we are dealing with unsigned integers
305 // we want to make sure that the last number is
322 //------------------------------------------------------------------------
324 sal_Bool
AStringNIsValid( const sal_Char
*pAStr
,
325 const sal_uInt32 nStrLen
328 sal_uInt32 nLen
= nStrLen
;
331 while ( ( nLen
>= 0 ) && ( *pAStr
) )
333 uChar
= (unsigned char)*pAStr
;
342 // Since we are dealing with unsigned integers
343 // we want to make sure that the last number is
359 //------------------------------------------------------------------------
361 static inline sal_Int32
ACharToUCharCompare( const sal_Unicode
*pUStr
,
362 const sal_Char
*pAStr
366 sal_Int32 nUChar
= (sal_Int32
)*pUStr
;
367 sal_Int32 nChar
= (sal_Int32
)((unsigned char)*pAStr
);
369 nCmp
= nUChar
- nChar
;
372 } // ACharToUCharCompare
374 //------------------------------------------------------------------------
376 sal_Int32
AStringToUStringCompare( const sal_Unicode
*pUStr
,
377 const sal_Char
*pAStr
380 sal_Int32 nCmp
= kErrCompareAStringToUString
;
382 if ( ( pUStr
!= NULL
) && ( pAStr
!= NULL
) )
384 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
386 while ( ( nCmp
== 0 ) && ( *pAStr
) )
391 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
396 } // AStringToUStringCompare
398 //------------------------------------------------------------------------
400 sal_Int32
AStringToUStringNCompare( const sal_Unicode
*pUStr
,
401 const sal_Char
*pAStr
,
402 const sal_uInt32 nAStrCount
405 sal_Int32 nCmp
= kErrCompareNAStringToUString
;
407 if ( ( pUStr
!= NULL
) && ( pAStr
!= NULL
) )
409 sal_uInt32 nCount
= nAStrCount
;
411 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
413 while ( ( nCmp
== 0 ) && ( *pAStr
) && ( nCount
) )
418 nCmp
= ACharToUCharCompare( pUStr
, pAStr
);
420 // Since we are dealing with unsigned integers
421 // we want to make sure that the last number is
436 } // AStringToUStringNCompare
438 //------------------------------------------------------------------------
440 sal_Int32
AStringToRTLUStringCompare( const rtl_uString
*pRTLUStr
,
441 const sal_Char
*pAStr
444 sal_Int32 nCmp
= kErrCompareAStringToRTLUString
;
446 if ( ( pRTLUStr
!= NULL
) && ( pAStr
!= NULL
) )
448 rtl_uString
*pRTLUStrCopy
= NULL
;
450 rtl_uString_newFromString( &pRTLUStrCopy
, pRTLUStr
);
452 if ( pRTLUStrCopy
!= NULL
)
454 const sal_Unicode
*pUStr
= rtl_uString_getStr( pRTLUStrCopy
);
458 nCmp
= AStringToUStringCompare( pUStr
, pAStr
);
461 rtl_uString_release( pRTLUStrCopy
);
468 } // AStringToRTLUStringCompare
470 //------------------------------------------------------------------------
472 sal_Int32
AStringToRTLUStringNCompare( const rtl_uString
*pRTLUStr
,
473 const sal_Char
*pAStr
,
474 const sal_uInt32 nAStrCount
477 sal_Int32 nCmp
= kErrCompareNAStringToRTLUString
;
479 if ( ( pRTLUStr
!= NULL
) && ( pAStr
!= NULL
) )
481 rtl_uString
*pRTLUStrCopy
= NULL
;
483 rtl_uString_newFromString( &pRTLUStrCopy
, pRTLUStr
);
485 if ( pRTLUStrCopy
!= NULL
)
487 const sal_Unicode
*pUStr
= rtl_uString_getStr( pRTLUStrCopy
);
491 nCmp
= AStringToUStringNCompare( pUStr
, pAStr
, nAStrCount
);
494 rtl_uString_release( pRTLUStrCopy
);
501 } // AStringToRTLUStringNCompare
503 //------------------------------------------------------------------------
505 sal_Bool
AStringToUStringCopy( sal_Unicode
*pDest
,
509 sal_Bool bCopied
= sal_False
;
510 sal_uInt32 nCount
= AStringLen( pSrc
);
511 sal_uInt32 nLen
= nCount
;
513 if ( ( pDest
!= NULL
)
515 && ( AStringNIsValid( pSrc
, nLen
) )
518 while ( nCount
>= 0 )
520 *pDest
= (unsigned char)*pSrc
;
525 // Since we are dealing with unsigned integers
526 // we want to make sure that the last number is
546 } // AStringToUStringCopy
548 //------------------------------------------------------------------------
550 sal_Bool
AStringToUStringNCopy( sal_Unicode
*pDest
,
551 const sal_Char
*pSrc
,
552 const sal_uInt32 nSrcLen
555 sal_Bool bCopied
= sal_False
;
556 sal_uInt32 nCount
= nSrcLen
;
557 sal_uInt32 nLen
= nSrcLen
;
559 if ( ( pDest
!= NULL
)
561 && ( AStringNIsValid( pSrc
, nLen
) )
564 while ( nCount
>= 0 )
566 *pDest
= (unsigned char)*pSrc
;
571 // Since we are dealing with unsigned integers
572 // we want to make sure that the last number is
592 } // AStringToUStringNCopy
594 //------------------------------------------------------------------------
595 //------------------------------------------------------------------------