update dev300-m58
[ooovba.git] / sal / rtl / source / strtmpl.c
blobe01f935bd5d9fe7d85022f4c0f626836caa0b3be
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: strtmpl.c,v $
10 * $Revision: 1.26 $
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 */
33 /* String-Class */
34 /* ======================================================================= */
37 inline void rtl_str_ImplCopy( IMPL_RTL_STRCODE* pDest,
38 const IMPL_RTL_STRCODE* pSrc,
39 sal_Int32 nCount )
41 while ( nCount > 0 )
43 *pDest = *pSrc;
44 pDest++;
45 pSrc++;
46 nCount--;
51 #define rtl_str_ImplCopy( _pDest, _pSrc, _nCount ) \
52 { \
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 ) \
57 { \
58 *__mm_pDest = *__mm_pSrc; \
59 __mm_pDest++; \
60 __mm_pSrc++; \
61 __mm_nCount--; \
62 } \
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;
72 while( *pTempStr )
73 pTempStr++;
74 return pTempStr-pStr;
77 /* ----------------------------------------------------------------------- */
79 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compare )( const IMPL_RTL_STRCODE* pStr1,
80 const IMPL_RTL_STRCODE* pStr2 )
82 sal_Int32 nRet;
83 while ( ((nRet = ((sal_Int32)(IMPL_RTL_USTRCODE(*pStr1)))-
84 ((sal_Int32)(IMPL_RTL_USTRCODE(*pStr2)))) == 0) &&
85 *pStr2 )
87 pStr1++;
88 pStr2++;
91 return nRet;
94 /* ----------------------------------------------------------------------- */
96 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compare_WithLength )( const IMPL_RTL_STRCODE* pStr1,
97 sal_Int32 nStr1Len,
98 const IMPL_RTL_STRCODE* pStr2,
99 sal_Int32 nStr2Len )
101 sal_Int32 nRet = nStr1Len - nStr2Len;
102 int nCount = (nRet <= 0) ? nStr1Len : nStr2Len;
104 --pStr1;
105 --pStr2;
106 while( (--nCount >= 0) && (*++pStr1 == *++pStr2) );
108 if( nCount >= 0 )
109 nRet = ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr1 )))
110 - ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr2 )));
112 return nRet;
115 /* ----------------------------------------------------------------------- */
117 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( shortenedCompare_WithLength )( const IMPL_RTL_STRCODE* pStr1,
118 sal_Int32 nStr1Len,
119 const IMPL_RTL_STRCODE* pStr2,
120 sal_Int32 nStr2Len,
121 sal_Int32 nShortenedLength )
123 const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
124 const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
125 sal_Int32 nRet;
126 while ( (nShortenedLength > 0) &&
127 (pStr1 < pStr1End) && (pStr2 < pStr2End) )
129 nRet = ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr1 )))-
130 ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr2 )));
131 if ( nRet )
132 return nRet;
134 nShortenedLength--;
135 pStr1++;
136 pStr2++;
139 if ( nShortenedLength <= 0 )
140 return 0;
141 return nStr1Len - nStr2Len;
144 /* ----------------------------------------------------------------------- */
146 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( reverseCompare_WithLength )( const IMPL_RTL_STRCODE* pStr1,
147 sal_Int32 nStr1Len,
148 const IMPL_RTL_STRCODE* pStr2,
149 sal_Int32 nStr2Len )
151 const IMPL_RTL_STRCODE* pStr1Run = pStr1+nStr1Len;
152 const IMPL_RTL_STRCODE* pStr2Run = pStr2+nStr2Len;
153 sal_Int32 nRet;
154 while ( (pStr1 < pStr1Run) && (pStr2 < pStr2Run) )
156 pStr1Run--;
157 pStr2Run--;
158 nRet = ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr1Run )))-
159 ((sal_Int32)(IMPL_RTL_USTRCODE( *pStr2Run )));
160 if ( nRet )
161 return nRet;
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 )
172 sal_Int32 nRet;
173 sal_Int32 c1;
174 sal_Int32 c2;
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) )
181 c1 += 32;
182 if ( (c2 >= 65) && (c2 <= 90) )
183 c2 += 32;
184 nRet = c1-c2;
185 if ( nRet != 0 )
186 return nRet;
188 pStr1++;
189 pStr2++;
191 while ( c2 );
193 return 0;
196 /* ----------------------------------------------------------------------- */
198 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( compareIgnoreAsciiCase_WithLength )( const IMPL_RTL_STRCODE* pStr1,
199 sal_Int32 nStr1Len,
200 const IMPL_RTL_STRCODE* pStr2,
201 sal_Int32 nStr2Len )
203 const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
204 const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
205 sal_Int32 nRet;
206 sal_Int32 c1;
207 sal_Int32 c2;
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) )
214 c1 += 32;
215 if ( (c2 >= 65) && (c2 <= 90) )
216 c2 += 32;
217 nRet = c1-c2;
218 if ( nRet != 0 )
219 return nRet;
221 pStr1++;
222 pStr2++;
225 return nStr1Len - nStr2Len;
228 /* ----------------------------------------------------------------------- */
230 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( shortenedCompareIgnoreAsciiCase_WithLength )( const IMPL_RTL_STRCODE* pStr1,
231 sal_Int32 nStr1Len,
232 const IMPL_RTL_STRCODE* pStr2,
233 sal_Int32 nStr2Len,
234 sal_Int32 nShortenedLength )
236 const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
237 const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
238 sal_Int32 nRet;
239 sal_Int32 c1;
240 sal_Int32 c2;
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) )
248 c1 += 32;
249 if ( (c2 >= 65) && (c2 <= 90) )
250 c2 += 32;
251 nRet = c1-c2;
252 if ( nRet != 0 )
253 return nRet;
255 nShortenedLength--;
256 pStr1++;
257 pStr2++;
260 if ( nShortenedLength <= 0 )
261 return 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,
275 sal_Int32 nLen )
277 sal_Int32 h = nLen;
279 if ( nLen < 256 )
281 while ( nLen > 0 )
283 h = (h*37) + IMPL_RTL_USTRCODE( *pStr );
284 pStr++;
285 nLen--;
288 else
290 sal_Int32 nSkip;
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 );
296 pStr++;
297 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
298 pStr++;
299 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
300 pStr++;
302 if ( nLen < 32 )
303 nSkip = nLen / 4;
304 else
305 nSkip = nLen / 8;
306 nLen -= 8;
307 while ( nLen > 0 )
309 h = (h*39) + IMPL_RTL_USTRCODE( *pStr );
310 pStr += nSkip;
311 nLen -= nSkip;
314 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
315 pEndStr++;
316 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
317 pEndStr++;
318 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
319 pEndStr++;
320 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
321 pEndStr++;
322 h = (h*39) + IMPL_RTL_USTRCODE( *pEndStr );
325 return h;
328 /* ----------------------------------------------------------------------- */
330 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( indexOfChar )( const IMPL_RTL_STRCODE* pStr,
331 IMPL_RTL_STRCODE c )
333 const IMPL_RTL_STRCODE* pTempStr = pStr;
334 while ( *pTempStr )
336 if ( *pTempStr == c )
337 return pTempStr-pStr;
339 pTempStr++;
342 return -1;
345 /* ----------------------------------------------------------------------- */
347 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( indexOfChar_WithLength )( const IMPL_RTL_STRCODE* pStr,
348 sal_Int32 nLen,
349 IMPL_RTL_STRCODE c )
351 const IMPL_RTL_STRCODE* pTempStr = pStr;
352 while ( nLen > 0 )
354 if ( *pTempStr == c )
355 return pTempStr-pStr;
357 pTempStr++;
358 nLen--;
361 return -1;
364 /* ----------------------------------------------------------------------- */
366 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( lastIndexOfChar )( const IMPL_RTL_STRCODE* pStr,
367 IMPL_RTL_STRCODE c )
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,
375 sal_Int32 nLen,
376 IMPL_RTL_STRCODE c )
378 pStr += nLen;
379 while ( nLen > 0 )
381 nLen--;
382 pStr--;
384 if ( *pStr == c )
385 return nLen;
388 return -1;
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,
403 sal_Int32 nStrLen,
404 const IMPL_RTL_STRCODE* pSubStr,
405 sal_Int32 nSubLen )
407 /* faster search for a single character */
408 if ( nSubLen < 2 )
410 /* an empty SubString is always not foundable */
411 if ( nSubLen == 1 )
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;
420 pTempStr++;
421 nStrLen--;
425 else
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;
438 while ( nTempLen )
440 if ( *pTempStr1 != *pTempStr2 )
441 break;
443 pTempStr1++;
444 pTempStr2++;
445 nTempLen--;
448 if ( !nTempLen )
449 return pTempStr-pStr;
451 else
452 break;
455 nStrLen--;
456 pTempStr++;
460 return -1;
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,
475 sal_Int32 nStrLen,
476 const IMPL_RTL_STRCODE* pSubStr,
477 sal_Int32 nSubLen )
479 /* faster search for a single character */
480 if ( nSubLen < 2 )
482 /* an empty SubString is always not foundable */
483 if ( nSubLen == 1 )
485 IMPL_RTL_STRCODE c = *pSubStr;
486 pStr += nStrLen;
487 while ( nStrLen > 0 )
489 nStrLen--;
490 pStr--;
492 if ( *pStr == c )
493 return nStrLen;
497 else
499 pStr += nStrLen;
500 nStrLen -= nSubLen;
501 pStr -= nSubLen;
502 while ( nStrLen >= 0 )
504 const IMPL_RTL_STRCODE* pTempStr1 = pStr;
505 const IMPL_RTL_STRCODE* pTempStr2 = pSubStr;
506 sal_Int32 nTempLen = nSubLen;
507 while ( nTempLen )
509 if ( *pTempStr1 != *pTempStr2 )
510 break;
512 pTempStr1++;
513 pTempStr2++;
514 nTempLen--;
517 if ( !nTempLen )
518 return nStrLen;
520 nStrLen--;
521 pStr--;
525 return -1;
528 /* ----------------------------------------------------------------------- */
530 void SAL_CALL IMPL_RTL_STRNAME( replaceChar )( IMPL_RTL_STRCODE* pStr,
531 IMPL_RTL_STRCODE cOld,
532 IMPL_RTL_STRCODE cNew )
534 while ( *pStr )
536 if ( *pStr == cOld )
537 *pStr = cNew;
539 pStr++;
543 /* ----------------------------------------------------------------------- */
545 void SAL_CALL IMPL_RTL_STRNAME( replaceChar_WithLength )( IMPL_RTL_STRCODE* pStr,
546 sal_Int32 nLen,
547 IMPL_RTL_STRCODE cOld,
548 IMPL_RTL_STRCODE cNew )
550 while ( nLen > 0 )
552 if ( *pStr == cOld )
553 *pStr = cNew;
555 pStr++;
556 nLen--;
560 /* ----------------------------------------------------------------------- */
562 void SAL_CALL IMPL_RTL_STRNAME( toAsciiLowerCase )( IMPL_RTL_STRCODE* pStr )
564 while ( *pStr )
566 /* Between A-Z (65-90), than to lowercase (+32) */
567 if ( (*pStr >= 65) && (*pStr <= 90) )
568 *pStr += 32;
570 pStr++;
574 /* ----------------------------------------------------------------------- */
576 void SAL_CALL IMPL_RTL_STRNAME( toAsciiLowerCase_WithLength )( IMPL_RTL_STRCODE* pStr,
577 sal_Int32 nLen )
579 while ( nLen > 0 )
581 /* Between A-Z (65-90), than to lowercase (+32) */
582 if ( (*pStr >= 65) && (*pStr <= 90) )
583 *pStr += 32;
585 pStr++;
586 nLen--;
590 /* ----------------------------------------------------------------------- */
592 void SAL_CALL IMPL_RTL_STRNAME( toAsciiUpperCase )( IMPL_RTL_STRCODE* pStr )
594 while ( *pStr )
596 /* Between a-z (97-122), than to uppercase (-32) */
597 if ( (*pStr >= 97) && (*pStr <= 122) )
598 *pStr -= 32;
600 pStr++;
604 /* ----------------------------------------------------------------------- */
606 void SAL_CALL IMPL_RTL_STRNAME( toAsciiUpperCase_WithLength )( IMPL_RTL_STRCODE* pStr,
607 sal_Int32 nLen )
609 while ( nLen > 0 )
611 /* Between a-z (97-122), than to uppercase (-32) */
612 if ( (*pStr >= 97) && (*pStr <= 122) )
613 *pStr -= 32;
615 pStr++;
616 nLen--;
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)) ) )
636 nPreSpaces++;
638 while ( (nIndex > nPreSpaces) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pStr+nIndex)) ) )
640 nPostSpaces++;
641 nIndex--;
644 if ( nPostSpaces )
646 nLen -= nPostSpaces;
647 *(pStr+nLen) = 0;
650 if ( nPreSpaces )
652 IMPL_RTL_STRCODE* pNewStr = pStr+nPreSpaces;
654 nLen -= nPreSpaces;
655 nIndex = nLen;
657 while ( nIndex )
659 *pStr = *pNewStr;
660 pStr++;
661 pNewStr++;
662 nIndex--;
664 *pStr = 0;
667 return nLen;
670 /* ----------------------------------------------------------------------- */
672 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfBoolean )( IMPL_RTL_STRCODE* pStr, sal_Bool b )
674 if ( b )
676 *pStr = 't';
677 pStr++;
678 *pStr = 'r';
679 pStr++;
680 *pStr = 'u';
681 pStr++;
682 *pStr = 'e';
683 pStr++;
684 *pStr = 0;
685 return 4;
687 else
689 *pStr = 'f';
690 pStr++;
691 *pStr = 'a';
692 pStr++;
693 *pStr = 'l';
694 pStr++;
695 *pStr = 's';
696 pStr++;
697 *pStr = 'e';
698 pStr++;
699 *pStr = 0;
700 return 5;
704 /* ----------------------------------------------------------------------- */
706 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfChar )( IMPL_RTL_STRCODE* pStr,
707 IMPL_RTL_STRCODE c )
709 *pStr++ = c;
710 *pStr = 0;
711 return 1;
714 /* ----------------------------------------------------------------------- */
716 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfInt32 )( IMPL_RTL_STRCODE* pStr,
717 sal_Int32 n,
718 sal_Int16 nRadix )
720 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
721 sal_Char* pBuf = aBuf;
722 sal_Int32 nLen = 0;
723 sal_uInt32 nValue;
725 /* Radix must be valid */
726 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
727 nRadix = 10;
729 /* is value negativ */
730 if ( n < 0 )
732 *pStr = '-';
733 pStr++;
734 nLen++;
735 nValue = -n; /* FIXME this code is not portable for n == -2147483648
736 (smallest negative value for sal_Int32) */
738 else
739 nValue = n;
741 /* create a recursive buffer with all values, except the last one */
744 sal_Char nDigit = (sal_Char)(nValue % nRadix);
745 nValue /= nRadix;
746 if ( nDigit > 9 )
747 *pBuf = (nDigit-10) + 'a';
748 else
749 *pBuf = (nDigit + '0' );
750 pBuf++;
752 while ( nValue > 0 );
754 /* copy the values in the right direction into the destination buffer */
757 pBuf--;
758 *pStr = *pBuf;
759 pStr++;
760 nLen++;
762 while ( pBuf != aBuf );
763 *pStr = 0;
765 return nLen;
768 /* ----------------------------------------------------------------------- */
770 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( valueOfInt64 )( IMPL_RTL_STRCODE* pStr,
771 sal_Int64 n,
772 sal_Int16 nRadix )
774 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
775 sal_Char* pBuf = aBuf;
776 sal_Int32 nLen = 0;
777 sal_uInt64 nValue;
779 /* Radix must be valid */
780 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
781 nRadix = 10;
783 /* is value negativ */
784 if ( n < 0 )
786 *pStr = '-';
787 pStr++;
788 nLen++;
789 nValue = -n; /* FIXME this code is not portable for
790 n == -9223372036854775808 (smallest negative value for
791 sal_Int64) */
793 else
794 nValue = n;
796 /* create a recursive buffer with all values, except the last one */
799 sal_Char nDigit = (sal_Char)(nValue % nRadix);
800 nValue /= nRadix;
801 if ( nDigit > 9 )
802 *pBuf = (nDigit-10) + 'a';
803 else
804 *pBuf = (nDigit + '0' );
805 pBuf++;
807 while ( nValue > 0 );
809 /* copy the values in the right direction into the destination buffer */
812 pBuf--;
813 *pStr = *pBuf;
814 pStr++;
815 nLen++;
817 while ( pBuf != aBuf );
818 *pStr = 0;
820 return nLen;
823 /* ----------------------------------------------------------------------- */
825 sal_Bool SAL_CALL IMPL_RTL_STRNAME( toBoolean )( const IMPL_RTL_STRCODE* pStr )
827 if ( *pStr == '1' )
828 return sal_True;
830 if ( (*pStr == 'T') || (*pStr == 't') )
832 pStr++;
833 if ( (*pStr == 'R') || (*pStr == 'r') )
835 pStr++;
836 if ( (*pStr == 'U') || (*pStr == 'u') )
838 pStr++;
839 if ( (*pStr == 'E') || (*pStr == 'e') )
840 return sal_True;
845 return sal_False;
848 /* ----------------------------------------------------------------------- */
850 sal_Int32 SAL_CALL IMPL_RTL_STRNAME( toInt32 )( const IMPL_RTL_STRCODE* pStr,
851 sal_Int16 nRadix )
853 sal_Bool bNeg;
854 sal_Int16 nDigit;
855 sal_Int32 n = 0;
857 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
858 nRadix = 10;
860 /* Skip whitespaces */
861 while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) )
862 pStr++;
864 if ( *pStr == '-' )
866 bNeg = sal_True;
867 pStr++;
869 else
871 if ( *pStr == '+' )
872 pStr++;
873 bNeg = sal_False;
876 while ( *pStr )
878 nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
879 if ( nDigit < 0 )
880 break;
882 n *= nRadix;
883 n += nDigit;
885 pStr++;
888 if ( bNeg )
889 return -n;
890 else
891 return n;
894 /* ----------------------------------------------------------------------- */
896 sal_Int64 SAL_CALL IMPL_RTL_STRNAME( toInt64 )( const IMPL_RTL_STRCODE* pStr,
897 sal_Int16 nRadix )
899 sal_Bool bNeg;
900 sal_Int16 nDigit;
901 sal_Int64 n = 0;
903 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
904 nRadix = 10;
906 /* Skip whitespaces */
907 while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) )
908 pStr++;
910 if ( *pStr == '-' )
912 bNeg = sal_True;
913 pStr++;
915 else
917 if ( *pStr == '+' )
918 pStr++;
919 bNeg = sal_False;
922 while ( *pStr )
924 nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
925 if ( nDigit < 0 )
926 break;
928 n *= nRadix;
929 n += nDigit;
931 pStr++;
934 if ( bNeg )
935 return -n;
936 else
937 return n;
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))
952 : NULL;
953 if (pData != NULL) {
954 pData->refCount = 1;
955 pData->length = nLen;
956 pData->buffer[nLen] = 0;
958 return pData;
961 /* ----------------------------------------------------------------------- */
963 static IMPL_RTL_STRCODE* IMPL_RTL_STRINGNAME( ImplNewCopy )( IMPL_RTL_STRINGDATA** ppThis,
964 IMPL_RTL_STRINGDATA* pStr,
965 sal_Int32 nCount )
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;
973 pSrc = pStr->buffer;
974 while ( nCount > 0 )
976 *pDest = *pSrc;
977 pDest++;
978 pSrc++;
979 nCount--;
982 *ppThis = pData;
983 return pDest;
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))
1008 return;
1010 /* OString doesn't have an 'intern' */
1011 #ifdef IMPL_RTL_INTERN
1012 if (SAL_STRING_IS_INTERN (pThis))
1014 internRelease (pThis);
1015 return;
1017 #endif
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 )
1030 if ( *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 )
1041 if ( nLen <= 0 )
1042 IMPL_RTL_STRINGNAME( new )( ppThis );
1043 else
1045 if ( *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;
1054 while ( nLen >= 0 )
1056 *pTempStr = 0;
1057 pTempStr++;
1058 nLen--;
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 );
1074 return;
1077 pOrg = *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 */
1083 if ( pOrg )
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;
1094 sal_Int32 nLen;
1096 if ( pCharStr )
1098 const IMPL_RTL_STRCODE* pTempStr = pCharStr;
1099 while( *pTempStr )
1100 pTempStr++;
1101 nLen = pTempStr-pCharStr;
1103 else
1104 nLen = 0;
1106 if ( !nLen )
1108 IMPL_RTL_STRINGNAME( new )( ppThis );
1109 return;
1112 pOrg = *ppThis;
1113 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
1114 OSL_ASSERT(*ppThis != NULL);
1115 pBuffer = (*ppThis)->buffer;
1118 *pBuffer = *pCharStr;
1119 pBuffer++;
1120 pCharStr++;
1122 while ( *pCharStr );
1124 /* must be done at least, if pCharStr == *ppThis */
1125 if ( pOrg )
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,
1133 sal_Int32 nLen )
1135 IMPL_RTL_STRINGDATA* pOrg;
1137 if ( !pCharStr || (nLen <= 0) )
1139 IMPL_RTL_STRINGNAME( new )( ppThis );
1140 return;
1143 pOrg = *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 */
1149 if ( pOrg )
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 );
1161 if ( *ppThis )
1162 IMPL_RTL_STRINGNAME( release )( *ppThis );
1164 *ppThis = pStr;
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 )
1192 *ppThis = pLeft;
1193 IMPL_RTL_AQUIRE( pLeft );
1195 else if ( !pLeft || !pLeft->length )
1197 *ppThis = pRight;
1198 IMPL_RTL_AQUIRE( pRight );
1200 else
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 );
1206 *ppThis = pTempStr;
1209 /* must be done at least, if left or right == *ppThis */
1210 if ( pOrg )
1211 IMPL_RTL_STRINGNAME( release )( pOrg );
1214 /* ----------------------------------------------------------------------- */
1216 void SAL_CALL IMPL_RTL_STRINGNAME( newReplaceStrAt )( IMPL_RTL_STRINGDATA** ppThis,
1217 IMPL_RTL_STRINGDATA* pStr,
1218 sal_Int32 nIndex,
1219 sal_Int32 nCount,
1220 IMPL_RTL_STRINGDATA* pNewSubStr )
1222 /* Append? */
1223 if ( nIndex >= pStr->length )
1225 /* newConcat test, if pNewSubStr is 0 */
1226 IMPL_RTL_STRINGNAME( newConcat )( ppThis, pStr, pNewSubStr );
1227 return;
1230 /* negativ index? */
1231 if ( nIndex < 0 )
1233 nCount -= nIndex;
1234 nIndex = 0;
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) )
1245 if ( !pNewSubStr )
1246 IMPL_RTL_STRINGNAME( new )( ppThis );
1247 else
1248 IMPL_RTL_STRINGNAME( assign )( ppThis, pNewSubStr );
1249 return;
1253 /* Assign of Str? */
1254 if ( !nCount && (!pNewSubStr || !pNewSubStr->length) )
1256 IMPL_RTL_STRINGNAME( assign )( ppThis, pStr );
1257 return;
1261 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
1262 IMPL_RTL_STRCODE* pBuffer;
1263 sal_Int32 nNewLen;
1265 /* Calculate length of the new string */
1266 nNewLen = pStr->length-nCount;
1267 if ( pNewSubStr )
1268 nNewLen += pNewSubStr->length;
1270 /* Alloc New Buffer */
1271 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nNewLen );
1272 OSL_ASSERT(*ppThis != NULL);
1273 pBuffer = (*ppThis)->buffer;
1274 if ( nIndex )
1276 rtl_str_ImplCopy( pBuffer, pStr->buffer, nIndex );
1277 pBuffer += 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 */
1287 if ( pOrg )
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;
1300 int bChanged = 0;
1301 sal_Int32 nLen = pStr->length;
1302 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1304 while ( nLen > 0 )
1306 if ( *pCharStr == cOld )
1308 /* Copy String */
1309 IMPL_RTL_STRCODE* pNewCharStr = IMPL_RTL_STRINGNAME( ImplNewCopy )( ppThis, pStr, pCharStr-pStr->buffer );
1311 /* replace/copy rest of the string */
1312 if ( pNewCharStr )
1314 *pNewCharStr = cNew;
1315 pNewCharStr++;
1316 pCharStr++;
1317 nLen--;
1319 while ( nLen > 0 )
1321 if ( *pCharStr == cOld )
1322 *pNewCharStr = cNew;
1323 else
1324 *pNewCharStr = *pCharStr;
1326 pNewCharStr++;
1327 pCharStr++;
1328 nLen--;
1332 bChanged = 1;
1333 break;
1336 pCharStr++;
1337 nLen--;
1340 if ( !bChanged )
1342 *ppThis = pStr;
1343 IMPL_RTL_AQUIRE( pStr );
1346 /* must be done at least, if pStr == *ppThis */
1347 if ( pOrg )
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;
1357 int bChanged = 0;
1358 sal_Int32 nLen = pStr->length;
1359 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1361 while ( nLen > 0 )
1363 /* Between A-Z (65-90), than to lowercase (+32) */
1364 if ( (*pCharStr >= 65) && (*pCharStr <= 90) )
1366 /* Copy String */
1367 IMPL_RTL_STRCODE* pNewCharStr = IMPL_RTL_STRINGNAME( ImplNewCopy )( ppThis, pStr, pCharStr-pStr->buffer );
1369 /* replace/copy rest of the string */
1370 if ( pNewCharStr )
1372 /* to lowercase (+32) */
1373 *pNewCharStr = *pCharStr+32;
1374 pNewCharStr++;
1375 pCharStr++;
1376 nLen--;
1378 while ( nLen > 0 )
1380 /* Between A-Z (65-90), than to lowercase (+32) */
1381 if ( (*pCharStr >= 65) && (*pCharStr <= 90) )
1382 *pNewCharStr = *pCharStr+32;
1383 else
1384 *pNewCharStr = *pCharStr;
1386 pNewCharStr++;
1387 pCharStr++;
1388 nLen--;
1392 bChanged = 1;
1393 break;
1396 pCharStr++;
1397 nLen--;
1400 if ( !bChanged )
1402 *ppThis = pStr;
1403 IMPL_RTL_AQUIRE( pStr );
1406 /* must be done at least, if pStr == *ppThis */
1407 if ( pOrg )
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;
1417 int bChanged = 0;
1418 sal_Int32 nLen = pStr->length;
1419 const IMPL_RTL_STRCODE* pCharStr = pStr->buffer;
1421 while ( nLen > 0 )
1423 /* Between a-z (97-122), than to uppercase (-32) */
1424 if ( (*pCharStr >= 97) && (*pCharStr <= 122) )
1426 /* Copy String */
1427 IMPL_RTL_STRCODE* pNewCharStr = IMPL_RTL_STRINGNAME( ImplNewCopy )( ppThis, pStr, pCharStr-pStr->buffer );
1429 /* replace/copy rest of the string */
1430 if ( pNewCharStr )
1432 /* to uppercase (-32) */
1433 *pNewCharStr = *pCharStr-32;
1434 pNewCharStr++;
1435 pCharStr++;
1436 nLen--;
1438 while ( nLen > 0 )
1440 /* Between a-z (97-122), than to uppercase (-32) */
1441 if ( (*pCharStr >= 97) && (*pCharStr <= 122) )
1442 *pNewCharStr = *pCharStr-32;
1443 else
1444 *pNewCharStr = *pCharStr;
1446 pNewCharStr++;
1447 pCharStr++;
1448 nLen--;
1452 bChanged = 1;
1453 break;
1456 pCharStr++;
1457 nLen--;
1460 if ( !bChanged )
1462 *ppThis = pStr;
1463 IMPL_RTL_AQUIRE( pStr );
1466 /* must be done at least, if pStr == *ppThis */
1467 if ( pOrg )
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)) ) )
1484 nPreSpaces++;
1486 while ( (nIndex > nPreSpaces) && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE(*(pCharStr+nIndex)) ) )
1488 nPostSpaces++;
1489 nIndex--;
1492 if ( !nPreSpaces && !nPostSpaces )
1494 *ppThis = pStr;
1495 IMPL_RTL_AQUIRE( pStr );
1497 else
1499 nLen -= nPostSpaces+nPreSpaces;
1500 *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
1501 OSL_ASSERT(*ppThis != NULL);
1502 if ( *ppThis )
1503 rtl_str_ImplCopy( (*ppThis)->buffer, pStr->buffer+nPreSpaces, nLen );
1506 /* must be done at least, if pStr == *ppThis */
1507 if ( pOrg )
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,
1515 sal_Int32 nToken,
1516 IMPL_RTL_STRCODE cTok,
1517 sal_Int32 nIndex )
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
1526 // negative:
1527 if (nIndex < 0) {
1528 nToken = -1;
1531 pCharStr += nIndex;
1532 pOrgCharStr = pCharStr;
1533 pCharStrStart = pCharStr;
1534 while ( nLen > 0 )
1536 if ( *pCharStr == cTok )
1538 nTokCount++;
1540 if ( nTokCount == nToken )
1541 pCharStrStart = pCharStr+1;
1542 else
1544 if ( nTokCount > nToken )
1545 break;
1549 pCharStr++;
1550 nLen--;
1553 if ( (nToken < 0) || (nTokCount < nToken) || (pCharStr == pCharStrStart) )
1555 IMPL_RTL_STRINGNAME( new )( ppThis );
1556 if( (nToken < 0) || (nTokCount < nToken ) )
1557 return -1;
1558 else if( nLen > 0 )
1559 return nIndex+(pCharStr-pOrgCharStr)+1;
1560 else return -1;
1562 else
1564 IMPL_RTL_STRINGNAME( newFromStr_WithLength )( ppThis, pCharStrStart, pCharStr-pCharStrStart );
1565 if ( nLen )
1566 return nIndex+(pCharStr-pOrgCharStr)+1;
1567 else
1568 return -1;