merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / rtl / source / strtmpl.c
blob8c60583b14cc1cbee126fb9a2281ca8dc4cf5cb3
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 /* ======================================================================= */
29 /* Internal C-String help functions which could be used without the */
30 /* String-Class */
31 /* ======================================================================= */
34 inline void rtl_str_ImplCopy( IMPL_RTL_STRCODE* pDest,
35 const IMPL_RTL_STRCODE* pSrc,
36 sal_Int32 nCount )
38 while ( nCount > 0 )
40 *pDest = *pSrc;
41 pDest++;
42 pSrc++;
43 nCount--;
48 #define rtl_str_ImplCopy( _pDest, _pSrc, _nCount ) \
49 { \
50 IMPL_RTL_STRCODE* __mm_pDest = _pDest; \
51 const IMPL_RTL_STRCODE* __mm_pSrc = _pSrc; \
52 sal_Int32 __mm_nCount = _nCount; \
53 while ( __mm_nCount > 0 ) \
54 { \
55 *__mm_pDest = *__mm_pSrc; \
56 __mm_pDest++; \
57 __mm_pSrc++; \
58 __mm_nCount--; \
59 } \
62 /* ======================================================================= */
63 /* C-String functions which could be used without the String-Class */
64 /* ======================================================================= */
66 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( getLength )( const IMPL_RTL_STRCODE* pStr )
68 const IMPL_RTL_STRCODE* pTempStr = pStr;
69 while( *pTempStr )
70 pTempStr++;
71 return pTempStr-pStr;
74 /* ----------------------------------------------------------------------- */
76 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compare )( const IMPL_RTL_STRCODE* pStr1,
77 const IMPL_RTL_STRCODE* pStr2 )
79 sal_Int32 nRet;
80 while ( ((nRet = ((sal_Int32)(IMPL_RTL_USTRCODE(*pStr1)))-
81 ((sal_Int32)(IMPL_RTL_USTRCODE(*pStr2)))) == 0) &&
82 *pStr2 )
84 pStr1++;
85 pStr2++;
88 return nRet;
91 /* ----------------------------------------------------------------------- */
93 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compare_WithLength )( const IMPL_RTL_STRCODE* pStr1,
94 sal_Int32 nStr1Len,
95 const IMPL_RTL_STRCODE* pStr2,
96 sal_Int32 nStr2Len )
98 sal_Int32 nRet = nStr1Len - nStr2Len;
99 int nCount = (nRet <= 0) ? nStr1Len : nStr2Len;
101 --pStr1;
102 --pStr2;
103 while( (--nCount >= 0) && (*++pStr1 == *++pStr2) );
105 if( nCount >= 0 )
106 nRet = ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr1 )))
107 - ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr2 )));
109 return nRet;
112 /* ----------------------------------------------------------------------- */
114 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( shortenedCompare_WithLength )( const IMPL_RTL_STRCODE* pStr1,
115 sal_Int32 nStr1Len,
116 const IMPL_RTL_STRCODE* pStr2,
117 sal_Int32 nStr2Len,
118 sal_Int32 nShortenedLength )
120 const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
121 const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
122 sal_Int32 nRet;
123 while ( (nShortenedLength > 0) &&
124 (pStr1 < pStr1End) && (pStr2 < pStr2End) )
126 nRet = ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr1 )))-
127 ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr2 )));
128 if ( nRet )
129 return nRet;
131 nShortenedLength--;
132 pStr1++;
133 pStr2++;
136 if ( nShortenedLength <= 0 )
137 return 0;
138 return nStr1Len - nStr2Len;
141 /* ----------------------------------------------------------------------- */
143 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( reverseCompare_WithLength )( const IMPL_RTL_STRCODE* pStr1,
144 sal_Int32 nStr1Len,
145 const IMPL_RTL_STRCODE* pStr2,
146 sal_Int32 nStr2Len )
148 const IMPL_RTL_STRCODE* pStr1Run = pStr1+nStr1Len;
149 const IMPL_RTL_STRCODE* pStr2Run = pStr2+nStr2Len;
150 sal_Int32 nRet;
151 while ( (pStr1 < pStr1Run) && (pStr2 < pStr2Run) )
153 pStr1Run--;
154 pStr2Run--;
155 nRet = ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr1Run )))-
156 ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr2Run )));
157 if ( nRet )
158 return nRet;
161 return nStr1Len - nStr2Len;
164 /* ----------------------------------------------------------------------- */
166 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compareIgnoreAsciiCase )( const IMPL_RTL_STRCODE* pStr1,
167 const IMPL_RTL_STRCODE* pStr2 )
169 sal_Int32 nRet;
170 sal_Int32 c1;
171 sal_Int32 c2;
174 /* If character between 'A' and 'Z', than convert it to lowercase */
175 c1 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr1 );
176 c2 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr2 );
177 if ( (c1 >= 65) && (c1 <= 90) )
178 c1 += 32;
179 if ( (c2 >= 65) && (c2 <= 90) )
180 c2 += 32;
181 nRet = c1-c2;
182 if ( nRet != 0 )
183 return nRet;
185 pStr1++;
186 pStr2++;
188 while ( c2 );
190 return 0;
193 /* ----------------------------------------------------------------------- */
195 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compareIgnoreAsciiCase_WithLength )( const IMPL_RTL_STRCODE* pStr1,
196 sal_Int32 nStr1Len,
197 const IMPL_RTL_STRCODE* pStr2,
198 sal_Int32 nStr2Len )
200 const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
201 const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
202 sal_Int32 nRet;
203 sal_Int32 c1;
204 sal_Int32 c2;
205 while ( (pStr1 < pStr1End) && (pStr2 < pStr2End) )
207 /* If character between 'A' and 'Z', than convert it to lowercase */
208 c1 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr1 );
209 c2 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr2 );
210 if ( (c1 >= 65) && (c1 <= 90) )
211 c1 += 32;
212 if ( (c2 >= 65) && (c2 <= 90) )
213 c2 += 32;
214 nRet = c1-c2;
215 if ( nRet != 0 )
216 return nRet;
218 pStr1++;
219 pStr2++;
222 return nStr1Len - nStr2Len;
225 /* ----------------------------------------------------------------------- */
227 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( shortenedCompareIgnoreAsciiCase_WithLength )( const IMPL_RTL_STRCODE* pStr1,
228 sal_Int32 nStr1Len,
229 const IMPL_RTL_STRCODE* pStr2,
230 sal_Int32 nStr2Len,
231 sal_Int32 nShortenedLength )
233 const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
234 const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
235 sal_Int32 nRet;
236 sal_Int32 c1;
237 sal_Int32 c2;
238 while ( (nShortenedLength > 0) &&
239 (pStr1 < pStr1End) && (pStr2 < pStr2End) )
241 /* If character between 'A' and 'Z', than convert it to lowercase */
242 c1 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr1 );
243 c2 = (sal_Int32)IMPL_RTL_USTRCODE( *pStr2 );
244 if ( (c1 >= 65) && (c1 <= 90) )
245 c1 += 32;
246 if ( (c2 >= 65) && (c2 <= 90) )
247 c2 += 32;
248 nRet = c1-c2;
249 if ( nRet != 0 )
250 return nRet;
252 nShortenedLength--;
253 pStr1++;
254 pStr2++;
257 if ( nShortenedLength <= 0 )
258 return 0;
259 return nStr1Len - nStr2Len;
262 /* ----------------------------------------------------------------------- */
264 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( hashCode )( const IMPL_RTL_STRCODE* pStr )
266 return IMPL_RTL_STRNAME( hashCode_WithLength )( pStr, IMPL_RTL_STRNAME( getLength )( pStr ) );
269 /* ----------------------------------------------------------------------- */
271 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( hashCode_WithLength )( const IMPL_RTL_STRCODE* pStr,
272 sal_Int32 nLen )
274 sal_Int32 h = nLen;
276 if ( nLen < 256 )
278 while ( nLen > 0 )
280 h = (h*37) + IMPL_RTL_USTRCODE( *pStr );
281 pStr++;
282 nLen--;
285 else
287 sal_Int32 nSkip;
288 const IMPL_RTL_STRCODE* pEndStr = pStr+nLen-5;
290 /* only sample some characters */
291 /* the first 3, some characters between, and the last 5 */
292 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
293 pStr++;
294 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
295 pStr++;
296 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
297 pStr++;
299 if ( nLen < 32 )
300 nSkip = nLen / 4;
301 else
302 nSkip = nLen / 8;
303 nLen -= 8;
304 while ( nLen > 0 )
306 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
307 pStr += nSkip;
308 nLen -= nSkip;
311 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
312 pEndStr++;
313 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
314 pEndStr++;
315 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
316 pEndStr++;
317 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
318 pEndStr++;
319 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
322 return h;
325 /* ----------------------------------------------------------------------- */
327 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( indexOfChar )( const IMPL_RTL_STRCODE* pStr,
328 IMPL_RTL_STRCODE c )
330 const IMPL_RTL_STRCODE* pTempStr = pStr;
331 while ( *pTempStr )
333 if ( *pTempStr == c )
334 return pTempStr-pStr;
336 pTempStr++;
339 return -1;
342 /* ----------------------------------------------------------------------- */
344 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( indexOfChar_WithLength )( const IMPL_RTL_STRCODE* pStr,
345 sal_Int32 nLen,
346 IMPL_RTL_STRCODE c )
348 const IMPL_RTL_STRCODE* pTempStr = pStr;
349 while ( nLen > 0 )
351 if ( *pTempStr == c )
352 return pTempStr-pStr;
354 pTempStr++;
355 nLen--;
358 return -1;
361 /* ----------------------------------------------------------------------- */
363 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( lastIndexOfChar )( const IMPL_RTL_STRCODE* pStr,
364 IMPL_RTL_STRCODE c )
366 return IMPL_RTL_STRNAME( lastIndexOfChar_WithLength )( pStr, IMPL_RTL_STRNAME( getLength )( pStr ), c );
369 /* ----------------------------------------------------------------------- */
371 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( lastIndexOfChar_WithLength )( const IMPL_RTL_STRCODE* pStr,
372 sal_Int32 nLen,
373 IMPL_RTL_STRCODE c )
375 pStr += nLen;
376 while ( nLen > 0 )
378 nLen--;
379 pStr--;
381 if ( *pStr == c )
382 return nLen;
385 return -1;
388 /* ----------------------------------------------------------------------- */
390 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( indexOfStr )( const IMPL_RTL_STRCODE* pStr,
391 const IMPL_RTL_STRCODE* pSubStr )
393 return IMPL_RTL_STRNAME( indexOfStr_WithLength )( pStr, IMPL_RTL_STRNAME( getLength )( pStr ),
394 pSubStr, IMPL_RTL_STRNAME( getLength )( pSubStr ) );
397 /* ----------------------------------------------------------------------- */
399 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( indexOfStr_WithLength )( const IMPL_RTL_STRCODE* pStr,
400 sal_Int32 nStrLen,
401 const IMPL_RTL_STRCODE* pSubStr,
402 sal_Int32 nSubLen )
404 /* faster search for a single character */
405 if ( nSubLen < 2 )
407 /* an empty SubString is always not foundable */
408 if ( nSubLen == 1 )
410 IMPL_RTL_STRCODE c = *pSubStr;
411 const IMPL_RTL_STRCODE* pTempStr = pStr;
412 while ( nStrLen > 0 )
414 if ( *pTempStr == c )
415 return pTempStr-pStr;
417 pTempStr++;
418 nStrLen--;
422 else
424 const IMPL_RTL_STRCODE* pTempStr = pStr;
425 while ( nStrLen > 0 )
427 if ( *pTempStr == *pSubStr )
429 /* Compare SubString */
430 if ( nSubLen <= nStrLen )
432 const IMPL_RTL_STRCODE* pTempStr1 = pTempStr;
433 const IMPL_RTL_STRCODE* pTempStr2 = pSubStr;
434 sal_Int32 nTempLen = nSubLen;
435 while ( nTempLen )
437 if ( *pTempStr1 != *pTempStr2 )
438 break;
440 pTempStr1++;
441 pTempStr2++;
442 nTempLen--;
445 if ( !nTempLen )
446 return pTempStr-pStr;
448 else
449 break;
452 nStrLen--;
453 pTempStr++;
457 return -1;
460 /* ----------------------------------------------------------------------- */
462 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( lastIndexOfStr )( const IMPL_RTL_STRCODE* pStr,
463 const IMPL_RTL_STRCODE* pSubStr )
465 return IMPL_RTL_STRNAME( lastIndexOfStr_WithLength )( pStr, IMPL_RTL_STRNAME( getLength )( pStr ),
466 pSubStr, IMPL_RTL_STRNAME( getLength )( pSubStr ) );
469 /* ----------------------------------------------------------------------- */
471 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( lastIndexOfStr_WithLength )( const IMPL_RTL_STRCODE* pStr,
472 sal_Int32 nStrLen,
473 const IMPL_RTL_STRCODE* pSubStr,
474 sal_Int32 nSubLen )
476 /* faster search for a single character */
477 if ( nSubLen < 2 )
479 /* an empty SubString is always not foundable */
480 if ( nSubLen == 1 )
482 IMPL_RTL_STRCODE c = *pSubStr;
483 pStr += nStrLen;
484 while ( nStrLen > 0 )
486 nStrLen--;
487 pStr--;
489 if ( *pStr == c )
490 return nStrLen;
494 else
496 pStr += nStrLen;
497 nStrLen -= nSubLen;
498 pStr -= nSubLen;
499 while ( nStrLen >= 0 )
501 const IMPL_RTL_STRCODE* pTempStr1 = pStr;
502 const IMPL_RTL_STRCODE* pTempStr2 = pSubStr;
503 sal_Int32 nTempLen = nSubLen;
504 while ( nTempLen )
506 if ( *pTempStr1 != *pTempStr2 )
507 break;
509 pTempStr1++;
510 pTempStr2++;
511 nTempLen--;
514 if ( !nTempLen )
515 return nStrLen;
517 nStrLen--;
518 pStr--;
522 return -1;
525 /* ----------------------------------------------------------------------- */
527 void SAL_CALL IMPL_RTL_STRNAME( replaceChar )( IMPL_RTL_STRCODE* pStr,
528 IMPL_RTL_STRCODE cOld,
529 IMPL_RTL_STRCODE cNew )
531 while ( *pStr )
533 if ( *pStr == cOld )
534 *pStr = cNew;
536 pStr++;
540 /* ----------------------------------------------------------------------- */
542 void SAL_CALL IMPL_RTL_STRNAME( replaceChar_WithLength )( IMPL_RTL_STRCODE* pStr,
543 sal_Int32 nLen,
544 IMPL_RTL_STRCODE cOld,
545 IMPL_RTL_STRCODE cNew )
547 while ( nLen > 0 )
549 if ( *pStr == cOld )
550 *pStr = cNew;
552 pStr++;
553 nLen--;
557 /* ----------------------------------------------------------------------- */
559 void SAL_CALL IMPL_RTL_STRNAME( toAsciiLowerCase )( IMPL_RTL_STRCODE* pStr )
561 while ( *pStr )
563 /* Between A-Z (65-90), than to lowercase (+32) */
564 if ( (*pStr >= 65) && (*pStr <= 90) )
565 *pStr += 32;
567 pStr++;
571 /* ----------------------------------------------------------------------- */
573 void SAL_CALL IMPL_RTL_STRNAME( toAsciiLowerCase_WithLength )( IMPL_RTL_STRCODE* pStr,
574 sal_Int32 nLen )
576 while ( nLen > 0 )
578 /* Between A-Z (65-90), than to lowercase (+32) */
579 if ( (*pStr >= 65) && (*pStr <= 90) )
580 *pStr += 32;
582 pStr++;
583 nLen--;
587 /* ----------------------------------------------------------------------- */
589 void SAL_CALL IMPL_RTL_STRNAME( toAsciiUpperCase )( IMPL_RTL_STRCODE* pStr )
591 while ( *pStr )
593 /* Between a-z (97-122), than to uppercase (-32) */
594 if ( (*pStr >= 97) && (*pStr <= 122) )
595 *pStr -= 32;
597 pStr++;
601 /* ----------------------------------------------------------------------- */
603 void SAL_CALL IMPL_RTL_STRNAME( toAsciiUpperCase_WithLength )( IMPL_RTL_STRCODE* pStr,
604 sal_Int32 nLen )
606 while ( nLen > 0 )
608 /* Between a-z (97-122), than to uppercase (-32) */
609 if ( (*pStr >= 97) && (*pStr <= 122) )
610 *pStr -= 32;
612 pStr++;
613 nLen--;
617 /* ----------------------------------------------------------------------- */
619 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( trim )( IMPL_RTL_STRCODE* pStr )
621 return IMPL_RTL_STRNAME( trim_WithLength )( pStr, IMPL_RTL_STRNAME( getLength )( pStr ) );
624 /* ----------------------------------------------------------------------- */
626 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( trim_WithLength )( IMPL_RTL_STRCODE* pStr, sal_Int32 nLen )
628 sal_Int32 nPreSpaces = 0;
629 sal_Int32 nPostSpaces = 0;
630 sal_Int32 nIndex = nLen-1;
632 while ( (nPreSpaces < nLen) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pStr+nPreSpaces)) ) )
633 nPreSpaces++;
635 while ( (nIndex > nPreSpaces) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pStr+nIndex)) ) )
637 nPostSpaces++;
638 nIndex--;
641 if ( nPostSpaces )
643 nLen -= nPostSpaces;
644 *(pStr+nLen) = 0;
647 if ( nPreSpaces )
649 IMPL_RTL_STRCODE* pNewStr = pStr+nPreSpaces;
651 nLen -= nPreSpaces;
652 nIndex = nLen;
654 while ( nIndex )
656 *pStr = *pNewStr;
657 pStr++;
658 pNewStr++;
659 nIndex--;
661 *pStr = 0;
664 return nLen;
667 /* ----------------------------------------------------------------------- */
669 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfBoolean )( IMPL_RTL_STRCODE* pStr, sal_Bool b )
671 if ( b )
673 *pStr = 't';
674 pStr++;
675 *pStr = 'r';
676 pStr++;
677 *pStr = 'u';
678 pStr++;
679 *pStr = 'e';
680 pStr++;
681 *pStr = 0;
682 return 4;
684 else
686 *pStr = 'f';
687 pStr++;
688 *pStr = 'a';
689 pStr++;
690 *pStr = 'l';
691 pStr++;
692 *pStr = 's';
693 pStr++;
694 *pStr = 'e';
695 pStr++;
696 *pStr = 0;
697 return 5;
701 /* ----------------------------------------------------------------------- */
703 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfChar )( IMPL_RTL_STRCODE* pStr,
704 IMPL_RTL_STRCODE c )
706 *pStr++ = c;
707 *pStr = 0;
708 return 1;
711 /* ----------------------------------------------------------------------- */
713 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfInt32 )( IMPL_RTL_STRCODE* pStr,
714 sal_Int32 n,
715 sal_Int16 nRadix )
717 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
718 sal_Char* pBuf = aBuf;
719 sal_Int32 nLen = 0;
720 sal_uInt32 nValue;
722 /* Radix must be valid */
723 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
724 nRadix = 10;
726 /* is value negativ */
727 if ( n < 0 )
729 *pStr = '-';
730 pStr++;
731 nLen++;
732 nValue = -n; /* FIXME this code is not portable for n == -2147483648
733 (smallest negative value for sal_Int32) */
735 else
736 nValue = n;
738 /* create a recursive buffer with all values, except the last one */
741 sal_Char nDigit = (sal_Char)(nValue % nRadix);
742 nValue /= nRadix;
743 if ( nDigit > 9 )
744 *pBuf = (nDigit-10) + 'a';
745 else
746 *pBuf = (nDigit + '0' );
747 pBuf++;
749 while ( nValue > 0 );
751 /* copy the values in the right direction into the destination buffer */
754 pBuf--;
755 *pStr = *pBuf;
756 pStr++;
757 nLen++;
759 while ( pBuf != aBuf );
760 *pStr = 0;
762 return nLen;
765 /* ----------------------------------------------------------------------- */
767 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfInt64 )( IMPL_RTL_STRCODE* pStr,
768 sal_Int64 n,
769 sal_Int16 nRadix )
771 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
772 sal_Char* pBuf = aBuf;
773 sal_Int32 nLen = 0;
774 sal_uInt64 nValue;
776 /* Radix must be valid */
777 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
778 nRadix = 10;
780 /* is value negativ */
781 if ( n < 0 )
783 *pStr = '-';
784 pStr++;
785 nLen++;
786 nValue = -n; /* FIXME this code is not portable for
787 n == -9223372036854775808 (smallest negative value for
788 sal_Int64) */
790 else
791 nValue = n;
793 /* create a recursive buffer with all values, except the last one */
796 sal_Char nDigit = (sal_Char)(nValue % nRadix);
797 nValue /= nRadix;
798 if ( nDigit > 9 )
799 *pBuf = (nDigit-10) + 'a';
800 else
801 *pBuf = (nDigit + '0' );
802 pBuf++;
804 while ( nValue > 0 );
806 /* copy the values in the right direction into the destination buffer */
809 pBuf--;
810 *pStr = *pBuf;
811 pStr++;
812 nLen++;
814 while ( pBuf != aBuf );
815 *pStr = 0;
817 return nLen;
820 /* ----------------------------------------------------------------------- */
822 sal_Bool SAL_CALL IMPL_RTL_STRNAME( toBoolean )( const IMPL_RTL_STRCODE* pStr )
824 if ( *pStr == '1' )
825 return sal_True;
827 if ( (*pStr == 'T') || (*pStr == 't') )
829 pStr++;
830 if ( (*pStr == 'R') || (*pStr == 'r') )
832 pStr++;
833 if ( (*pStr == 'U') || (*pStr == 'u') )
835 pStr++;
836 if ( (*pStr == 'E') || (*pStr == 'e') )
837 return sal_True;
842 return sal_False;
845 /* ----------------------------------------------------------------------- */
847 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( toInt32 )( const IMPL_RTL_STRCODE* pStr,
848 sal_Int16 nRadix )
850 sal_Bool bNeg;
851 sal_Int16 nDigit;
852 sal_Int32 n = 0;
854 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
855 nRadix = 10;
857 /* Skip whitespaces */
858 while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) )
859 pStr++;
861 if ( *pStr == '-' )
863 bNeg = sal_True;
864 pStr++;
866 else
868 if ( *pStr == '+' )
869 pStr++;
870 bNeg = sal_False;
873 while ( *pStr )
875 nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
876 if ( nDigit < 0 )
877 break;
879 n *= nRadix;
880 n += nDigit;
882 pStr++;
885 if ( bNeg )
886 return -n;
887 else
888 return n;
891 /* ----------------------------------------------------------------------- */
893 sal_Int64 SAL_CALL IMPL_RTL_STRNAME( toInt64 )( const IMPL_RTL_STRCODE* pStr,
894 sal_Int16 nRadix )
896 sal_Bool bNeg;
897 sal_Int16 nDigit;
898 sal_Int64 n = 0;
900 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
901 nRadix = 10;
903 /* Skip whitespaces */
904 while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) )
905 pStr++;
907 if ( *pStr == '-' )
909 bNeg = sal_True;
910 pStr++;
912 else
914 if ( *pStr == '+' )
915 pStr++;
916 bNeg = sal_False;
919 while ( *pStr )
921 nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
922 if ( nDigit < 0 )
923 break;
925 n *= nRadix;
926 n += nDigit;
928 pStr++;
931 if ( bNeg )
932 return -n;
933 else
934 return n;
937 /* ======================================================================= */
938 /* Internal String-Class help functions */
939 /* ======================================================================= */
941 static IMPL_RTL_STRINGDATA* IMPL_RTL_STRINGNAME( ImplAlloc )( sal_Int32 nLen )
943 IMPL_RTL_STRINGDATA * pData
944 = (SAL_INT_CAST(sal_uInt32, nLen)
945 <= ((SAL_MAX_UINT32 - sizeof (IMPL_RTL_STRINGDATA))
946 / sizeof (IMPL_RTL_STRCODE)))
947 ? (IMPL_RTL_STRINGDATA *) rtl_allocateMemory(
948 sizeof (IMPL_RTL_STRINGDATA) + nLen * sizeof (IMPL_RTL_STRCODE))
949 : NULL;
950 if (pData != NULL) {
951 pData->refCount = 1;
952 pData->length = nLen;
953 pData->buffer[nLen] = 0;
955 return pData;
958 /* ----------------------------------------------------------------------- */
960 static IMPL_RTL_STRCODE* IMPL_RTL_STRINGNAME( ImplNewCopy )( IMPL_RTL_STRINGDATA** ppThis,
961 IMPL_RTL_STRINGDATA* pStr,
962 sal_Int32 nCount )
964 IMPL_RTL_STRCODE* pDest;
965 const IMPL_RTL_STRCODE* pSrc;
966 IMPL_RTL_STRINGDATA* pData = IMPL_RTL_STRINGNAME( ImplAlloc )( pStr->length );
967 OSL_ASSERT(pData != NULL);
969 pDest = pData->buffer;
970 pSrc = pStr->buffer;
971 while ( nCount > 0 )
973 *pDest = *pSrc;
974 pDest++;
975 pSrc++;
976 nCount--;
979 *ppThis = pData;
980 return pDest;
983 /* ======================================================================= */
984 /* String-Class functions */
985 /* ======================================================================= */
987 #define IMPL_RTL_AQUIRE( pThis ) \
989 if (!SAL_STRING_IS_STATIC (pThis)) \
990 osl_incrementInterlockedCount( &((pThis)->refCount) ); \
993 /* ----------------------------------------------------------------------- */
995 void SAL_CALL IMPL_RTL_STRINGNAME( acquire )( IMPL_RTL_STRINGDATA* pThis )
997 IMPL_RTL_AQUIRE( pThis );
1000 /* ----------------------------------------------------------------------- */
1002 void SAL_CALL IMPL_RTL_STRINGNAME( release )( IMPL_RTL_STRINGDATA* pThis )
1004 if (SAL_STRING_IS_STATIC (pThis))
1005 return;
1007 /* OString doesn't have an 'intern' */
1008 #ifdef IMPL_RTL_INTERN
1009 if (SAL_STRING_IS_INTERN (pThis))
1011 internRelease (pThis);
1012 return;
1014 #endif
1016 if ( pThis->refCount == 1 ||
1017 !osl_decrementInterlockedCount( &(pThis->refCount) ) )
1019 rtl_freeMemory( pThis );
1023 /* ----------------------------------------------------------------------- */
1025 void SAL_CALL IMPL_RTL_STRINGNAME( new )( IMPL_RTL_STRINGDATA** ppThis )
1027 if ( *ppThis)
1028 IMPL_RTL_STRINGNAME( release )( *ppThis );
1030 *ppThis = (IMPL_RTL_STRINGDATA*) (&IMPL_RTL_EMPTYSTRING);
1031 IMPL_RTL_AQUIRE( *ppThis );
1034 /* ----------------------------------------------------------------------- */
1036 void SAL_CALL IMPL_RTL_STRINGNAME( new_WithLength )( IMPL_RTL_STRINGDATA** ppThis, sal_Int32 nLen )
1038 if ( nLen <= 0 )
1039 IMPL_RTL_STRINGNAME( new )( ppThis );
1040 else
1042 if ( *ppThis)
1043 IMPL_RTL_STRINGNAME( release )( *ppThis );
1045 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
1046 OSL_ASSERT(*ppThis != NULL);
1047 (*ppThis)->length = 0;
1050 IMPL_RTL_STRCODE* pTempStr = (*ppThis)->buffer;
1051 while ( nLen >= 0 )
1053 *pTempStr = 0;
1054 pTempStr++;
1055 nLen--;
1061 /* ----------------------------------------------------------------------- */
1063 void SAL_CALL IMPL_RTL_STRINGNAME( newFromString )( IMPL_RTL_STRINGDATA** ppThis,
1064 const IMPL_RTL_STRINGDATA* pStr )
1066 IMPL_RTL_STRINGDATA* pOrg;
1068 if ( !pStr->length )
1070 IMPL_RTL_STRINGNAME( new )( ppThis );
1071 return;
1074 pOrg = *ppThis;
1075 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( pStr->length );
1076 OSL_ASSERT(*ppThis != NULL);
1077 rtl_str_ImplCopy( (*ppThis)->buffer, pStr->buffer, pStr->length );
1079 /* must be done at least, if pStr == *ppThis */
1080 if ( pOrg )
1081 IMPL_RTL_STRINGNAME( release )( pOrg );
1084 /* ----------------------------------------------------------------------- */
1086 void SAL_CALL IMPL_RTL_STRINGNAME( newFromStr )( IMPL_RTL_STRINGDATA** ppThis,
1087 const IMPL_RTL_STRCODE* pCharStr )
1089 IMPL_RTL_STRCODE* pBuffer;
1090 IMPL_RTL_STRINGDATA* pOrg;
1091 sal_Int32 nLen;
1093 if ( pCharStr )
1095 const IMPL_RTL_STRCODE* pTempStr = pCharStr;
1096 while( *pTempStr )
1097 pTempStr++;
1098 nLen = pTempStr-pCharStr;
1100 else
1101 nLen = 0;
1103 if ( !nLen )
1105 IMPL_RTL_STRINGNAME( new )( ppThis );
1106 return;
1109 pOrg = *ppThis;
1110 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
1111 OSL_ASSERT(*ppThis != NULL);
1112 pBuffer = (*ppThis)->buffer;
1115 *pBuffer = *pCharStr;
1116 pBuffer++;
1117 pCharStr++;
1119 while ( *pCharStr );
1121 /* must be done at least, if pCharStr == *ppThis */
1122 if ( pOrg )
1123 IMPL_RTL_STRINGNAME( release )( pOrg );
1126 /* ----------------------------------------------------------------------- */
1128 void SAL_CALL IMPL_RTL_STRINGNAME( newFromStr_WithLength )( IMPL_RTL_STRINGDATA** ppThis,
1129 const IMPL_RTL_STRCODE* pCharStr,
1130 sal_Int32 nLen )
1132 IMPL_RTL_STRINGDATA* pOrg;
1134 if ( !pCharStr || (nLen <= 0) )
1136 IMPL_RTL_STRINGNAME( new )( ppThis );
1137 return;
1140 pOrg = *ppThis;
1141 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
1142 OSL_ASSERT(*ppThis != NULL);
1143 rtl_str_ImplCopy( (*ppThis)->buffer, pCharStr, nLen );
1145 /* must be done at least, if pCharStr == *ppThis */
1146 if ( pOrg )
1147 IMPL_RTL_STRINGNAME( release )( pOrg );
1150 /* ----------------------------------------------------------------------- */
1152 void SAL_CALL IMPL_RTL_STRINGNAME( assign )( IMPL_RTL_STRINGDATA** ppThis,
1153 IMPL_RTL_STRINGDATA* pStr )
1155 /* must be done at first, if pStr == *ppThis */
1156 IMPL_RTL_AQUIRE( pStr );
1158 if ( *ppThis )
1159 IMPL_RTL_STRINGNAME( release )( *ppThis );
1161 *ppThis = pStr;
1164 /* ----------------------------------------------------------------------- */
1166 sal_Int32 SAL_CALL IMPL_RTL_STRINGNAME( getLength )( const IMPL_RTL_STRINGDATA* pThis )
1168 return pThis->length;
1171 /* ----------------------------------------------------------------------- */
1173 IMPL_RTL_STRCODE* SAL_CALL IMPL_RTL_STRINGNAME( getStr )( IMPL_RTL_STRINGDATA * pThis )
1175 return pThis->buffer;
1178 /* ----------------------------------------------------------------------- */
1180 void SAL_CALL IMPL_RTL_STRINGNAME( newConcat )( IMPL_RTL_STRINGDATA** ppThis,
1181 IMPL_RTL_STRINGDATA* pLeft,
1182 IMPL_RTL_STRINGDATA* pRight )
1184 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1186 /* Test for 0-Pointer - if not, change newReplaceStrAt! */
1187 if ( !pRight || !pRight->length )
1189 *ppThis = pLeft;
1190 IMPL_RTL_AQUIRE( pLeft );
1192 else if ( !pLeft || !pLeft->length )
1194 *ppThis = pRight;
1195 IMPL_RTL_AQUIRE( pRight );
1197 else
1199 IMPL_RTL_STRINGDATA* pTempStr = IMPL_RTL_STRINGNAME( ImplAlloc )( pLeft->length + pRight->length );
1200 OSL_ASSERT(pTempStr != NULL);
1201 rtl_str_ImplCopy( pTempStr->buffer, pLeft->buffer, pLeft->length );
1202 rtl_str_ImplCopy( pTempStr->buffer+pLeft->length, pRight->buffer, pRight->length );
1203 *ppThis = pTempStr;
1206 /* must be done at least, if left or right == *ppThis */
1207 if ( pOrg )
1208 IMPL_RTL_STRINGNAME( release )( pOrg );
1211 /* ----------------------------------------------------------------------- */
1213 void SAL_CALL IMPL_RTL_STRINGNAME( newReplaceStrAt )( IMPL_RTL_STRINGDATA** ppThis,
1214 IMPL_RTL_STRINGDATA* pStr,
1215 sal_Int32 nIndex,
1216 sal_Int32 nCount,
1217 IMPL_RTL_STRINGDATA* pNewSubStr )
1219 /* Append? */
1220 if ( nIndex >= pStr->length )
1222 /* newConcat test, if pNewSubStr is 0 */
1223 IMPL_RTL_STRINGNAME( newConcat )( ppThis, pStr, pNewSubStr );
1224 return;
1227 /* negativ index? */
1228 if ( nIndex < 0 )
1230 nCount -= nIndex;
1231 nIndex = 0;
1234 /* not more than the String length could be deleted */
1235 if ( nCount >= pStr->length-nIndex )
1237 nCount = pStr->length-nIndex;
1239 /* Assign of NewSubStr? */
1240 if ( !nIndex && (nCount >= pStr->length) )
1242 if ( !pNewSubStr )
1243 IMPL_RTL_STRINGNAME( new )( ppThis );
1244 else
1245 IMPL_RTL_STRINGNAME( assign )( ppThis, pNewSubStr );
1246 return;
1250 /* Assign of Str? */
1251 if ( !nCount && (!pNewSubStr || !pNewSubStr->length) )
1253 IMPL_RTL_STRINGNAME( assign )( ppThis, pStr );
1254 return;
1258 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1259 IMPL_RTL_STRCODE* pBuffer;
1260 sal_Int32 nNewLen;
1262 /* Calculate length of the new string */
1263 nNewLen = pStr->length-nCount;
1264 if ( pNewSubStr )
1265 nNewLen += pNewSubStr->length;
1267 /* Alloc New Buffer */
1268 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nNewLen );
1269 OSL_ASSERT(*ppThis != NULL);
1270 pBuffer = (*ppThis)->buffer;
1271 if ( nIndex )
1273 rtl_str_ImplCopy( pBuffer, pStr->buffer, nIndex );
1274 pBuffer += nIndex;
1276 if ( pNewSubStr && pNewSubStr->length )
1278 rtl_str_ImplCopy( pBuffer, pNewSubStr->buffer, pNewSubStr->length );
1279 pBuffer += pNewSubStr->length;
1281 rtl_str_ImplCopy( pBuffer, pStr->buffer+nIndex+nCount, pStr->length-nIndex-nCount );
1283 /* must be done at least, if pStr or pNewSubStr == *ppThis */
1284 if ( pOrg )
1285 IMPL_RTL_STRINGNAME( release )( pOrg );
1289 /* ----------------------------------------------------------------------- */
1291 void SAL_CALL IMPL_RTL_STRINGNAME( newReplace )( IMPL_RTL_STRINGDATA** ppThis,
1292 IMPL_RTL_STRINGDATA* pStr,
1293 IMPL_RTL_STRCODE cOld,
1294 IMPL_RTL_STRCODE cNew )
1296 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1297 int bChanged = 0;
1298 sal_Int32 nLen = pStr->length;
1299 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1301 while ( nLen > 0 )
1303 if ( *pCharStr == cOld )
1305 /* Copy String */
1306 IMPL_RTL_STRCODE* pNewCharStr = IMPL_RTL_STRINGNAME( ImplNewCopy )( ppThis, pStr, pCharStr-pStr->buffer );
1308 /* replace/copy rest of the string */
1309 if ( pNewCharStr )
1311 *pNewCharStr = cNew;
1312 pNewCharStr++;
1313 pCharStr++;
1314 nLen--;
1316 while ( nLen > 0 )
1318 if ( *pCharStr == cOld )
1319 *pNewCharStr = cNew;
1320 else
1321 *pNewCharStr = *pCharStr;
1323 pNewCharStr++;
1324 pCharStr++;
1325 nLen--;
1329 bChanged = 1;
1330 break;
1333 pCharStr++;
1334 nLen--;
1337 if ( !bChanged )
1339 *ppThis = pStr;
1340 IMPL_RTL_AQUIRE( pStr );
1343 /* must be done at least, if pStr == *ppThis */
1344 if ( pOrg )
1345 IMPL_RTL_STRINGNAME( release )( pOrg );
1348 /* ----------------------------------------------------------------------- */
1350 void SAL_CALL IMPL_RTL_STRINGNAME( newToAsciiLowerCase )( IMPL_RTL_STRINGDATA** ppThis,
1351 IMPL_RTL_STRINGDATA* pStr )
1353 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1354 int bChanged = 0;
1355 sal_Int32 nLen = pStr->length;
1356 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1358 while ( nLen > 0 )
1360 /* Between A-Z (65-90), than to lowercase (+32) */
1361 if ( (*pCharStr >= 65) && (*pCharStr <= 90) )
1363 /* Copy String */
1364 IMPL_RTL_STRCODE* pNewCharStr = IMPL_RTL_STRINGNAME( ImplNewCopy )( ppThis, pStr, pCharStr-pStr->buffer );
1366 /* replace/copy rest of the string */
1367 if ( pNewCharStr )
1369 /* to lowercase (+32) */
1370 *pNewCharStr = *pCharStr+32;
1371 pNewCharStr++;
1372 pCharStr++;
1373 nLen--;
1375 while ( nLen > 0 )
1377 /* Between A-Z (65-90), than to lowercase (+32) */
1378 if ( (*pCharStr >= 65) && (*pCharStr <= 90) )
1379 *pNewCharStr = *pCharStr+32;
1380 else
1381 *pNewCharStr = *pCharStr;
1383 pNewCharStr++;
1384 pCharStr++;
1385 nLen--;
1389 bChanged = 1;
1390 break;
1393 pCharStr++;
1394 nLen--;
1397 if ( !bChanged )
1399 *ppThis = pStr;
1400 IMPL_RTL_AQUIRE( pStr );
1403 /* must be done at least, if pStr == *ppThis */
1404 if ( pOrg )
1405 IMPL_RTL_STRINGNAME( release )( pOrg );
1408 /* ----------------------------------------------------------------------- */
1410 void SAL_CALL IMPL_RTL_STRINGNAME( newToAsciiUpperCase )( IMPL_RTL_STRINGDATA** ppThis,
1411 IMPL_RTL_STRINGDATA* pStr )
1413 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1414 int bChanged = 0;
1415 sal_Int32 nLen = pStr->length;
1416 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1418 while ( nLen > 0 )
1420 /* Between a-z (97-122), than to uppercase (-32) */
1421 if ( (*pCharStr >= 97) && (*pCharStr <= 122) )
1423 /* Copy String */
1424 IMPL_RTL_STRCODE* pNewCharStr = IMPL_RTL_STRINGNAME( ImplNewCopy )( ppThis, pStr, pCharStr-pStr->buffer );
1426 /* replace/copy rest of the string */
1427 if ( pNewCharStr )
1429 /* to uppercase (-32) */
1430 *pNewCharStr = *pCharStr-32;
1431 pNewCharStr++;
1432 pCharStr++;
1433 nLen--;
1435 while ( nLen > 0 )
1437 /* Between a-z (97-122), than to uppercase (-32) */
1438 if ( (*pCharStr >= 97) && (*pCharStr <= 122) )
1439 *pNewCharStr = *pCharStr-32;
1440 else
1441 *pNewCharStr = *pCharStr;
1443 pNewCharStr++;
1444 pCharStr++;
1445 nLen--;
1449 bChanged = 1;
1450 break;
1453 pCharStr++;
1454 nLen--;
1457 if ( !bChanged )
1459 *ppThis = pStr;
1460 IMPL_RTL_AQUIRE( pStr );
1463 /* must be done at least, if pStr == *ppThis */
1464 if ( pOrg )
1465 IMPL_RTL_STRINGNAME( release )( pOrg );
1468 /* ----------------------------------------------------------------------- */
1470 void SAL_CALL IMPL_RTL_STRINGNAME( newTrim )( IMPL_RTL_STRINGDATA** ppThis,
1471 IMPL_RTL_STRINGDATA* pStr )
1473 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1474 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1475 sal_Int32 nPreSpaces = 0;
1476 sal_Int32 nPostSpaces = 0;
1477 sal_Int32 nLen = pStr->length;
1478 sal_Int32 nIndex = nLen-1;
1480 while ( (nPreSpaces < nLen) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pCharStr+nPreSpaces)) ) )
1481 nPreSpaces++;
1483 while ( (nIndex > nPreSpaces) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pCharStr+nIndex)) ) )
1485 nPostSpaces++;
1486 nIndex--;
1489 if ( !nPreSpaces && !nPostSpaces )
1491 *ppThis = pStr;
1492 IMPL_RTL_AQUIRE( pStr );
1494 else
1496 nLen -= nPostSpaces+nPreSpaces;
1497 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
1498 OSL_ASSERT(*ppThis != NULL);
1499 if ( *ppThis )
1500 rtl_str_ImplCopy( (*ppThis)->buffer, pStr->buffer+nPreSpaces, nLen );
1503 /* must be done at least, if pStr == *ppThis */
1504 if ( pOrg )
1505 IMPL_RTL_STRINGNAME( release )( pOrg );
1508 /* ----------------------------------------------------------------------- */
1510 sal_Int32 SAL_CALL IMPL_RTL_STRINGNAME( getToken )( IMPL_RTL_STRINGDATA** ppThis,
1511 IMPL_RTL_STRINGDATA* pStr,
1512 sal_Int32 nToken,
1513 IMPL_RTL_STRCODE cTok,
1514 sal_Int32 nIndex )
1516 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1517 const IMPL_RTL_STRCODE* pCharStrStart;
1518 const IMPL_RTL_STRCODE* pOrgCharStr;
1519 sal_Int32 nLen = pStr->length-nIndex;
1520 sal_Int32 nTokCount = 0;
1522 // Set ppThis to an empty string and return -1 if either nToken or nIndex is
1523 // negative:
1524 if (nIndex < 0) {
1525 nToken = -1;
1528 pCharStr += nIndex;
1529 pOrgCharStr = pCharStr;
1530 pCharStrStart = pCharStr;
1531 while ( nLen > 0 )
1533 if ( *pCharStr == cTok )
1535 nTokCount++;
1537 if ( nTokCount == nToken )
1538 pCharStrStart = pCharStr+1;
1539 else
1541 if ( nTokCount > nToken )
1542 break;
1546 pCharStr++;
1547 nLen--;
1550 if ( (nToken < 0) || (nTokCount < nToken) || (pCharStr == pCharStrStart) )
1552 IMPL_RTL_STRINGNAME( new )( ppThis );
1553 if( (nToken < 0) || (nTokCount < nToken ) )
1554 return -1;
1555 else if( nLen > 0 )
1556 return nIndex+(pCharStr-pOrgCharStr)+1;
1557 else return -1;
1559 else
1561 IMPL_RTL_STRINGNAME( newFromStr_WithLength )( ppThis, pCharStrStart, pCharStr-pCharStrStart );
1562 if ( nLen )
1563 return nIndex+(pCharStr-pOrgCharStr)+1;
1564 else
1565 return -1;