update dev300-m58
[ooovba.git] / sal / qa / OStringBuffer / rtl_String_Utils.cxx
blobaf1d201ba9dd5153b180a15c62a4fe2fd945f15a
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.8 $
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 /* disable assignment within condition expression */
86 #ifdef WNT
87 #pragma warning( disable : 4706 )
88 #endif
89 sal_Char* cpystr( sal_Char* dst, const sal_Char* src )
91 const sal_Char* psrc = src;
92 sal_Char* pdst = dst;
94 while( *pdst++ = *psrc++ );
95 return ( dst );
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;
104 sal_uInt32 i;
106 if ( len >= AStringLen(src) )
108 return( cpystr( dst, src ) );
111 // copy string by char
112 for( i = 0; i < len; i++ )
113 *pdst++ = *psrc++;
114 *pdst = '\0';
116 return ( dst );
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;
124 sal_uInt32 i = 0;
126 while ( (*pBuf1 == *pBuf2) && i < len )
128 (pBuf1)++;
129 (pBuf2)++;
130 i++;
132 return( 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')
143 (pBuf1)++;
144 (pBuf2)++;
146 if (*pBuf1 == '\0' && *pBuf2 == '\0')
147 res = sal_True;
148 else
149 res = sal_False;
150 return (res);
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;
157 sal_uInt32 i = 0;
159 while ( (*pBuf1 == *pBuf2) && i < len )
161 (pBuf1)++;
162 (pBuf2)++;
163 i++;
165 return( 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')
177 (pBuf1)++;
178 (pBuf2)++;
180 if (*pBuf1 == '\0' && *pBuf2 == '\0')
181 res = sal_True;
182 else
183 res = sal_False;
184 return (res);
187 sal_Char* createName( sal_Char* dst, const sal_Char* meth, sal_uInt32 cnt )
189 sal_Char* pdst = dst;
190 sal_Char nstr[16];
191 sal_Char* pstr = nstr;
192 rtl_str_valueOfInt32( pstr, cnt, 10 );
194 cpystr( pdst, meth );
195 cpystr( pdst+ AStringLen(meth), "_" );
197 if ( cnt < 100 )
199 cpystr(pdst + AStringLen(pdst), "0" );
201 if ( cnt < 10 )
203 cpystr(pdst + AStringLen(pdst), "0" );
206 cpystr( pdst + AStringLen(pdst), nstr );
207 return( pdst );
210 //------------------------------------------------------------------------
211 // testing the method compareTo( const OString & aStr )
212 //------------------------------------------------------------------------
213 void makeComment( char *com, const char *str1, const char *str2,
214 sal_Int32 sgn )
216 cpystr(com, str1);
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,
229 const float nX,
230 const float nEPS
233 sal_Bool cmp = sal_False;
235 if ( pStr != NULL )
237 ::rtl::OString aStr(pStr);
239 float actNum = 0;
240 float expNum = nX;
241 float eps = nEPS;
243 actNum = aStr.toFloat();
245 if ( abs( (int)(actNum - expNum) ) <= eps )
247 cmp = sal_True;
248 } // if
249 } // if
251 return cmp;
252 } // AStringToFloatCompare
254 //------------------------------------------------------------------------
256 sal_Bool AStringToDoubleCompare ( const sal_Char *pStr,
257 const double nX,
258 const double nEPS
261 sal_Bool cmp = sal_False;
263 if ( pStr != NULL )
265 ::rtl::OString aStr(pStr);
267 double actNum = 0;
268 double expNum = nX;
269 double eps = nEPS;
271 actNum = aStr.toDouble();
273 if ( abs( (int)(actNum - expNum) ) <= eps )
275 cmp = sal_True;
276 } // if
277 } // if
279 return cmp;
280 } // AStringToDoubleCompare
282 //------------------------------------------------------------------------
285 //------------------------------------------------------------------------
287 sal_uInt32 UStringLen( const sal_Unicode *pUStr )
289 sal_uInt32 nUStrLen = 0;
291 if ( pUStr != NULL )
293 const sal_Unicode *pTempUStr = pUStr;
295 while( *pTempUStr )
297 pTempUStr++;
298 } // while
300 nUStrLen = (sal_uInt32)( pTempUStr - pUStr );
301 } // if
303 return nUStrLen;
304 } // UStringLen
306 //------------------------------------------------------------------------
308 sal_Bool AStringIsValid( const sal_Char *pAStr )
310 if ( pAStr != NULL )
312 sal_uInt32 nLen = AStringLen( pAStr );
313 sal_uChar uChar = 0;
315 while ( *pAStr )
317 uChar = (unsigned char)*pAStr;
319 if ( uChar > 127 )
321 return sal_False;
322 } // if
324 pAStr++;
326 // Since we are dealing with unsigned integers
327 // we want to make sure that the last number is
328 // indeed zero.
330 if ( nLen > 0 )
332 nLen--;
333 } // if
334 else
336 break;
337 } // else
338 } // while
339 } // if
341 return sal_True;
342 } // AStringIsValid
344 //------------------------------------------------------------------------
346 sal_Bool AStringNIsValid( const sal_Char *pAStr,
347 const sal_uInt32 nStrLen
350 sal_uInt32 nLen = nStrLen;
351 sal_uChar uChar = 0;
353 while ( *pAStr )
355 uChar = (unsigned char)*pAStr;
357 if ( uChar > 127 )
359 return sal_False;
360 } // if
362 pAStr++;
364 // Since we are dealing with unsigned integers
365 // we want to make sure that the last number is
366 // indeed zero.
368 if ( nLen > 0 )
370 nLen--;
371 } // if
372 else
374 break;
375 } // else
376 } // while
378 return sal_True;
379 } // AStringNIsValid
381 //------------------------------------------------------------------------
383 static inline sal_Int32 ACharToUCharCompare( const sal_Unicode *pUStr,
384 const sal_Char *pAStr
387 sal_Int32 nCmp = 0;
388 sal_Int32 nUChar = (sal_Int32)*pUStr;
389 sal_Int32 nChar = (sal_Int32)((unsigned char)*pAStr);
391 nCmp = nUChar - nChar;
393 return nCmp;
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 ) )
410 pUStr++;
411 pAStr++;
413 nCmp = ACharToUCharCompare( pUStr, pAStr );
414 } // while
415 } // if
417 return nCmp;
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 ) )
437 pUStr++;
438 pAStr++;
440 nCmp = ACharToUCharCompare( pUStr, pAStr );
442 // Since we are dealing with unsigned integers
443 // we want to make sure that the last number is
444 // indeed zero.
446 if ( nCount > 0 )
448 nCount--;
449 } // if
450 else
452 break;
453 } // else
454 } // while
455 } // if
457 return nCmp;
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 );
478 if ( pUStr != NULL )
480 nCmp = AStringToUStringCompare( pUStr, pAStr );
481 } // if
483 rtl_uString_release( pRTLUStrCopy );
485 pRTLUStrCopy = NULL;
486 } // if
487 } // if
489 return nCmp;
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 );
511 if ( pUStr != NULL )
513 nCmp = AStringToUStringNCompare( pUStr, pAStr, nAStrCount );
514 } // if
516 rtl_uString_release( pRTLUStrCopy );
518 pRTLUStrCopy = NULL;
519 } // if
520 } // if
522 return nCmp;
523 } // AStringToRTLUStringNCompare
525 //------------------------------------------------------------------------
527 sal_Bool AStringToUStringCopy( sal_Unicode *pDest,
528 const sal_Char *pSrc
531 sal_Bool bCopied = sal_False;
532 sal_uInt32 nCount = AStringLen( pSrc );
533 sal_uInt32 nLen = nCount;
535 if ( ( pDest != NULL )
536 && ( pSrc != NULL )
537 && ( AStringNIsValid( pSrc, nLen ) )
540 for (;;)
542 *pDest = (unsigned char)*pSrc;
544 pDest++;
545 pSrc++;
547 // Since we are dealing with unsigned integers
548 // we want to make sure that the last number is
549 // indeed zero.
551 if ( nCount > 0 )
553 nCount--;
554 } // if
555 else
557 break;
558 } // else
559 } // while
561 if ( nCount == 0 )
563 bCopied = sal_True;
564 } // if
565 } // if
567 return bCopied;
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 )
582 && ( pSrc != NULL )
583 && ( AStringNIsValid( pSrc, nLen ) )
586 for (;;)
588 *pDest = (unsigned char)*pSrc;
590 pDest++;
591 pSrc++;
593 // Since we are dealing with unsigned integers
594 // we want to make sure that the last number is
595 // indeed zero.
597 if ( nCount > 0 )
599 nCount--;
600 } // if
601 else
603 break;
604 } // else
605 } // while
607 if ( nCount == 0 )
609 bCopied = sal_True;
610 } // if
611 } // if
613 return bCopied;
614 } // AStringToUStringNCopy
616 //------------------------------------------------------------------------
617 //------------------------------------------------------------------------