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: strtmpl.c,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 /* ======================================================================= */
32 /* Internal C-String help functions which could be used without the */
34 /* ======================================================================= */
37 inline void rtl_str_ImplCopy( IMPL_RTL_STRCODE* pDest,
38 const IMPL_RTL_STRCODE* pSrc,
51 #define rtl_str_ImplCopy( _pDest, _pSrc, _nCount ) \
53 IMPL_RTL_STRCODE* __mm_pDest = _pDest; \
54 const IMPL_RTL_STRCODE* __mm_pSrc = _pSrc; \
55 sal_Int32 __mm_nCount = _nCount; \
56 while ( __mm_nCount > 0 ) \
58 *__mm_pDest = *__mm_pSrc; \
65 /* ======================================================================= */
66 /* C-String functions which could be used without the String-Class */
67 /* ======================================================================= */
69 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( getLength
)( const IMPL_RTL_STRCODE
* pStr
)
71 const IMPL_RTL_STRCODE
* pTempStr
= pStr
;
77 /* ----------------------------------------------------------------------- */
79 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( compare
)( const IMPL_RTL_STRCODE
* pStr1
,
80 const IMPL_RTL_STRCODE
* pStr2
)
83 while ( ((nRet
= ((sal_Int32
)(IMPL_RTL_USTRCODE(*pStr1
)))-
84 ((sal_Int32
)(IMPL_RTL_USTRCODE(*pStr2
)))) == 0) &&
94 /* ----------------------------------------------------------------------- */
96 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( compare_WithLength
)( const IMPL_RTL_STRCODE
* pStr1
,
98 const IMPL_RTL_STRCODE
* pStr2
,
101 sal_Int32 nRet
= nStr1Len
- nStr2Len
;
102 int nCount
= (nRet
<= 0) ? nStr1Len
: nStr2Len
;
106 while( (--nCount
>= 0) && (*++pStr1
== *++pStr2
) );
109 nRet
= ((sal_Int32
)(IMPL_RTL_USTRCODE( *pStr1
)))
110 - ((sal_Int32
)(IMPL_RTL_USTRCODE( *pStr2
)));
115 /* ----------------------------------------------------------------------- */
117 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( shortenedCompare_WithLength
)( const IMPL_RTL_STRCODE
* pStr1
,
119 const IMPL_RTL_STRCODE
* pStr2
,
121 sal_Int32 nShortenedLength
)
123 const IMPL_RTL_STRCODE
* pStr1End
= pStr1
+ nStr1Len
;
124 const IMPL_RTL_STRCODE
* pStr2End
= pStr2
+ nStr2Len
;
126 while ( (nShortenedLength
> 0) &&
127 (pStr1
< pStr1End
) && (pStr2
< pStr2End
) )
129 nRet
= ((sal_Int32
)(IMPL_RTL_USTRCODE( *pStr1
)))-
130 ((sal_Int32
)(IMPL_RTL_USTRCODE( *pStr2
)));
139 if ( nShortenedLength
<= 0 )
141 return nStr1Len
- nStr2Len
;
144 /* ----------------------------------------------------------------------- */
146 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( reverseCompare_WithLength
)( const IMPL_RTL_STRCODE
* pStr1
,
148 const IMPL_RTL_STRCODE
* pStr2
,
151 const IMPL_RTL_STRCODE
* pStr1Run
= pStr1
+nStr1Len
;
152 const IMPL_RTL_STRCODE
* pStr2Run
= pStr2
+nStr2Len
;
154 while ( (pStr1
< pStr1Run
) && (pStr2
< pStr2Run
) )
158 nRet
= ((sal_Int32
)(IMPL_RTL_USTRCODE( *pStr1Run
)))-
159 ((sal_Int32
)(IMPL_RTL_USTRCODE( *pStr2Run
)));
164 return nStr1Len
- nStr2Len
;
167 /* ----------------------------------------------------------------------- */
169 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( compareIgnoreAsciiCase
)( const IMPL_RTL_STRCODE
* pStr1
,
170 const IMPL_RTL_STRCODE
* pStr2
)
177 /* If character between 'A' and 'Z', than convert it to lowercase */
178 c1
= (sal_Int32
)IMPL_RTL_USTRCODE( *pStr1
);
179 c2
= (sal_Int32
)IMPL_RTL_USTRCODE( *pStr2
);
180 if ( (c1
>= 65) && (c1
<= 90) )
182 if ( (c2
>= 65) && (c2
<= 90) )
196 /* ----------------------------------------------------------------------- */
198 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( compareIgnoreAsciiCase_WithLength
)( const IMPL_RTL_STRCODE
* pStr1
,
200 const IMPL_RTL_STRCODE
* pStr2
,
203 const IMPL_RTL_STRCODE
* pStr1End
= pStr1
+ nStr1Len
;
204 const IMPL_RTL_STRCODE
* pStr2End
= pStr2
+ nStr2Len
;
208 while ( (pStr1
< pStr1End
) && (pStr2
< pStr2End
) )
210 /* If character between 'A' and 'Z', than convert it to lowercase */
211 c1
= (sal_Int32
)IMPL_RTL_USTRCODE( *pStr1
);
212 c2
= (sal_Int32
)IMPL_RTL_USTRCODE( *pStr2
);
213 if ( (c1
>= 65) && (c1
<= 90) )
215 if ( (c2
>= 65) && (c2
<= 90) )
225 return nStr1Len
- nStr2Len
;
228 /* ----------------------------------------------------------------------- */
230 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( shortenedCompareIgnoreAsciiCase_WithLength
)( const IMPL_RTL_STRCODE
* pStr1
,
232 const IMPL_RTL_STRCODE
* pStr2
,
234 sal_Int32 nShortenedLength
)
236 const IMPL_RTL_STRCODE
* pStr1End
= pStr1
+ nStr1Len
;
237 const IMPL_RTL_STRCODE
* pStr2End
= pStr2
+ nStr2Len
;
241 while ( (nShortenedLength
> 0) &&
242 (pStr1
< pStr1End
) && (pStr2
< pStr2End
) )
244 /* If character between 'A' and 'Z', than convert it to lowercase */
245 c1
= (sal_Int32
)IMPL_RTL_USTRCODE( *pStr1
);
246 c2
= (sal_Int32
)IMPL_RTL_USTRCODE( *pStr2
);
247 if ( (c1
>= 65) && (c1
<= 90) )
249 if ( (c2
>= 65) && (c2
<= 90) )
260 if ( nShortenedLength
<= 0 )
262 return nStr1Len
- nStr2Len
;
265 /* ----------------------------------------------------------------------- */
267 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( hashCode
)( const IMPL_RTL_STRCODE
* pStr
)
269 return IMPL_RTL_STRNAME( hashCode_WithLength
)( pStr
, IMPL_RTL_STRNAME( getLength
)( pStr
) );
272 /* ----------------------------------------------------------------------- */
274 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( hashCode_WithLength
)( const IMPL_RTL_STRCODE
* pStr
,
283 h
= (h
*37) + IMPL_RTL_USTRCODE( *pStr
);
291 const IMPL_RTL_STRCODE
* pEndStr
= pStr
+nLen
-5;
293 /* only sample some characters */
294 /* the first 3, some characters between, and the last 5 */
295 h
= (h
*39) + IMPL_RTL_USTRCODE( *pStr
);
297 h
= (h
*39) + IMPL_RTL_USTRCODE( *pStr
);
299 h
= (h
*39) + IMPL_RTL_USTRCODE( *pStr
);
309 h
= (h
*39) + IMPL_RTL_USTRCODE( *pStr
);
314 h
= (h
*39) + IMPL_RTL_USTRCODE( *pEndStr
);
316 h
= (h
*39) + IMPL_RTL_USTRCODE( *pEndStr
);
318 h
= (h
*39) + IMPL_RTL_USTRCODE( *pEndStr
);
320 h
= (h
*39) + IMPL_RTL_USTRCODE( *pEndStr
);
322 h
= (h
*39) + IMPL_RTL_USTRCODE( *pEndStr
);
328 /* ----------------------------------------------------------------------- */
330 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( indexOfChar
)( const IMPL_RTL_STRCODE
* pStr
,
333 const IMPL_RTL_STRCODE
* pTempStr
= pStr
;
336 if ( *pTempStr
== c
)
337 return pTempStr
-pStr
;
345 /* ----------------------------------------------------------------------- */
347 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( indexOfChar_WithLength
)( const IMPL_RTL_STRCODE
* pStr
,
351 const IMPL_RTL_STRCODE
* pTempStr
= pStr
;
354 if ( *pTempStr
== c
)
355 return pTempStr
-pStr
;
364 /* ----------------------------------------------------------------------- */
366 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( lastIndexOfChar
)( const IMPL_RTL_STRCODE
* pStr
,
369 return IMPL_RTL_STRNAME( lastIndexOfChar_WithLength
)( pStr
, IMPL_RTL_STRNAME( getLength
)( pStr
), c
);
372 /* ----------------------------------------------------------------------- */
374 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( lastIndexOfChar_WithLength
)( const IMPL_RTL_STRCODE
* pStr
,
391 /* ----------------------------------------------------------------------- */
393 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( indexOfStr
)( const IMPL_RTL_STRCODE
* pStr
,
394 const IMPL_RTL_STRCODE
* pSubStr
)
396 return IMPL_RTL_STRNAME( indexOfStr_WithLength
)( pStr
, IMPL_RTL_STRNAME( getLength
)( pStr
),
397 pSubStr
, IMPL_RTL_STRNAME( getLength
)( pSubStr
) );
400 /* ----------------------------------------------------------------------- */
402 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( indexOfStr_WithLength
)( const IMPL_RTL_STRCODE
* pStr
,
404 const IMPL_RTL_STRCODE
* pSubStr
,
407 /* faster search for a single character */
410 /* an empty SubString is always not foundable */
413 IMPL_RTL_STRCODE c
= *pSubStr
;
414 const IMPL_RTL_STRCODE
* pTempStr
= pStr
;
415 while ( nStrLen
> 0 )
417 if ( *pTempStr
== c
)
418 return pTempStr
-pStr
;
427 const IMPL_RTL_STRCODE
* pTempStr
= pStr
;
428 while ( nStrLen
> 0 )
430 if ( *pTempStr
== *pSubStr
)
432 /* Compare SubString */
433 if ( nSubLen
<= nStrLen
)
435 const IMPL_RTL_STRCODE
* pTempStr1
= pTempStr
;
436 const IMPL_RTL_STRCODE
* pTempStr2
= pSubStr
;
437 sal_Int32 nTempLen
= nSubLen
;
440 if ( *pTempStr1
!= *pTempStr2
)
449 return pTempStr
-pStr
;
463 /* ----------------------------------------------------------------------- */
465 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( lastIndexOfStr
)( const IMPL_RTL_STRCODE
* pStr
,
466 const IMPL_RTL_STRCODE
* pSubStr
)
468 return IMPL_RTL_STRNAME( lastIndexOfStr_WithLength
)( pStr
, IMPL_RTL_STRNAME( getLength
)( pStr
),
469 pSubStr
, IMPL_RTL_STRNAME( getLength
)( pSubStr
) );
472 /* ----------------------------------------------------------------------- */
474 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( lastIndexOfStr_WithLength
)( const IMPL_RTL_STRCODE
* pStr
,
476 const IMPL_RTL_STRCODE
* pSubStr
,
479 /* faster search for a single character */
482 /* an empty SubString is always not foundable */
485 IMPL_RTL_STRCODE c
= *pSubStr
;
487 while ( nStrLen
> 0 )
502 while ( nStrLen
>= 0 )
504 const IMPL_RTL_STRCODE
* pTempStr1
= pStr
;
505 const IMPL_RTL_STRCODE
* pTempStr2
= pSubStr
;
506 sal_Int32 nTempLen
= nSubLen
;
509 if ( *pTempStr1
!= *pTempStr2
)
528 /* ----------------------------------------------------------------------- */
530 void SAL_CALL
IMPL_RTL_STRNAME( replaceChar
)( IMPL_RTL_STRCODE
* pStr
,
531 IMPL_RTL_STRCODE cOld
,
532 IMPL_RTL_STRCODE cNew
)
543 /* ----------------------------------------------------------------------- */
545 void SAL_CALL
IMPL_RTL_STRNAME( replaceChar_WithLength
)( IMPL_RTL_STRCODE
* pStr
,
547 IMPL_RTL_STRCODE cOld
,
548 IMPL_RTL_STRCODE cNew
)
560 /* ----------------------------------------------------------------------- */
562 void SAL_CALL
IMPL_RTL_STRNAME( toAsciiLowerCase
)( IMPL_RTL_STRCODE
* pStr
)
566 /* Between A-Z (65-90), than to lowercase (+32) */
567 if ( (*pStr
>= 65) && (*pStr
<= 90) )
574 /* ----------------------------------------------------------------------- */
576 void SAL_CALL
IMPL_RTL_STRNAME( toAsciiLowerCase_WithLength
)( IMPL_RTL_STRCODE
* pStr
,
581 /* Between A-Z (65-90), than to lowercase (+32) */
582 if ( (*pStr
>= 65) && (*pStr
<= 90) )
590 /* ----------------------------------------------------------------------- */
592 void SAL_CALL
IMPL_RTL_STRNAME( toAsciiUpperCase
)( IMPL_RTL_STRCODE
* pStr
)
596 /* Between a-z (97-122), than to uppercase (-32) */
597 if ( (*pStr
>= 97) && (*pStr
<= 122) )
604 /* ----------------------------------------------------------------------- */
606 void SAL_CALL
IMPL_RTL_STRNAME( toAsciiUpperCase_WithLength
)( IMPL_RTL_STRCODE
* pStr
,
611 /* Between a-z (97-122), than to uppercase (-32) */
612 if ( (*pStr
>= 97) && (*pStr
<= 122) )
620 /* ----------------------------------------------------------------------- */
622 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( trim
)( IMPL_RTL_STRCODE
* pStr
)
624 return IMPL_RTL_STRNAME( trim_WithLength
)( pStr
, IMPL_RTL_STRNAME( getLength
)( pStr
) );
627 /* ----------------------------------------------------------------------- */
629 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( trim_WithLength
)( IMPL_RTL_STRCODE
* pStr
, sal_Int32 nLen
)
631 sal_Int32 nPreSpaces
= 0;
632 sal_Int32 nPostSpaces
= 0;
633 sal_Int32 nIndex
= nLen
-1;
635 while ( (nPreSpaces
< nLen
) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pStr
+nPreSpaces
)) ) )
638 while ( (nIndex
> nPreSpaces
) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pStr
+nIndex
)) ) )
652 IMPL_RTL_STRCODE
* pNewStr
= pStr
+nPreSpaces
;
670 /* ----------------------------------------------------------------------- */
672 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( valueOfBoolean
)( IMPL_RTL_STRCODE
* pStr
, sal_Bool b
)
704 /* ----------------------------------------------------------------------- */
706 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( valueOfChar
)( IMPL_RTL_STRCODE
* pStr
,
714 /* ----------------------------------------------------------------------- */
716 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( valueOfInt32
)( IMPL_RTL_STRCODE
* pStr
,
720 sal_Char aBuf
[RTL_STR_MAX_VALUEOFINT32
];
721 sal_Char
* pBuf
= aBuf
;
725 /* Radix must be valid */
726 if ( (nRadix
< RTL_STR_MIN_RADIX
) || (nRadix
> RTL_STR_MAX_RADIX
) )
729 /* is value negativ */
735 nValue
= -n
; /* FIXME this code is not portable for n == -2147483648
736 (smallest negative value for sal_Int32) */
741 /* create a recursive buffer with all values, except the last one */
744 sal_Char nDigit
= (sal_Char
)(nValue
% nRadix
);
747 *pBuf
= (nDigit
-10) + 'a';
749 *pBuf
= (nDigit
+ '0' );
752 while ( nValue
> 0 );
754 /* copy the values in the right direction into the destination buffer */
762 while ( pBuf
!= aBuf
);
768 /* ----------------------------------------------------------------------- */
770 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( valueOfInt64
)( IMPL_RTL_STRCODE
* pStr
,
774 sal_Char aBuf
[RTL_STR_MAX_VALUEOFINT64
];
775 sal_Char
* pBuf
= aBuf
;
779 /* Radix must be valid */
780 if ( (nRadix
< RTL_STR_MIN_RADIX
) || (nRadix
> RTL_STR_MAX_RADIX
) )
783 /* is value negativ */
789 nValue
= -n
; /* FIXME this code is not portable for
790 n == -9223372036854775808 (smallest negative value for
796 /* create a recursive buffer with all values, except the last one */
799 sal_Char nDigit
= (sal_Char
)(nValue
% nRadix
);
802 *pBuf
= (nDigit
-10) + 'a';
804 *pBuf
= (nDigit
+ '0' );
807 while ( nValue
> 0 );
809 /* copy the values in the right direction into the destination buffer */
817 while ( pBuf
!= aBuf
);
823 /* ----------------------------------------------------------------------- */
825 sal_Bool SAL_CALL
IMPL_RTL_STRNAME( toBoolean
)( const IMPL_RTL_STRCODE
* pStr
)
830 if ( (*pStr
== 'T') || (*pStr
== 't') )
833 if ( (*pStr
== 'R') || (*pStr
== 'r') )
836 if ( (*pStr
== 'U') || (*pStr
== 'u') )
839 if ( (*pStr
== 'E') || (*pStr
== 'e') )
848 /* ----------------------------------------------------------------------- */
850 sal_Int32 SAL_CALL
IMPL_RTL_STRNAME( toInt32
)( const IMPL_RTL_STRCODE
* pStr
,
857 if ( (nRadix
< RTL_STR_MIN_RADIX
) || (nRadix
> RTL_STR_MAX_RADIX
) )
860 /* Skip whitespaces */
861 while ( *pStr
&& rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr
) ) )
878 nDigit
= rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr
), nRadix
);
894 /* ----------------------------------------------------------------------- */
896 sal_Int64 SAL_CALL
IMPL_RTL_STRNAME( toInt64
)( const IMPL_RTL_STRCODE
* pStr
,
903 if ( (nRadix
< RTL_STR_MIN_RADIX
) || (nRadix
> RTL_STR_MAX_RADIX
) )
906 /* Skip whitespaces */
907 while ( *pStr
&& rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr
) ) )
924 nDigit
= rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr
), nRadix
);
940 /* ======================================================================= */
941 /* Internal String-Class help functions */
942 /* ======================================================================= */
944 static IMPL_RTL_STRINGDATA
* IMPL_RTL_STRINGNAME( ImplAlloc
)( sal_Int32 nLen
)
946 IMPL_RTL_STRINGDATA
* pData
947 = (SAL_INT_CAST(sal_uInt32
, nLen
)
948 <= ((SAL_MAX_UINT32
- sizeof (IMPL_RTL_STRINGDATA
))
949 / sizeof (IMPL_RTL_STRCODE
)))
950 ? (IMPL_RTL_STRINGDATA
*) rtl_allocateMemory(
951 sizeof (IMPL_RTL_STRINGDATA
) + nLen
* sizeof (IMPL_RTL_STRCODE
))
955 pData
->length
= nLen
;
956 pData
->buffer
[nLen
] = 0;
961 /* ----------------------------------------------------------------------- */
963 static IMPL_RTL_STRCODE
* IMPL_RTL_STRINGNAME( ImplNewCopy
)( IMPL_RTL_STRINGDATA
** ppThis
,
964 IMPL_RTL_STRINGDATA
* pStr
,
967 IMPL_RTL_STRCODE
* pDest
;
968 const IMPL_RTL_STRCODE
* pSrc
;
969 IMPL_RTL_STRINGDATA
* pData
= IMPL_RTL_STRINGNAME( ImplAlloc
)( pStr
->length
);
970 OSL_ASSERT(pData
!= NULL
);
972 pDest
= pData
->buffer
;
986 /* ======================================================================= */
987 /* String-Class functions */
988 /* ======================================================================= */
990 #define IMPL_RTL_AQUIRE( pThis ) \
992 if (!SAL_STRING_IS_STATIC (pThis)) \
993 osl_incrementInterlockedCount( &((pThis)->refCount) ); \
996 /* ----------------------------------------------------------------------- */
998 void SAL_CALL
IMPL_RTL_STRINGNAME( acquire
)( IMPL_RTL_STRINGDATA
* pThis
)
1000 IMPL_RTL_AQUIRE( pThis
);
1003 /* ----------------------------------------------------------------------- */
1005 void SAL_CALL
IMPL_RTL_STRINGNAME( release
)( IMPL_RTL_STRINGDATA
* pThis
)
1007 if (SAL_STRING_IS_STATIC (pThis
))
1010 /* OString doesn't have an 'intern' */
1011 #ifdef IMPL_RTL_INTERN
1012 if (SAL_STRING_IS_INTERN (pThis
))
1014 internRelease (pThis
);
1019 if ( pThis
->refCount
== 1 ||
1020 !osl_decrementInterlockedCount( &(pThis
->refCount
) ) )
1022 rtl_freeMemory( pThis
);
1026 /* ----------------------------------------------------------------------- */
1028 void SAL_CALL
IMPL_RTL_STRINGNAME( new )( IMPL_RTL_STRINGDATA
** ppThis
)
1031 IMPL_RTL_STRINGNAME( release
)( *ppThis
);
1033 *ppThis
= (IMPL_RTL_STRINGDATA
*) (&IMPL_RTL_EMPTYSTRING
);
1034 IMPL_RTL_AQUIRE( *ppThis
);
1037 /* ----------------------------------------------------------------------- */
1039 void SAL_CALL
IMPL_RTL_STRINGNAME( new_WithLength
)( IMPL_RTL_STRINGDATA
** ppThis
, sal_Int32 nLen
)
1042 IMPL_RTL_STRINGNAME( new )( ppThis
);
1046 IMPL_RTL_STRINGNAME( release
)( *ppThis
);
1048 *ppThis
= IMPL_RTL_STRINGNAME( ImplAlloc
)( nLen
);
1049 OSL_ASSERT(*ppThis
!= NULL
);
1050 (*ppThis
)->length
= 0;
1053 IMPL_RTL_STRCODE
* pTempStr
= (*ppThis
)->buffer
;
1064 /* ----------------------------------------------------------------------- */
1066 void SAL_CALL
IMPL_RTL_STRINGNAME( newFromString
)( IMPL_RTL_STRINGDATA
** ppThis
,
1067 const IMPL_RTL_STRINGDATA
* pStr
)
1069 IMPL_RTL_STRINGDATA
* pOrg
;
1071 if ( !pStr
->length
)
1073 IMPL_RTL_STRINGNAME( new )( ppThis
);
1078 *ppThis
= IMPL_RTL_STRINGNAME( ImplAlloc
)( pStr
->length
);
1079 OSL_ASSERT(*ppThis
!= NULL
);
1080 rtl_str_ImplCopy( (*ppThis
)->buffer
, pStr
->buffer
, pStr
->length
);
1082 /* must be done at least, if pStr == *ppThis */
1084 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1087 /* ----------------------------------------------------------------------- */
1089 void SAL_CALL
IMPL_RTL_STRINGNAME( newFromStr
)( IMPL_RTL_STRINGDATA
** ppThis
,
1090 const IMPL_RTL_STRCODE
* pCharStr
)
1092 IMPL_RTL_STRCODE
* pBuffer
;
1093 IMPL_RTL_STRINGDATA
* pOrg
;
1098 const IMPL_RTL_STRCODE
* pTempStr
= pCharStr
;
1101 nLen
= pTempStr
-pCharStr
;
1108 IMPL_RTL_STRINGNAME( new )( ppThis
);
1113 *ppThis
= IMPL_RTL_STRINGNAME( ImplAlloc
)( nLen
);
1114 OSL_ASSERT(*ppThis
!= NULL
);
1115 pBuffer
= (*ppThis
)->buffer
;
1118 *pBuffer
= *pCharStr
;
1122 while ( *pCharStr
);
1124 /* must be done at least, if pCharStr == *ppThis */
1126 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1129 /* ----------------------------------------------------------------------- */
1131 void SAL_CALL
IMPL_RTL_STRINGNAME( newFromStr_WithLength
)( IMPL_RTL_STRINGDATA
** ppThis
,
1132 const IMPL_RTL_STRCODE
* pCharStr
,
1135 IMPL_RTL_STRINGDATA
* pOrg
;
1137 if ( !pCharStr
|| (nLen
<= 0) )
1139 IMPL_RTL_STRINGNAME( new )( ppThis
);
1144 *ppThis
= IMPL_RTL_STRINGNAME( ImplAlloc
)( nLen
);
1145 OSL_ASSERT(*ppThis
!= NULL
);
1146 rtl_str_ImplCopy( (*ppThis
)->buffer
, pCharStr
, nLen
);
1148 /* must be done at least, if pCharStr == *ppThis */
1150 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1153 /* ----------------------------------------------------------------------- */
1155 void SAL_CALL
IMPL_RTL_STRINGNAME( assign
)( IMPL_RTL_STRINGDATA
** ppThis
,
1156 IMPL_RTL_STRINGDATA
* pStr
)
1158 /* must be done at first, if pStr == *ppThis */
1159 IMPL_RTL_AQUIRE( pStr
);
1162 IMPL_RTL_STRINGNAME( release
)( *ppThis
);
1167 /* ----------------------------------------------------------------------- */
1169 sal_Int32 SAL_CALL
IMPL_RTL_STRINGNAME( getLength
)( const IMPL_RTL_STRINGDATA
* pThis
)
1171 return pThis
->length
;
1174 /* ----------------------------------------------------------------------- */
1176 IMPL_RTL_STRCODE
* SAL_CALL
IMPL_RTL_STRINGNAME( getStr
)( IMPL_RTL_STRINGDATA
* pThis
)
1178 return pThis
->buffer
;
1181 /* ----------------------------------------------------------------------- */
1183 void SAL_CALL
IMPL_RTL_STRINGNAME( newConcat
)( IMPL_RTL_STRINGDATA
** ppThis
,
1184 IMPL_RTL_STRINGDATA
* pLeft
,
1185 IMPL_RTL_STRINGDATA
* pRight
)
1187 IMPL_RTL_STRINGDATA
* pOrg
= *ppThis
;
1189 /* Test for 0-Pointer - if not, change newReplaceStrAt! */
1190 if ( !pRight
|| !pRight
->length
)
1193 IMPL_RTL_AQUIRE( pLeft
);
1195 else if ( !pLeft
|| !pLeft
->length
)
1198 IMPL_RTL_AQUIRE( pRight
);
1202 IMPL_RTL_STRINGDATA
* pTempStr
= IMPL_RTL_STRINGNAME( ImplAlloc
)( pLeft
->length
+ pRight
->length
);
1203 OSL_ASSERT(pTempStr
!= NULL
);
1204 rtl_str_ImplCopy( pTempStr
->buffer
, pLeft
->buffer
, pLeft
->length
);
1205 rtl_str_ImplCopy( pTempStr
->buffer
+pLeft
->length
, pRight
->buffer
, pRight
->length
);
1209 /* must be done at least, if left or right == *ppThis */
1211 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1214 /* ----------------------------------------------------------------------- */
1216 void SAL_CALL
IMPL_RTL_STRINGNAME( newReplaceStrAt
)( IMPL_RTL_STRINGDATA
** ppThis
,
1217 IMPL_RTL_STRINGDATA
* pStr
,
1220 IMPL_RTL_STRINGDATA
* pNewSubStr
)
1223 if ( nIndex
>= pStr
->length
)
1225 /* newConcat test, if pNewSubStr is 0 */
1226 IMPL_RTL_STRINGNAME( newConcat
)( ppThis
, pStr
, pNewSubStr
);
1230 /* negativ index? */
1237 /* not more than the String length could be deleted */
1238 if ( nCount
>= pStr
->length
-nIndex
)
1240 nCount
= pStr
->length
-nIndex
;
1242 /* Assign of NewSubStr? */
1243 if ( !nIndex
&& (nCount
>= pStr
->length
) )
1246 IMPL_RTL_STRINGNAME( new )( ppThis
);
1248 IMPL_RTL_STRINGNAME( assign
)( ppThis
, pNewSubStr
);
1253 /* Assign of Str? */
1254 if ( !nCount
&& (!pNewSubStr
|| !pNewSubStr
->length
) )
1256 IMPL_RTL_STRINGNAME( assign
)( ppThis
, pStr
);
1261 IMPL_RTL_STRINGDATA
* pOrg
= *ppThis
;
1262 IMPL_RTL_STRCODE
* pBuffer
;
1265 /* Calculate length of the new string */
1266 nNewLen
= pStr
->length
-nCount
;
1268 nNewLen
+= pNewSubStr
->length
;
1270 /* Alloc New Buffer */
1271 *ppThis
= IMPL_RTL_STRINGNAME( ImplAlloc
)( nNewLen
);
1272 OSL_ASSERT(*ppThis
!= NULL
);
1273 pBuffer
= (*ppThis
)->buffer
;
1276 rtl_str_ImplCopy( pBuffer
, pStr
->buffer
, nIndex
);
1279 if ( pNewSubStr
&& pNewSubStr
->length
)
1281 rtl_str_ImplCopy( pBuffer
, pNewSubStr
->buffer
, pNewSubStr
->length
);
1282 pBuffer
+= pNewSubStr
->length
;
1284 rtl_str_ImplCopy( pBuffer
, pStr
->buffer
+nIndex
+nCount
, pStr
->length
-nIndex
-nCount
);
1286 /* must be done at least, if pStr or pNewSubStr == *ppThis */
1288 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1292 /* ----------------------------------------------------------------------- */
1294 void SAL_CALL
IMPL_RTL_STRINGNAME( newReplace
)( IMPL_RTL_STRINGDATA
** ppThis
,
1295 IMPL_RTL_STRINGDATA
* pStr
,
1296 IMPL_RTL_STRCODE cOld
,
1297 IMPL_RTL_STRCODE cNew
)
1299 IMPL_RTL_STRINGDATA
* pOrg
= *ppThis
;
1301 sal_Int32 nLen
= pStr
->length
;
1302 const IMPL_RTL_STRCODE
* pCharStr
= pStr
->buffer
;
1306 if ( *pCharStr
== cOld
)
1309 IMPL_RTL_STRCODE
* pNewCharStr
= IMPL_RTL_STRINGNAME( ImplNewCopy
)( ppThis
, pStr
, pCharStr
-pStr
->buffer
);
1311 /* replace/copy rest of the string */
1314 *pNewCharStr
= cNew
;
1321 if ( *pCharStr
== cOld
)
1322 *pNewCharStr
= cNew
;
1324 *pNewCharStr
= *pCharStr
;
1343 IMPL_RTL_AQUIRE( pStr
);
1346 /* must be done at least, if pStr == *ppThis */
1348 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1351 /* ----------------------------------------------------------------------- */
1353 void SAL_CALL
IMPL_RTL_STRINGNAME( newToAsciiLowerCase
)( IMPL_RTL_STRINGDATA
** ppThis
,
1354 IMPL_RTL_STRINGDATA
* pStr
)
1356 IMPL_RTL_STRINGDATA
* pOrg
= *ppThis
;
1358 sal_Int32 nLen
= pStr
->length
;
1359 const IMPL_RTL_STRCODE
* pCharStr
= pStr
->buffer
;
1363 /* Between A-Z (65-90), than to lowercase (+32) */
1364 if ( (*pCharStr
>= 65) && (*pCharStr
<= 90) )
1367 IMPL_RTL_STRCODE
* pNewCharStr
= IMPL_RTL_STRINGNAME( ImplNewCopy
)( ppThis
, pStr
, pCharStr
-pStr
->buffer
);
1369 /* replace/copy rest of the string */
1372 /* to lowercase (+32) */
1373 *pNewCharStr
= *pCharStr
+32;
1380 /* Between A-Z (65-90), than to lowercase (+32) */
1381 if ( (*pCharStr
>= 65) && (*pCharStr
<= 90) )
1382 *pNewCharStr
= *pCharStr
+32;
1384 *pNewCharStr
= *pCharStr
;
1403 IMPL_RTL_AQUIRE( pStr
);
1406 /* must be done at least, if pStr == *ppThis */
1408 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1411 /* ----------------------------------------------------------------------- */
1413 void SAL_CALL
IMPL_RTL_STRINGNAME( newToAsciiUpperCase
)( IMPL_RTL_STRINGDATA
** ppThis
,
1414 IMPL_RTL_STRINGDATA
* pStr
)
1416 IMPL_RTL_STRINGDATA
* pOrg
= *ppThis
;
1418 sal_Int32 nLen
= pStr
->length
;
1419 const IMPL_RTL_STRCODE
* pCharStr
= pStr
->buffer
;
1423 /* Between a-z (97-122), than to uppercase (-32) */
1424 if ( (*pCharStr
>= 97) && (*pCharStr
<= 122) )
1427 IMPL_RTL_STRCODE
* pNewCharStr
= IMPL_RTL_STRINGNAME( ImplNewCopy
)( ppThis
, pStr
, pCharStr
-pStr
->buffer
);
1429 /* replace/copy rest of the string */
1432 /* to uppercase (-32) */
1433 *pNewCharStr
= *pCharStr
-32;
1440 /* Between a-z (97-122), than to uppercase (-32) */
1441 if ( (*pCharStr
>= 97) && (*pCharStr
<= 122) )
1442 *pNewCharStr
= *pCharStr
-32;
1444 *pNewCharStr
= *pCharStr
;
1463 IMPL_RTL_AQUIRE( pStr
);
1466 /* must be done at least, if pStr == *ppThis */
1468 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1471 /* ----------------------------------------------------------------------- */
1473 void SAL_CALL
IMPL_RTL_STRINGNAME( newTrim
)( IMPL_RTL_STRINGDATA
** ppThis
,
1474 IMPL_RTL_STRINGDATA
* pStr
)
1476 IMPL_RTL_STRINGDATA
* pOrg
= *ppThis
;
1477 const IMPL_RTL_STRCODE
* pCharStr
= pStr
->buffer
;
1478 sal_Int32 nPreSpaces
= 0;
1479 sal_Int32 nPostSpaces
= 0;
1480 sal_Int32 nLen
= pStr
->length
;
1481 sal_Int32 nIndex
= nLen
-1;
1483 while ( (nPreSpaces
< nLen
) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pCharStr
+nPreSpaces
)) ) )
1486 while ( (nIndex
> nPreSpaces
) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pCharStr
+nIndex
)) ) )
1492 if ( !nPreSpaces
&& !nPostSpaces
)
1495 IMPL_RTL_AQUIRE( pStr
);
1499 nLen
-= nPostSpaces
+nPreSpaces
;
1500 *ppThis
= IMPL_RTL_STRINGNAME( ImplAlloc
)( nLen
);
1501 OSL_ASSERT(*ppThis
!= NULL
);
1503 rtl_str_ImplCopy( (*ppThis
)->buffer
, pStr
->buffer
+nPreSpaces
, nLen
);
1506 /* must be done at least, if pStr == *ppThis */
1508 IMPL_RTL_STRINGNAME( release
)( pOrg
);
1511 /* ----------------------------------------------------------------------- */
1513 sal_Int32 SAL_CALL
IMPL_RTL_STRINGNAME( getToken
)( IMPL_RTL_STRINGDATA
** ppThis
,
1514 IMPL_RTL_STRINGDATA
* pStr
,
1516 IMPL_RTL_STRCODE cTok
,
1519 const IMPL_RTL_STRCODE
* pCharStr
= pStr
->buffer
;
1520 const IMPL_RTL_STRCODE
* pCharStrStart
;
1521 const IMPL_RTL_STRCODE
* pOrgCharStr
;
1522 sal_Int32 nLen
= pStr
->length
-nIndex
;
1523 sal_Int32 nTokCount
= 0;
1525 // Set ppThis to an empty string and return -1 if either nToken or nIndex is
1532 pOrgCharStr
= pCharStr
;
1533 pCharStrStart
= pCharStr
;
1536 if ( *pCharStr
== cTok
)
1540 if ( nTokCount
== nToken
)
1541 pCharStrStart
= pCharStr
+1;
1544 if ( nTokCount
> nToken
)
1553 if ( (nToken
< 0) || (nTokCount
< nToken
) || (pCharStr
== pCharStrStart
) )
1555 IMPL_RTL_STRINGNAME( new )( ppThis
);
1556 if( (nToken
< 0) || (nTokCount
< nToken
) )
1559 return nIndex
+(pCharStr
-pOrgCharStr
)+1;
1564 IMPL_RTL_STRINGNAME( newFromStr_WithLength
)( ppThis
, pCharStrStart
, pCharStr
-pCharStrStart
);
1566 return nIndex
+(pCharStr
-pOrgCharStr
)+1;