merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / rtl_strings / rtl_String_Utils.cxx
blob0fccc111a18497d07757efc14ce7b68f3e63be6f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 #*************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 //------------------------------------------------------------------------
32 //------------------------------------------------------------------------
34 #include <math.h>
35 #include <stdlib.h>
37 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
40 #ifndef _SAL_TYPES_H_
41 #include <sal/types.h>
42 #endif
44 #ifndef _RTL_USTRING_H_
45 #include <rtl/ustring.h>
46 #endif
48 #ifndef _RTL_STRING_HXX_
49 #include <rtl/string.hxx>
50 #endif
52 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
55 #ifndef _RTL_STRING_UTILS_CONST_H_
56 #include <rtl_String_Utils_Const.h>
57 #endif
59 //------------------------------------------------------------------------
60 //------------------------------------------------------------------------
62 using namespace rtl;
64 sal_uInt32 AStringLen( const sal_Char *pAStr )
66 sal_uInt32 nStrLen = 0;
68 if ( pAStr != NULL )
70 const sal_Char *pTempStr = pAStr;
72 while( *pTempStr )
74 pTempStr++;
75 } // while
77 nStrLen = (sal_uInt32)( pTempStr - pAStr );
78 } // if
80 return nStrLen;
81 } // AStringLen
82 sal_Char* cpystr( sal_Char* dst, const sal_Char* src )
84 const sal_Char* psrc = src;
85 sal_Char* pdst = dst;
87 while( *pdst++ = *psrc++ );
88 return ( dst );
91 sal_Char* cpynstr( sal_Char* dst, const sal_Char* src, sal_uInt32 cnt )
94 const sal_Char* psrc = src;
95 sal_Char* pdst = dst;
96 sal_uInt32 len = cnt;
97 sal_uInt32 i;
99 if ( len >= AStringLen(src) )
101 return( cpystr( dst, src ) );
104 // copy string by char
105 for( i = 0; i < len; i++ )
106 *pdst++ = *psrc++;
107 *pdst = '\0';
109 return ( dst );
112 //------------------------------------------------------------------------
113 sal_Bool cmpstr( const sal_Char* str1, const sal_Char* str2, sal_uInt32 len )
115 const sal_Char* pBuf1 = str1;
116 const sal_Char* pBuf2 = str2;
117 sal_uInt32 i = 0;
119 while ( (*pBuf1 == *pBuf2) && i < len )
121 (pBuf1)++;
122 (pBuf2)++;
123 i++;
125 return( i == len );
127 //------------------------------------------------------------------------
128 sal_Bool cmpustr( const sal_Unicode* str1, const sal_Unicode* str2, sal_uInt32 len )
130 const sal_Unicode* pBuf1 = str1;
131 const sal_Unicode* pBuf2 = str2;
132 sal_uInt32 i = 0;
134 while ( (*pBuf1 == *pBuf2) && i < len )
136 (pBuf1)++;
137 (pBuf2)++;
138 i++;
140 return( i == len );
143 //-----------------------------------------------------------------------
144 sal_Bool cmpustr( const sal_Unicode* str1, const sal_Unicode* str2 )
146 const sal_Unicode* pBuf1 = str1;
147 const sal_Unicode* pBuf2 = str2;
148 sal_Bool res = sal_True;
150 while ( (*pBuf1 == *pBuf2) && *pBuf1 !='\0' && *pBuf2 != '\0')
152 (pBuf1)++;
153 (pBuf2)++;
155 if (*pBuf1 == '\0' && *pBuf2 == '\0')
156 res = sal_True;
157 else
158 res = sal_False;
159 return (res);
162 sal_Char* createName( sal_Char* dst, const sal_Char* meth, sal_uInt32 cnt )
164 sal_Char* pdst = dst;
165 sal_Char nstr[16];
166 sal_Char* pstr = nstr;
167 rtl_str_valueOfInt32( pstr, cnt, 10 );
169 cpystr( pdst, meth );
170 cpystr( pdst+ AStringLen(meth), "_" );
172 if ( cnt < 100 )
174 cpystr(pdst + AStringLen(pdst), "0" );
176 if ( cnt < 10 )
178 cpystr(pdst + AStringLen(pdst), "0" );
181 cpystr( pdst + AStringLen(pdst), nstr );
182 return( pdst );
185 //------------------------------------------------------------------------
186 // testing the method compareTo( const OString & aStr )
187 //------------------------------------------------------------------------
188 void makeComment( char *com, const char *str1, const char *str2,
189 sal_Int32 sgn )
191 cpystr(com, str1);
192 int str1Length = AStringLen( str1 );
193 const char *sign = (sgn == 0) ? " == " : (sgn > 0) ? " > " : " < " ;
194 cpystr(com + str1Length, sign);
195 int signLength = AStringLen(sign);
196 cpystr(com + str1Length + signLength, str2);
197 com[str1Length + signLength + AStringLen(str2)] = 0;
201 //------------------------------------------------------------------------
203 sal_Bool AStringToFloatCompare ( const sal_Char *pStr,
204 const float nX,
205 const float nEPS
208 sal_Bool cmp = sal_False;
210 if ( pStr != NULL )
212 ::rtl::OString aStr(pStr);
214 float actNum = 0;
215 float expNum = nX;
216 float eps = nEPS;
218 actNum = aStr.toFloat();
220 if ( abs( (int)(actNum - expNum) ) <= eps )
222 cmp = sal_True;
223 } // if
224 } // if
226 return cmp;
227 } // AStringToFloatCompare
229 //------------------------------------------------------------------------
231 sal_Bool AStringToDoubleCompare ( const sal_Char *pStr,
232 const double nX,
233 const double nEPS
236 sal_Bool cmp = sal_False;
238 if ( pStr != NULL )
240 ::rtl::OString aStr(pStr);
242 double actNum = 0;
243 double expNum = nX;
244 double eps = nEPS;
246 actNum = aStr.toDouble();
248 if ( abs( (int)(actNum - expNum) ) <= eps )
250 cmp = sal_True;
251 } // if
252 } // if
254 return cmp;
255 } // AStringToDoubleCompare
257 //------------------------------------------------------------------------
260 //------------------------------------------------------------------------
262 sal_uInt32 UStringLen( const sal_Unicode *pUStr )
264 sal_uInt32 nUStrLen = 0;
266 if ( pUStr != NULL )
268 const sal_Unicode *pTempUStr = pUStr;
270 while( *pTempUStr )
272 pTempUStr++;
273 } // while
275 nUStrLen = (sal_uInt32)( pTempUStr - pUStr );
276 } // if
278 return nUStrLen;
279 } // UStringLen
281 //------------------------------------------------------------------------
283 sal_Bool AStringIsValid( const sal_Char *pAStr )
285 if ( pAStr != NULL )
287 sal_uInt32 nLen = AStringLen( pAStr );
288 sal_uChar uChar = 0;
290 while ( ( nLen >= 0 ) && ( *pAStr ) )
292 uChar = (unsigned char)*pAStr;
294 if ( uChar > 127 )
296 return sal_False;
297 } // if
299 pAStr++;
301 // Since we are dealing with unsigned integers
302 // we want to make sure that the last number is
303 // indeed zero.
305 if ( nLen > 0 )
307 nLen--;
308 } // if
309 else
311 break;
312 } // else
313 } // while
314 } // if
316 return sal_True;
317 } // AStringIsValid
319 //------------------------------------------------------------------------
321 sal_Bool AStringNIsValid( const sal_Char *pAStr,
322 const sal_uInt32 nStrLen
325 sal_uInt32 nLen = nStrLen;
326 sal_uChar uChar = 0;
328 while ( ( nLen >= 0 ) && ( *pAStr ) )
330 uChar = (unsigned char)*pAStr;
332 if ( uChar > 127 )
334 return sal_False;
335 } // if
337 pAStr++;
339 // Since we are dealing with unsigned integers
340 // we want to make sure that the last number is
341 // indeed zero.
343 if ( nLen > 0 )
345 nLen--;
346 } // if
347 else
349 break;
350 } // else
351 } // while
353 return sal_True;
354 } // AStringNIsValid
356 //------------------------------------------------------------------------
358 static inline sal_Int32 ACharToUCharCompare( const sal_Unicode *pUStr,
359 const sal_Char *pAStr
362 sal_Int32 nCmp = 0;
363 sal_Int32 nUChar = (sal_Int32)*pUStr;
364 sal_Int32 nChar = (sal_Int32)((unsigned char)*pAStr);
366 nCmp = nUChar - nChar;
368 return nCmp;
369 } // ACharToUCharCompare
371 //------------------------------------------------------------------------
373 sal_Int32 AStringToUStringCompare( const sal_Unicode *pUStr,
374 const sal_Char *pAStr
377 sal_Int32 nCmp = kErrCompareAStringToUString;
379 if ( ( pUStr != NULL ) && ( pAStr != NULL ) )
381 nCmp = ACharToUCharCompare( pUStr, pAStr );
383 while ( ( nCmp == 0 ) && ( *pAStr ) )
385 pUStr++;
386 pAStr++;
388 nCmp = ACharToUCharCompare( pUStr, pAStr );
389 } // while
390 } // if
392 return nCmp;
393 } // AStringToUStringCompare
395 //------------------------------------------------------------------------
397 sal_Int32 AStringToUStringNCompare( const sal_Unicode *pUStr,
398 const sal_Char *pAStr,
399 const sal_uInt32 nAStrCount
402 sal_Int32 nCmp = kErrCompareNAStringToUString;
404 if ( ( pUStr != NULL ) && ( pAStr != NULL ) )
406 sal_uInt32 nCount = nAStrCount;
408 nCmp = ACharToUCharCompare( pUStr, pAStr );
410 while ( ( nCmp == 0 ) && ( *pAStr ) && ( nCount ) )
412 pUStr++;
413 pAStr++;
415 nCmp = ACharToUCharCompare( pUStr, pAStr );
417 // Since we are dealing with unsigned integers
418 // we want to make sure that the last number is
419 // indeed zero.
421 if ( nCount > 0 )
423 nCount--;
424 } // if
425 else
427 break;
428 } // else
429 } // while
430 } // if
432 return nCmp;
433 } // AStringToUStringNCompare
435 //------------------------------------------------------------------------
437 sal_Int32 AStringToRTLUStringCompare( const rtl_uString *pRTLUStr,
438 const sal_Char *pAStr
441 sal_Int32 nCmp = kErrCompareAStringToRTLUString;
443 if ( ( pRTLUStr != NULL ) && ( pAStr != NULL ) )
445 rtl_uString *pRTLUStrCopy = NULL;
447 rtl_uString_newFromString( &pRTLUStrCopy, pRTLUStr );
449 if ( pRTLUStrCopy != NULL )
451 const sal_Unicode *pUStr = rtl_uString_getStr( pRTLUStrCopy );
453 if ( pUStr != NULL )
455 nCmp = AStringToUStringCompare( pUStr, pAStr );
456 } // if
458 rtl_uString_release( pRTLUStrCopy );
460 pRTLUStrCopy = NULL;
461 } // if
462 } // if
464 return nCmp;
465 } // AStringToRTLUStringCompare
467 //------------------------------------------------------------------------
469 sal_Int32 AStringToRTLUStringNCompare( const rtl_uString *pRTLUStr,
470 const sal_Char *pAStr,
471 const sal_uInt32 nAStrCount
474 sal_Int32 nCmp = kErrCompareNAStringToRTLUString;
476 if ( ( pRTLUStr != NULL ) && ( pAStr != NULL ) )
478 rtl_uString *pRTLUStrCopy = NULL;
480 rtl_uString_newFromString( &pRTLUStrCopy, pRTLUStr );
482 if ( pRTLUStrCopy != NULL )
484 const sal_Unicode *pUStr = rtl_uString_getStr( pRTLUStrCopy );
486 if ( pUStr != NULL )
488 nCmp = AStringToUStringNCompare( pUStr, pAStr, nAStrCount );
489 } // if
491 rtl_uString_release( pRTLUStrCopy );
493 pRTLUStrCopy = NULL;
494 } // if
495 } // if
497 return nCmp;
498 } // AStringToRTLUStringNCompare
500 //------------------------------------------------------------------------
502 sal_Bool AStringToUStringCopy( sal_Unicode *pDest,
503 const sal_Char *pSrc
506 sal_Bool bCopied = sal_False;
507 sal_uInt32 nCount = AStringLen( pSrc );
508 sal_uInt32 nLen = nCount;
510 if ( ( pDest != NULL )
511 && ( pSrc != NULL )
512 && ( AStringNIsValid( pSrc, nLen ) )
515 while ( nCount >= 0 )
517 *pDest = (unsigned char)*pSrc;
519 pDest++;
520 pSrc++;
522 // Since we are dealing with unsigned integers
523 // we want to make sure that the last number is
524 // indeed zero.
526 if ( nCount > 0 )
528 nCount--;
529 } // if
530 else
532 break;
533 } // else
534 } // while
536 if ( nCount == 0 )
538 bCopied = sal_True;
539 } // if
540 } // if
542 return bCopied;
543 } // AStringToUStringCopy
545 //------------------------------------------------------------------------
547 sal_Bool AStringToUStringNCopy( sal_Unicode *pDest,
548 const sal_Char *pSrc,
549 const sal_uInt32 nSrcLen
552 sal_Bool bCopied = sal_False;
553 sal_uInt32 nCount = nSrcLen;
554 sal_uInt32 nLen = nSrcLen;
556 if ( ( pDest != NULL )
557 && ( pSrc != NULL )
558 && ( AStringNIsValid( pSrc, nLen ) )
561 while ( nCount >= 0 )
563 *pDest = (unsigned char)*pSrc;
565 pDest++;
566 pSrc++;
568 // Since we are dealing with unsigned integers
569 // we want to make sure that the last number is
570 // indeed zero.
572 if ( nCount > 0 )
574 nCount--;
575 } // if
576 else
578 break;
579 } // else
580 } // while
582 if ( nCount == 0 )
584 bCopied = sal_True;
585 } // if
586 } // if
588 return bCopied;
589 } // AStringToUStringNCopy
591 //------------------------------------------------------------------------
592 //------------------------------------------------------------------------