update dev300-m57
[ooovba.git] / sal / qa / rtl_strings / rtl_String_Utils.cxx
blobdf00e32e348919f4c745f3134496ec5d7c3efb2a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_String_Utils.cxx,v $
10 * $Revision: 1.5 $
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 //------------------------------------------------------------------------
37 #include <math.h>
38 #include <stdlib.h>
40 //------------------------------------------------------------------------
41 //------------------------------------------------------------------------
43 #ifndef _SAL_TYPES_H_
44 #include <sal/types.h>
45 #endif
47 #ifndef _RTL_USTRING_H_
48 #include <rtl/ustring.h>
49 #endif
51 #ifndef _RTL_STRING_HXX_
52 #include <rtl/string.hxx>
53 #endif
55 //------------------------------------------------------------------------
56 //------------------------------------------------------------------------
58 #ifndef _RTL_STRING_UTILS_CONST_H_
59 #include <rtl_String_Utils_Const.h>
60 #endif
62 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
65 using namespace rtl;
67 sal_uInt32 AStringLen( const sal_Char *pAStr )
69 sal_uInt32 nStrLen = 0;
71 if ( pAStr != NULL )
73 const sal_Char *pTempStr = pAStr;
75 while( *pTempStr )
77 pTempStr++;
78 } // while
80 nStrLen = (sal_uInt32)( pTempStr - pAStr );
81 } // if
83 return nStrLen;
84 } // AStringLen
85 sal_Char* cpystr( sal_Char* dst, const sal_Char* src )
87 const sal_Char* psrc = src;
88 sal_Char* pdst = dst;
90 while( *pdst++ = *psrc++ );
91 return ( dst );
94 sal_Char* cpynstr( sal_Char* dst, const sal_Char* src, sal_uInt32 cnt )
97 const sal_Char* psrc = src;
98 sal_Char* pdst = dst;
99 sal_uInt32 len = cnt;
100 sal_uInt32 i;
102 if ( len >= AStringLen(src) )
104 return( cpystr( dst, src ) );
107 // copy string by char
108 for( i = 0; i < len; i++ )
109 *pdst++ = *psrc++;
110 *pdst = '\0';
112 return ( dst );
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;
120 sal_uInt32 i = 0;
122 while ( (*pBuf1 == *pBuf2) && i < len )
124 (pBuf1)++;
125 (pBuf2)++;
126 i++;
128 return( 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;
135 sal_uInt32 i = 0;
137 while ( (*pBuf1 == *pBuf2) && i < len )
139 (pBuf1)++;
140 (pBuf2)++;
141 i++;
143 return( 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')
155 (pBuf1)++;
156 (pBuf2)++;
158 if (*pBuf1 == '\0' && *pBuf2 == '\0')
159 res = sal_True;
160 else
161 res = sal_False;
162 return (res);
165 sal_Char* createName( sal_Char* dst, const sal_Char* meth, sal_uInt32 cnt )
167 sal_Char* pdst = dst;
168 sal_Char nstr[16];
169 sal_Char* pstr = nstr;
170 rtl_str_valueOfInt32( pstr, cnt, 10 );
172 cpystr( pdst, meth );
173 cpystr( pdst+ AStringLen(meth), "_" );
175 if ( cnt < 100 )
177 cpystr(pdst + AStringLen(pdst), "0" );
179 if ( cnt < 10 )
181 cpystr(pdst + AStringLen(pdst), "0" );
184 cpystr( pdst + AStringLen(pdst), nstr );
185 return( pdst );
188 //------------------------------------------------------------------------
189 // testing the method compareTo( const OString & aStr )
190 //------------------------------------------------------------------------
191 void makeComment( char *com, const char *str1, const char *str2,
192 sal_Int32 sgn )
194 cpystr(com, str1);
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,
207 const float nX,
208 const float nEPS
211 sal_Bool cmp = sal_False;
213 if ( pStr != NULL )
215 ::rtl::OString aStr(pStr);
217 float actNum = 0;
218 float expNum = nX;
219 float eps = nEPS;
221 actNum = aStr.toFloat();
223 if ( abs( (int)(actNum - expNum) ) <= eps )
225 cmp = sal_True;
226 } // if
227 } // if
229 return cmp;
230 } // AStringToFloatCompare
232 //------------------------------------------------------------------------
234 sal_Bool AStringToDoubleCompare ( const sal_Char *pStr,
235 const double nX,
236 const double nEPS
239 sal_Bool cmp = sal_False;
241 if ( pStr != NULL )
243 ::rtl::OString aStr(pStr);
245 double actNum = 0;
246 double expNum = nX;
247 double eps = nEPS;
249 actNum = aStr.toDouble();
251 if ( abs( (int)(actNum - expNum) ) <= eps )
253 cmp = sal_True;
254 } // if
255 } // if
257 return cmp;
258 } // AStringToDoubleCompare
260 //------------------------------------------------------------------------
263 //------------------------------------------------------------------------
265 sal_uInt32 UStringLen( const sal_Unicode *pUStr )
267 sal_uInt32 nUStrLen = 0;
269 if ( pUStr != NULL )
271 const sal_Unicode *pTempUStr = pUStr;
273 while( *pTempUStr )
275 pTempUStr++;
276 } // while
278 nUStrLen = (sal_uInt32)( pTempUStr - pUStr );
279 } // if
281 return nUStrLen;
282 } // UStringLen
284 //------------------------------------------------------------------------
286 sal_Bool AStringIsValid( const sal_Char *pAStr )
288 if ( pAStr != NULL )
290 sal_uInt32 nLen = AStringLen( pAStr );
291 sal_uChar uChar = 0;
293 while ( ( nLen >= 0 ) && ( *pAStr ) )
295 uChar = (unsigned char)*pAStr;
297 if ( uChar > 127 )
299 return sal_False;
300 } // if
302 pAStr++;
304 // Since we are dealing with unsigned integers
305 // we want to make sure that the last number is
306 // indeed zero.
308 if ( nLen > 0 )
310 nLen--;
311 } // if
312 else
314 break;
315 } // else
316 } // while
317 } // if
319 return sal_True;
320 } // AStringIsValid
322 //------------------------------------------------------------------------
324 sal_Bool AStringNIsValid( const sal_Char *pAStr,
325 const sal_uInt32 nStrLen
328 sal_uInt32 nLen = nStrLen;
329 sal_uChar uChar = 0;
331 while ( ( nLen >= 0 ) && ( *pAStr ) )
333 uChar = (unsigned char)*pAStr;
335 if ( uChar > 127 )
337 return sal_False;
338 } // if
340 pAStr++;
342 // Since we are dealing with unsigned integers
343 // we want to make sure that the last number is
344 // indeed zero.
346 if ( nLen > 0 )
348 nLen--;
349 } // if
350 else
352 break;
353 } // else
354 } // while
356 return sal_True;
357 } // AStringNIsValid
359 //------------------------------------------------------------------------
361 static inline sal_Int32 ACharToUCharCompare( const sal_Unicode *pUStr,
362 const sal_Char *pAStr
365 sal_Int32 nCmp = 0;
366 sal_Int32 nUChar = (sal_Int32)*pUStr;
367 sal_Int32 nChar = (sal_Int32)((unsigned char)*pAStr);
369 nCmp = nUChar - nChar;
371 return nCmp;
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 ) )
388 pUStr++;
389 pAStr++;
391 nCmp = ACharToUCharCompare( pUStr, pAStr );
392 } // while
393 } // if
395 return nCmp;
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 ) )
415 pUStr++;
416 pAStr++;
418 nCmp = ACharToUCharCompare( pUStr, pAStr );
420 // Since we are dealing with unsigned integers
421 // we want to make sure that the last number is
422 // indeed zero.
424 if ( nCount > 0 )
426 nCount--;
427 } // if
428 else
430 break;
431 } // else
432 } // while
433 } // if
435 return nCmp;
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 );
456 if ( pUStr != NULL )
458 nCmp = AStringToUStringCompare( pUStr, pAStr );
459 } // if
461 rtl_uString_release( pRTLUStrCopy );
463 pRTLUStrCopy = NULL;
464 } // if
465 } // if
467 return nCmp;
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 );
489 if ( pUStr != NULL )
491 nCmp = AStringToUStringNCompare( pUStr, pAStr, nAStrCount );
492 } // if
494 rtl_uString_release( pRTLUStrCopy );
496 pRTLUStrCopy = NULL;
497 } // if
498 } // if
500 return nCmp;
501 } // AStringToRTLUStringNCompare
503 //------------------------------------------------------------------------
505 sal_Bool AStringToUStringCopy( sal_Unicode *pDest,
506 const sal_Char *pSrc
509 sal_Bool bCopied = sal_False;
510 sal_uInt32 nCount = AStringLen( pSrc );
511 sal_uInt32 nLen = nCount;
513 if ( ( pDest != NULL )
514 && ( pSrc != NULL )
515 && ( AStringNIsValid( pSrc, nLen ) )
518 while ( nCount >= 0 )
520 *pDest = (unsigned char)*pSrc;
522 pDest++;
523 pSrc++;
525 // Since we are dealing with unsigned integers
526 // we want to make sure that the last number is
527 // indeed zero.
529 if ( nCount > 0 )
531 nCount--;
532 } // if
533 else
535 break;
536 } // else
537 } // while
539 if ( nCount == 0 )
541 bCopied = sal_True;
542 } // if
543 } // if
545 return bCopied;
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 )
560 && ( pSrc != NULL )
561 && ( AStringNIsValid( pSrc, nLen ) )
564 while ( nCount >= 0 )
566 *pDest = (unsigned char)*pSrc;
568 pDest++;
569 pSrc++;
571 // Since we are dealing with unsigned integers
572 // we want to make sure that the last number is
573 // indeed zero.
575 if ( nCount > 0 )
577 nCount--;
578 } // if
579 else
581 break;
582 } // else
583 } // while
585 if ( nCount == 0 )
587 bCopied = sal_True;
588 } // if
589 } // if
591 return bCopied;
592 } // AStringToUStringNCopy
594 //------------------------------------------------------------------------
595 //------------------------------------------------------------------------