Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #ifdef _MSC_VER
23 : #pragma warning(disable:4738) // storing 32-bit float result in memory, possible loss of performance
24 : #endif
25 :
26 : #include <cassert>
27 : #include <cstdlib>
28 :
29 : #include <osl/diagnose.h>
30 : #include <osl/interlck.h>
31 : #include <rtl/alloc.h>
32 : #include <osl/mutex.h>
33 : #include <osl/doublecheckedlocking.h>
34 : #include <rtl/tencinfo.h>
35 :
36 : #include <string.h>
37 : #include <sal/alloca.h>
38 : #include <sal/log.hxx>
39 :
40 : #include "hash.hxx"
41 : #include "strimp.hxx"
42 : #include "surrogates.hxx"
43 : #include <rtl/ustring.h>
44 :
45 : #include "rtl/math.h"
46 :
47 : /* ======================================================================= */
48 :
49 : /* static data to be referenced by all empty strings
50 : * the refCount is predefined to 1 and must never become 0 !
51 : */
52 : static rtl_uString const aImplEmpty_rtl_uString =
53 : {
54 : (sal_Int32) (SAL_STRING_INTERN_FLAG|SAL_STRING_STATIC_FLAG|1), /*sal_Int32 refCount; */
55 : 0, /*sal_Int32 length; */
56 : { 0 } /*sal_Unicode buffer[1];*/
57 : };
58 :
59 : /* ======================================================================= */
60 : /* These macros are for the "poor-man templates" included from
61 : * the strtmpl.cxx just below, used to share code between here and
62 : * string.cxx
63 : */
64 :
65 : #define IMPL_RTL_STRCODE sal_Unicode
66 : #define IMPL_RTL_USTRCODE( c ) (c)
67 : #define IMPL_RTL_STRNAME( n ) rtl_ustr_ ## n
68 :
69 : #define IMPL_RTL_STRINGNAME( n ) rtl_uString_ ## n
70 : #define IMPL_RTL_STRINGDATA rtl_uString
71 : #define IMPL_RTL_EMPTYSTRING aImplEmpty_rtl_uString
72 : #define IMPL_RTL_INTERN
73 : static void internRelease (rtl_uString *pThis);
74 :
75 : #if USE_SDT_PROBES
76 : #define RTL_LOG_STRING_BITS 16
77 : #endif
78 :
79 : /* ======================================================================= */
80 :
81 : /* Include String/UString template code */
82 :
83 : #include "strtmpl.cxx"
84 :
85 4750298 : sal_Int32 rtl_ustr_indexOfAscii_WithLength(
86 : sal_Unicode const * str, sal_Int32 len,
87 : char const * subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
88 : {
89 4750298 : if (subLen > 0 && subLen <= len) {
90 : sal_Int32 i;
91 35856144 : for (i = 0; i <= len - subLen; ++i) {
92 33156695 : if (rtl_ustr_asciil_reverseEquals_WithLength(
93 33156696 : str + i, subStr, subLen))
94 : {
95 148199 : return i;
96 : }
97 : }
98 : }
99 4602098 : return -1;
100 : }
101 :
102 2112 : sal_Int32 rtl_ustr_lastIndexOfAscii_WithLength(
103 : sal_Unicode const * str, sal_Int32 len,
104 : char const * subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
105 : {
106 2112 : if (subLen > 0 && subLen <= len) {
107 : sal_Int32 i;
108 2276 : for (i = len - subLen; i >= 0; --i) {
109 2275 : if (rtl_ustr_asciil_reverseEquals_WithLength(
110 2275 : str + i, subStr, subLen))
111 : {
112 2110 : return i;
113 : }
114 : }
115 : }
116 2 : return -1;
117 : }
118 :
119 61 : sal_Int32 SAL_CALL rtl_ustr_valueOfFloat(sal_Unicode * pStr, float f)
120 : SAL_THROW_EXTERN_C()
121 : {
122 61 : rtl_uString * pResult = NULL;
123 : sal_Int32 nLen;
124 : rtl_math_doubleToUString(
125 : &pResult, 0, 0, f, rtl_math_StringFormat_G,
126 : RTL_USTR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0,
127 61 : 0, sal_True);
128 61 : nLen = pResult->length;
129 : OSL_ASSERT(nLen < RTL_USTR_MAX_VALUEOFFLOAT);
130 61 : memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Unicode));
131 61 : rtl_uString_release(pResult);
132 61 : return nLen;
133 : }
134 :
135 69666 : sal_Int32 SAL_CALL rtl_ustr_valueOfDouble(sal_Unicode * pStr, double d)
136 : SAL_THROW_EXTERN_C()
137 : {
138 69666 : rtl_uString * pResult = NULL;
139 : sal_Int32 nLen;
140 : rtl_math_doubleToUString(
141 : &pResult, 0, 0, d, rtl_math_StringFormat_G,
142 : RTL_USTR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0,
143 69666 : 0, sal_True);
144 69666 : nLen = pResult->length;
145 : OSL_ASSERT(nLen < RTL_USTR_MAX_VALUEOFDOUBLE);
146 69666 : memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Unicode));
147 69666 : rtl_uString_release(pResult);
148 69666 : return nLen;
149 : }
150 :
151 74 : float SAL_CALL rtl_ustr_toFloat(sal_Unicode const * pStr) SAL_THROW_EXTERN_C()
152 : {
153 : return (float) rtl_math_uStringToDouble(pStr,
154 74 : pStr + rtl_ustr_getLength(pStr),
155 74 : '.', 0, 0, 0);
156 : }
157 :
158 13234 : double SAL_CALL rtl_ustr_toDouble(sal_Unicode const * pStr) SAL_THROW_EXTERN_C()
159 : {
160 13234 : return rtl_math_uStringToDouble(pStr, pStr + rtl_ustr_getLength(pStr), '.',
161 13234 : 0, 0, 0);
162 : }
163 :
164 : /* ======================================================================= */
165 :
166 45951 : sal_Int32 SAL_CALL rtl_ustr_ascii_compare( const sal_Unicode* pStr1,
167 : const sal_Char* pStr2 )
168 : SAL_THROW_EXTERN_C()
169 : {
170 : sal_Int32 nRet;
171 496927 : while ( ((nRet = ((sal_Int32)(*pStr1))-
172 225488 : ((sal_Int32)((unsigned char)(*pStr2)))) == 0) &&
173 : *pStr2 )
174 : {
175 : /* Check ASCII range */
176 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
177 : "rtl_ustr_ascii_compare - Found char > 127" );
178 179537 : pStr1++;
179 179537 : pStr2++;
180 : }
181 :
182 45951 : return nRet;
183 : }
184 :
185 : /* ----------------------------------------------------------------------- */
186 :
187 418375312 : sal_Int32 SAL_CALL rtl_ustr_ascii_compare_WithLength( const sal_Unicode* pStr1,
188 : sal_Int32 nStr1Len,
189 : const sal_Char* pStr2 )
190 : SAL_THROW_EXTERN_C()
191 : {
192 418375312 : sal_Int32 nRet = 0;
193 2071029872 : while( ((nRet = (nStr1Len ? (sal_Int32)(*pStr1) : 0)-
194 414109954 : ((sal_Int32)((unsigned char)(*pStr2)))) == 0) &&
195 1234279248 : nStr1Len && *pStr2 )
196 : {
197 : /* Check ASCII range */
198 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
199 : "rtl_ustr_ascii_compare_WithLength - Found char > 127" );
200 407951968 : pStr1++;
201 407951968 : pStr2++;
202 407951968 : nStr1Len--;
203 : }
204 :
205 418375312 : return nRet;
206 : }
207 :
208 : /* ----------------------------------------------------------------------- */
209 :
210 3635194 : sal_Int32 SAL_CALL rtl_ustr_ascii_shortenedCompare_WithLength( const sal_Unicode* pStr1,
211 : sal_Int32 nStr1Len,
212 : const sal_Char* pStr2,
213 : sal_Int32 nShortenedLength )
214 : SAL_THROW_EXTERN_C()
215 : {
216 3635194 : const sal_Unicode* pStr1End = pStr1 + nStr1Len;
217 : sal_Int32 nRet;
218 15757512 : while ( (nShortenedLength > 0) &&
219 10605315 : (pStr1 < pStr1End) && *pStr2 )
220 : {
221 : /* Check ASCII range */
222 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
223 : "rtl_ustr_ascii_shortenedCompare_WithLength - Found char > 127" );
224 :
225 10605315 : nRet = ((sal_Int32)*pStr1)-
226 10605315 : ((sal_Int32)(unsigned char)*pStr2);
227 10605315 : if ( nRet != 0 )
228 2118191 : return nRet;
229 :
230 8487124 : nShortenedLength--;
231 8487124 : pStr1++;
232 8487124 : pStr2++;
233 : }
234 :
235 1517003 : if ( nShortenedLength <= 0 )
236 1510051 : return 0;
237 :
238 6952 : if ( *pStr2 )
239 : {
240 : OSL_ENSURE( pStr1 == pStr1End, "pStr1 == pStr1End failed" );
241 : // first is a substring of the second string => less (negative value)
242 6952 : nRet = -1;
243 : }
244 : else
245 : {
246 : // greater or equal
247 0 : nRet = pStr1End - pStr1;
248 : }
249 :
250 6952 : return nRet;
251 : }
252 :
253 : /* ----------------------------------------------------------------------- */
254 :
255 2575 : sal_Int32 SAL_CALL rtl_ustr_asciil_reverseCompare_WithLength( const sal_Unicode* pStr1,
256 : sal_Int32 nStr1Len,
257 : const sal_Char* pStr2,
258 : sal_Int32 nStr2Len )
259 : SAL_THROW_EXTERN_C()
260 : {
261 2575 : const sal_Unicode* pStr1Run = pStr1+nStr1Len;
262 2575 : const sal_Char* pStr2Run = pStr2+nStr2Len;
263 : sal_Int32 nRet;
264 5550 : while ( (pStr1 < pStr1Run) && (pStr2 < pStr2Run) )
265 : {
266 : /* Check ASCII range */
267 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
268 : "rtl_ustr_asciil_reverseCompare_WithLength - Found char > 127" );
269 2955 : pStr1Run--;
270 2955 : pStr2Run--;
271 2955 : nRet = ((sal_Int32)*pStr1Run)-((sal_Int32)*pStr2Run);
272 2955 : if ( nRet )
273 2555 : return nRet;
274 : }
275 :
276 20 : return nStr1Len - nStr2Len;
277 : }
278 :
279 : /* ----------------------------------------------------------------------- */
280 :
281 59371444 : sal_Bool SAL_CALL rtl_ustr_asciil_reverseEquals_WithLength( const sal_Unicode* pStr1,
282 : const sal_Char* pStr2,
283 : sal_Int32 nStrLen )
284 : SAL_THROW_EXTERN_C()
285 : {
286 59371444 : const sal_Unicode* pStr1Run = pStr1+nStrLen;
287 59371444 : const sal_Char* pStr2Run = pStr2+nStrLen;
288 289835813 : while ( pStr1 < pStr1Run )
289 : {
290 : /* Check ASCII range */
291 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
292 : "rtl_ustr_asciil_reverseEquals_WithLength - Found char > 127" );
293 214844644 : pStr1Run--;
294 214844644 : pStr2Run--;
295 214844644 : if( *pStr1Run != (sal_Unicode)*pStr2Run )
296 43751719 : return sal_False;
297 : }
298 :
299 15619725 : return sal_True;
300 : }
301 :
302 : /* ----------------------------------------------------------------------- */
303 :
304 0 : sal_Int32 SAL_CALL rtl_ustr_ascii_compareIgnoreAsciiCase( const sal_Unicode* pStr1,
305 : const sal_Char* pStr2 )
306 : SAL_THROW_EXTERN_C()
307 : {
308 : sal_Int32 nRet;
309 : sal_Int32 c1;
310 : sal_Int32 c2;
311 0 : do
312 : {
313 : /* Check ASCII range */
314 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
315 : "rtl_ustr_ascii_compareIgnoreAsciiCase - Found char > 127" );
316 : /* If character between 'A' and 'Z', than convert it to lowercase */
317 0 : c1 = (sal_Int32)*pStr1;
318 0 : c2 = (sal_Int32)((unsigned char)*pStr2);
319 0 : if ( (c1 >= 65) && (c1 <= 90) )
320 0 : c1 += 32;
321 0 : if ( (c2 >= 65) && (c2 <= 90) )
322 0 : c2 += 32;
323 0 : nRet = c1-c2;
324 0 : if ( nRet != 0 )
325 0 : return nRet;
326 :
327 0 : pStr1++;
328 0 : pStr2++;
329 : }
330 : while ( c2 );
331 :
332 0 : return 0;
333 : }
334 :
335 : /* ----------------------------------------------------------------------- */
336 :
337 205739197 : sal_Int32 SAL_CALL rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( const sal_Unicode* pStr1,
338 : sal_Int32 nStr1Len,
339 : const sal_Char* pStr2 )
340 : SAL_THROW_EXTERN_C()
341 : {
342 : sal_Int32 nRet;
343 : sal_Int32 c1;
344 : sal_Int32 c2;
345 27490391 : do
346 : {
347 : /* Check ASCII range */
348 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
349 : "rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength - Found char > 127" );
350 205739197 : if ( !nStr1Len )
351 903403 : return *pStr2 == '\0' ? 0 : -1;
352 :
353 : /* If character between 'A' and 'Z', than convert it to lowercase */
354 204835794 : c1 = (sal_Int32)*pStr1;
355 204835794 : c2 = (sal_Int32)((unsigned char)*pStr2);
356 204835794 : if ( (c1 >= 65) && (c1 <= 90) )
357 179395654 : c1 += 32;
358 204835794 : if ( (c2 >= 65) && (c2 <= 90) )
359 177464065 : c2 += 32;
360 204835794 : nRet = c1-c2;
361 204835794 : if ( nRet != 0 )
362 177345403 : return nRet;
363 :
364 27490391 : pStr1++;
365 27490391 : pStr2++;
366 27490391 : nStr1Len--;
367 : }
368 : while( c2 );
369 :
370 0 : return 0;
371 : }
372 :
373 1803158 : sal_Int32 rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
374 : sal_Unicode const * first, sal_Int32 firstLen,
375 : char const * second, sal_Int32 secondLen) SAL_THROW_EXTERN_C()
376 : {
377 : sal_Int32 i;
378 1803158 : sal_Int32 len = firstLen < secondLen ? firstLen : secondLen;
379 2093149 : for (i = 0; i < len; ++i) {
380 : /* Check ASCII range */
381 : SAL_WARN_IF( ((unsigned char)*second) > 127, "rtl.string",
382 : "rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths - Found char > 127" );
383 2071714 : sal_Int32 c1 = *first++;
384 2071714 : sal_Int32 c2 = (unsigned char) *second++;
385 : sal_Int32 d;
386 2071714 : if (c1 >= 65 && c1 <= 90) {
387 426812 : c1 += 32;
388 : }
389 2071714 : if (c2 >= 65 && c2 <= 90) {
390 71740 : c2 += 32;
391 : }
392 2071714 : d = c1 - c2;
393 2071714 : if (d != 0) {
394 1781723 : return d;
395 : }
396 : }
397 21435 : return firstLen - secondLen;
398 : }
399 :
400 : /* ----------------------------------------------------------------------- */
401 :
402 3258638 : sal_Int32 SAL_CALL rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( const sal_Unicode* pStr1,
403 : sal_Int32 nStr1Len,
404 : const sal_Char* pStr2,
405 : sal_Int32 nShortenedLength )
406 : SAL_THROW_EXTERN_C()
407 : {
408 3258638 : const sal_Unicode* pStr1End = pStr1 + nStr1Len;
409 : sal_Int32 nRet;
410 : sal_Int32 c1;
411 : sal_Int32 c2;
412 8036398 : while ( (nShortenedLength > 0) &&
413 4613632 : (pStr1 < pStr1End) && *pStr2 )
414 : {
415 : /* Check ASCII range */
416 : SAL_WARN_IF( ((unsigned char)*pStr2) > 127, "rtl.string",
417 : "rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength - Found char > 127" );
418 :
419 : /* If character between 'A' and 'Z', than convert it to lowercase */
420 4613632 : c1 = (sal_Int32)*pStr1;
421 4613632 : c2 = (sal_Int32)((unsigned char)*pStr2);
422 4613632 : if ( (c1 >= 65) && (c1 <= 90) )
423 240459 : c1 += 32;
424 4613632 : if ( (c2 >= 65) && (c2 <= 90) )
425 240630 : c2 += 32;
426 4613632 : nRet = c1-c2;
427 4613632 : if ( nRet != 0 )
428 3094510 : return nRet;
429 :
430 1519122 : nShortenedLength--;
431 1519122 : pStr1++;
432 1519122 : pStr2++;
433 : }
434 :
435 164128 : if ( nShortenedLength <= 0 )
436 164078 : return 0;
437 :
438 50 : if ( *pStr2 )
439 : {
440 : OSL_ENSURE( pStr1 == pStr1End, "pStr1 == pStr1End failed" );
441 : // first is a substring of the second string => less (negative value)
442 50 : nRet = -1;
443 : }
444 : else
445 : {
446 : // greater or equal
447 0 : nRet = pStr1End - pStr1;
448 : }
449 :
450 50 : return nRet;
451 : }
452 :
453 : /* ----------------------------------------------------------------------- */
454 :
455 10297034 : void SAL_CALL rtl_uString_newFromAscii( rtl_uString** ppThis,
456 : const sal_Char* pCharStr )
457 : SAL_THROW_EXTERN_C()
458 : {
459 : sal_Int32 nLen;
460 :
461 10297034 : if ( pCharStr )
462 : {
463 10296977 : const sal_Char* pTempStr = pCharStr;
464 122395887 : while( *pTempStr )
465 101801933 : pTempStr++;
466 10296977 : nLen = pTempStr-pCharStr;
467 : }
468 : else
469 57 : nLen = 0;
470 :
471 10297034 : if ( !nLen )
472 : {
473 97274 : rtl_uString_new( ppThis );
474 10394308 : return;
475 : }
476 :
477 10199760 : if ( *ppThis )
478 6115 : rtl_uString_release( *ppThis );
479 :
480 10199760 : *ppThis = rtl_uString_ImplAlloc( nLen );
481 : OSL_ASSERT(*ppThis != NULL);
482 10199760 : if ( (*ppThis) )
483 : {
484 10199760 : sal_Unicode* pBuffer = (*ppThis)->buffer;
485 101801933 : do
486 : {
487 : /* Check ASCII range */
488 : SAL_WARN_IF( ((unsigned char)*pCharStr) > 127, "rtl.string",
489 : "rtl_uString_newFromAscii - Found char > 127" );
490 :
491 101801933 : *pBuffer = *pCharStr;
492 101801933 : pBuffer++;
493 101801933 : pCharStr++;
494 : }
495 : while ( *pCharStr );
496 :
497 : RTL_LOG_STRING_NEW( *ppThis );
498 : }
499 : }
500 :
501 17484 : void SAL_CALL rtl_uString_newFromCodePoints(
502 : rtl_uString ** newString, sal_uInt32 const * codePoints,
503 : sal_Int32 codePointCount) SAL_THROW_EXTERN_C()
504 : {
505 : sal_Int32 n;
506 : sal_Int32 i;
507 : sal_Unicode * p;
508 : OSL_ASSERT(
509 : newString != NULL &&
510 : (codePoints != NULL || codePointCount == 0) &&
511 : codePointCount >= 0);
512 17484 : if (codePointCount == 0) {
513 8589 : rtl_uString_new(newString);
514 8589 : return;
515 : }
516 8895 : if (*newString != NULL) {
517 0 : rtl_uString_release(*newString);
518 : }
519 8895 : n = codePointCount;
520 17795 : for (i = 0; i < codePointCount; ++i) {
521 : OSL_ASSERT(codePoints[i] <= 0x10FFFF);
522 8900 : if (codePoints[i] >= 0x10000) {
523 4 : ++n;
524 : }
525 : }
526 : /* Builds on the assumption that sal_Int32 uses 32 bit two's complement
527 : representation with wrap around (the necessary number of UTF-16 code
528 : units will be no larger than 2 * SAL_MAX_INT32, represented as
529 : sal_Int32 -2): */
530 8895 : if (n < 0) {
531 0 : *newString = NULL;
532 0 : return;
533 : }
534 8895 : *newString = rtl_uString_ImplAlloc(n);
535 8895 : if (*newString == NULL) {
536 0 : return;
537 : }
538 8895 : p = (*newString)->buffer;
539 17795 : for (i = 0; i < codePointCount; ++i) {
540 8900 : sal_uInt32 c = codePoints[i];
541 8900 : if (c < 0x10000) {
542 8896 : *p++ = (sal_Unicode) c;
543 : } else {
544 4 : c -= 0x10000;
545 4 : *p++ = (sal_Unicode) ((c >> 10) | SAL_RTL_FIRST_HIGH_SURROGATE);
546 4 : *p++ = (sal_Unicode) ((c & 0x3FF) | SAL_RTL_FIRST_LOW_SURROGATE);
547 : }
548 : }
549 : RTL_LOG_STRING_NEW( *newString );
550 : }
551 :
552 : /* ======================================================================= */
553 :
554 18975339 : static int rtl_ImplGetFastUTF8UnicodeLen( const sal_Char* pStr, sal_Int32 nLen, bool * ascii )
555 : {
556 : int n;
557 : unsigned char c;
558 : const sal_Char* pEndStr;
559 :
560 18975339 : *ascii = true;
561 18975339 : n = 0;
562 18975339 : pEndStr = pStr+nLen;
563 293928951 : while ( pStr < pEndStr )
564 : {
565 255978273 : c = (unsigned char)*pStr;
566 :
567 255978273 : if ( !(c & 0x80) )
568 253574258 : pStr++;
569 : else
570 : {
571 2404015 : if ( (c & 0xE0) == 0xC0 )
572 28688 : pStr += 2;
573 2375327 : else if ( (c & 0xF0) == 0xE0 )
574 2375325 : pStr += 3;
575 2 : else if ( (c & 0xF8) == 0xF0 )
576 0 : pStr += 4;
577 2 : else if ( (c & 0xFC) == 0xF8 )
578 0 : pStr += 5;
579 2 : else if ( (c & 0xFE) == 0xFC )
580 0 : pStr += 6;
581 : else
582 2 : pStr++;
583 2404015 : *ascii = false;
584 : }
585 :
586 255978273 : n++;
587 : }
588 :
589 18975339 : return n;
590 : }
591 :
592 : /* ----------------------------------------------------------------------- */
593 :
594 29250295 : static void rtl_string2UString_status( rtl_uString** ppThis,
595 : const sal_Char* pStr,
596 : sal_Int32 nLen,
597 : rtl_TextEncoding eTextEncoding,
598 : sal_uInt32 nCvtFlags,
599 : sal_uInt32 *pInfo )
600 : {
601 : OSL_ENSURE(nLen == 0 || rtl_isOctetTextEncoding(eTextEncoding),
602 : "rtl_string2UString_status() - Wrong TextEncoding" );
603 :
604 29250295 : if ( !nLen )
605 : {
606 577158 : rtl_uString_new( ppThis );
607 577158 : if (pInfo != NULL) {
608 447525 : *pInfo = 0;
609 : }
610 : }
611 : else
612 : {
613 28673137 : if ( *ppThis )
614 7927213 : rtl_uString_release( *ppThis );
615 :
616 : /* Optimization for US-ASCII */
617 28673139 : if ( eTextEncoding == RTL_TEXTENCODING_ASCII_US )
618 : {
619 : sal_Unicode* pBuffer;
620 9602410 : *ppThis = rtl_uString_ImplAlloc( nLen );
621 9602410 : if (*ppThis == NULL) {
622 0 : if (pInfo != NULL) {
623 : *pInfo = RTL_TEXTTOUNICODE_INFO_ERROR |
624 0 : RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
625 : }
626 0 : return;
627 : }
628 9602410 : pBuffer = (*ppThis)->buffer;
629 135554783 : do
630 : {
631 : /* Check ASCII range */
632 : SAL_WARN_IF( ((unsigned char)*pStr) > 127, "rtl.string",
633 : "rtl_string2UString_status() - Found char > 127 and RTL_TEXTENCODING_ASCII_US is specified" );
634 :
635 135554783 : *pBuffer = *pStr;
636 135554783 : pBuffer++;
637 135554783 : pStr++;
638 135554783 : nLen--;
639 : }
640 : while ( nLen );
641 9602410 : if (pInfo != NULL) {
642 7629885 : *pInfo = 0;
643 : }
644 : }
645 : else
646 : {
647 : rtl_uString* pTemp;
648 19070729 : rtl_uString* pTemp2 = NULL;
649 : rtl_TextToUnicodeConverter hConverter;
650 : sal_uInt32 nInfo;
651 : sal_Size nSrcBytes;
652 : sal_Size nDestChars;
653 : sal_Size nNewLen;
654 :
655 : /* Optimization for UTF-8 - we try to calculate the exact length */
656 : /* For all other encoding we try the maximum - and reallocate
657 : the buffer if needed */
658 19070729 : if ( eTextEncoding == RTL_TEXTENCODING_UTF8 )
659 : {
660 : bool ascii;
661 18975339 : nNewLen = rtl_ImplGetFastUTF8UnicodeLen( pStr, nLen, &ascii );
662 : /* Includes the string only ASCII, then we could copy
663 : the buffer faster */
664 18975339 : if ( ascii )
665 : {
666 : sal_Unicode* pBuffer;
667 18299784 : *ppThis = rtl_uString_ImplAlloc( nLen );
668 18299784 : if (*ppThis == NULL)
669 : {
670 0 : if (pInfo != NULL) {
671 : *pInfo = RTL_TEXTTOUNICODE_INFO_ERROR |
672 0 : RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
673 : }
674 18299784 : return;
675 : }
676 18299784 : pBuffer = (*ppThis)->buffer;
677 249134762 : do
678 : {
679 : /* Check ASCII range */
680 : SAL_WARN_IF( ((unsigned char)*pStr) > 127, "rtl.string",
681 : "rtl_string2UString_status() - UTF8 test encoding is wrong" );
682 :
683 249134762 : *pBuffer = *pStr;
684 249134762 : pBuffer++;
685 249134762 : pStr++;
686 249134762 : nLen--;
687 : }
688 : while ( nLen );
689 18299784 : if (pInfo != NULL) {
690 8973727 : *pInfo = 0;
691 : }
692 : RTL_LOG_STRING_NEW( *ppThis );
693 18299784 : return;
694 : }
695 : }
696 : else
697 95390 : nNewLen = nLen;
698 :
699 770945 : nCvtFlags |= RTL_TEXTTOUNICODE_FLAGS_FLUSH;
700 770945 : hConverter = rtl_createTextToUnicodeConverter( eTextEncoding );
701 :
702 770945 : pTemp = rtl_uString_ImplAlloc( nNewLen );
703 770945 : if (pTemp == NULL) {
704 0 : if (pInfo != NULL) {
705 : *pInfo = RTL_TEXTTOUNICODE_INFO_ERROR |
706 0 : RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
707 : }
708 0 : return;
709 : }
710 : nDestChars = rtl_convertTextToUnicode( hConverter, 0,
711 : pStr, nLen,
712 : pTemp->buffer, nNewLen,
713 : nCvtFlags,
714 770945 : &nInfo, &nSrcBytes );
715 :
716 : /* Buffer not big enough, try again with enough space */
717 : /* Shouldn't be the case, but if we get textencoding which
718 : could results in more unicode characters we have this
719 : code here. Could be the case for apple encodings */
720 1541946 : while ( nInfo & RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL )
721 : {
722 56 : rtl_freeMemory( pTemp );
723 56 : nNewLen += 8;
724 56 : pTemp = rtl_uString_ImplAlloc( nNewLen );
725 56 : if (pTemp == NULL) {
726 0 : if (pInfo != NULL) {
727 : *pInfo = RTL_TEXTTOUNICODE_INFO_ERROR |
728 0 : RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
729 : }
730 0 : return;
731 : }
732 : nDestChars = rtl_convertTextToUnicode( hConverter, 0,
733 : pStr, nLen,
734 : pTemp->buffer, nNewLen,
735 : nCvtFlags,
736 56 : &nInfo, &nSrcBytes );
737 : }
738 :
739 770945 : if (pInfo)
740 24203 : *pInfo = nInfo;
741 :
742 : /* Set the buffer to the correct size or if there is too
743 : much overhead, reallocate to the correct size */
744 770945 : if ( nNewLen > nDestChars+8 )
745 : {
746 0 : pTemp2 = rtl_uString_ImplAlloc( nDestChars );
747 : }
748 770945 : if (pTemp2 != NULL)
749 : {
750 0 : rtl_str_ImplCopy(pTemp2->buffer, pTemp->buffer, nDestChars);
751 0 : rtl_freeMemory(pTemp);
752 0 : pTemp = pTemp2;
753 : }
754 : else
755 : {
756 770945 : pTemp->length = nDestChars;
757 770945 : pTemp->buffer[nDestChars] = 0;
758 : }
759 :
760 770945 : rtl_destroyTextToUnicodeConverter( hConverter );
761 770945 : *ppThis = pTemp;
762 :
763 : /* Results the conversion in an empty buffer -
764 : create an empty string */
765 770945 : if ( pTemp && !nDestChars )
766 4 : rtl_uString_new( ppThis );
767 : }
768 : }
769 : RTL_LOG_STRING_NEW( *ppThis );
770 : }
771 :
772 11757670 : void SAL_CALL rtl_string2UString( rtl_uString** ppThis,
773 : const sal_Char* pStr,
774 : sal_Int32 nLen,
775 : rtl_TextEncoding eTextEncoding,
776 : sal_uInt32 nCvtFlags ) SAL_THROW_EXTERN_C()
777 : {
778 : rtl_string2UString_status( ppThis, pStr, nLen, eTextEncoding,
779 11757670 : nCvtFlags, NULL );
780 11757671 : }
781 :
782 : /* ----------------------------------------------------------------------- */
783 :
784 : enum StrLifecycle {
785 : CANNOT_RETURN,
786 : CAN_RETURN = 1
787 : };
788 :
789 : static oslMutex
790 817735 : getInternMutex()
791 : {
792 : static oslMutex pPoolGuard = NULL;
793 817735 : if( !pPoolGuard )
794 : {
795 : oslMutex pGlobalGuard;
796 105 : pGlobalGuard = *osl_getGlobalMutex();
797 105 : osl_acquireMutex( pGlobalGuard );
798 105 : if( !pPoolGuard )
799 : {
800 105 : oslMutex p = osl_createMutex();
801 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
802 105 : pPoolGuard = p;
803 : }
804 105 : osl_releaseMutex( pGlobalGuard );
805 : }
806 : else
807 : {
808 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
809 : }
810 :
811 817735 : return pPoolGuard;
812 : }
813 :
814 : /* returns true if we found a dup in the pool */
815 462794 : static void rtl_ustring_intern_internal( rtl_uString ** newStr,
816 : rtl_uString * str,
817 : StrLifecycle can_return )
818 : {
819 : oslMutex pPoolMutex;
820 :
821 462794 : pPoolMutex = getInternMutex();
822 :
823 462794 : osl_acquireMutex( pPoolMutex );
824 :
825 462794 : *newStr = rtl_str_hash_intern (str, can_return);
826 :
827 462794 : osl_releaseMutex( pPoolMutex );
828 :
829 : RTL_LOG_STRING_INTERN_NEW(*newStr, str);
830 :
831 462794 : if( can_return && *newStr != str )
832 : { /* we dupped, then found a match */
833 91445 : rtl_freeMemory( str );
834 : }
835 462794 : }
836 :
837 32346 : void SAL_CALL rtl_uString_intern( rtl_uString ** newStr,
838 : rtl_uString * str) SAL_THROW_EXTERN_C()
839 : {
840 32346 : if (SAL_STRING_IS_INTERN(str))
841 : {
842 1 : IMPL_RTL_AQUIRE( str );
843 1 : *newStr = str;
844 : }
845 : else
846 : {
847 32345 : rtl_uString *pOrg = *newStr;
848 32345 : *newStr = NULL;
849 32345 : rtl_ustring_intern_internal( newStr, str, CANNOT_RETURN );
850 32345 : if (pOrg)
851 0 : rtl_uString_release (pOrg);
852 : }
853 32346 : }
854 :
855 417286 : static int rtl_canGuessUOutputLength( int len, rtl_TextEncoding eTextEncoding )
856 : {
857 : // FIXME: Maybe we should use a bit flag in the higher bits of the
858 : // eTextEncoding value itself to determine the encoding type. But if we
859 : // do, be sure to mask the value in certain places that expect the values
860 : // to be numbered serially from 0 and up. One such place is
861 : // Impl_getTextEncodingData().
862 :
863 417286 : switch ( eTextEncoding )
864 : {
865 : // 1 to 1 (with no zero elements)
866 : case RTL_TEXTENCODING_IBM_437:
867 : case RTL_TEXTENCODING_IBM_850:
868 : case RTL_TEXTENCODING_IBM_860:
869 : case RTL_TEXTENCODING_IBM_861:
870 : case RTL_TEXTENCODING_IBM_863:
871 : case RTL_TEXTENCODING_IBM_865:
872 0 : return len;
873 : break;
874 : }
875 417286 : return 0;
876 : }
877 :
878 430449 : void SAL_CALL rtl_uString_internConvert( rtl_uString ** newStr,
879 : const sal_Char * str,
880 : sal_Int32 len,
881 : rtl_TextEncoding eTextEncoding,
882 : sal_uInt32 convertFlags,
883 : sal_uInt32 * pInfo )
884 : SAL_THROW_EXTERN_C()
885 : {
886 : rtl_uString *scratch;
887 :
888 430449 : if (*newStr)
889 : {
890 0 : rtl_uString_release (*newStr);
891 0 : *newStr = NULL;
892 : }
893 :
894 430449 : if ( len < 256 )
895 : { // try various optimisations
896 : sal_Int32 ulen;
897 430449 : if ( len < 0 )
898 0 : len = strlen( str );
899 430449 : if ( eTextEncoding == RTL_TEXTENCODING_ASCII_US )
900 : {
901 : int i;
902 : rtl_uString *pScratch;
903 : pScratch = static_cast< rtl_uString * >(
904 13163 : alloca(sizeof (rtl_uString) + len * sizeof (sal_Unicode)));
905 136042 : for (i = 0; i < len; i++)
906 : {
907 : /* Check ASCII range */
908 : SAL_WARN_IF( ((unsigned char)str[i]) > 127, "rtl.string",
909 : "rtl_ustring_internConvert() - Found char > 127 and RTL_TEXTENCODING_ASCII_US is specified" );
910 122879 : pScratch->buffer[i] = str[i];
911 : }
912 13163 : pScratch->length = len;
913 13163 : rtl_ustring_intern_internal( newStr, pScratch, CANNOT_RETURN );
914 26326 : return;
915 : }
916 417286 : else if ( (ulen = rtl_canGuessUOutputLength(len, eTextEncoding)) != 0 )
917 : {
918 : rtl_uString *pScratch;
919 : rtl_TextToUnicodeConverter hConverter;
920 : sal_Size nSrcBytes;
921 : sal_uInt32 nInfo;
922 :
923 : pScratch = static_cast< rtl_uString * >(
924 0 : alloca(
925 0 : sizeof (rtl_uString) + ulen * sizeof (sal_Unicode)));
926 :
927 0 : hConverter = rtl_createTextToUnicodeConverter( eTextEncoding );
928 : rtl_convertTextToUnicode(
929 0 : hConverter, 0, str, len, pScratch->buffer, ulen, convertFlags, &nInfo, &nSrcBytes );
930 0 : rtl_destroyTextToUnicodeConverter( hConverter );
931 :
932 0 : if (pInfo)
933 0 : *pInfo = nInfo;
934 :
935 0 : pScratch->length = ulen;
936 0 : rtl_ustring_intern_internal( newStr, pScratch, CANNOT_RETURN );
937 0 : return;
938 : }
939 :
940 : /* FIXME: we want a nice UTF-8 / alloca shortcut here */
941 : }
942 :
943 417286 : scratch = NULL;
944 : rtl_string2UString_status( &scratch, str, len, eTextEncoding, convertFlags,
945 417286 : pInfo );
946 417286 : if (!scratch) {
947 0 : return;
948 : }
949 417286 : rtl_ustring_intern_internal( newStr, scratch, CAN_RETURN );
950 : }
951 :
952 : static void
953 4709762 : internRelease (rtl_uString *pThis)
954 : {
955 : oslMutex pPoolMutex;
956 :
957 4709762 : rtl_uString *pFree = NULL;
958 4709762 : if ( SAL_STRING_REFCOUNT(
959 : osl_atomic_decrement( &(pThis->refCount) ) ) == 0)
960 : {
961 : RTL_LOG_STRING_INTERN_DELETE(pThis);
962 354941 : pPoolMutex = getInternMutex();
963 354941 : osl_acquireMutex( pPoolMutex );
964 :
965 354941 : rtl_str_hash_remove (pThis);
966 :
967 : /* May have been separately acquired */
968 354941 : if ( SAL_STRING_REFCOUNT(
969 : osl_atomic_increment( &(pThis->refCount) ) ) == 1 )
970 : {
971 : /* we got the last ref */
972 354941 : pFree = pThis;
973 : }
974 : else /* very unusual */
975 : {
976 0 : internRelease (pThis);
977 : }
978 :
979 354941 : osl_releaseMutex( pPoolMutex );
980 : }
981 4709762 : if (pFree)
982 354941 : rtl_freeMemory (pFree);
983 4709762 : }
984 :
985 36034257 : sal_uInt32 SAL_CALL rtl_uString_iterateCodePoints(
986 : rtl_uString const * string, sal_Int32 * indexUtf16,
987 : sal_Int32 incrementCodePoints)
988 : {
989 : sal_Int32 n;
990 : sal_Unicode cu;
991 : sal_uInt32 cp;
992 : assert(string != NULL && indexUtf16 != NULL);
993 36034257 : n = *indexUtf16;
994 : assert(n >= 0 && n <= string->length);
995 72202655 : while (incrementCodePoints < 0) {
996 : assert(n > 0);
997 134141 : cu = string->buffer[--n];
998 134145 : if (SAL_RTL_IS_LOW_SURROGATE(cu) && n != 0 &&
999 8 : SAL_RTL_IS_HIGH_SURROGATE(string->buffer[n - 1]))
1000 : {
1001 3 : --n;
1002 : }
1003 134141 : ++incrementCodePoints;
1004 : }
1005 : assert(n >= 0 && n < string->length);
1006 36034257 : cu = string->buffer[n];
1007 36034268 : if (SAL_RTL_IS_HIGH_SURROGATE(cu) && string->length - n >= 2 &&
1008 22 : SAL_RTL_IS_LOW_SURROGATE(string->buffer[n + 1]))
1009 : {
1010 11 : cp = SAL_RTL_COMBINE_SURROGATES(cu, string->buffer[n + 1]);
1011 : } else {
1012 36034246 : cp = cu;
1013 : }
1014 89344729 : while (incrementCodePoints > 0) {
1015 : assert(n < string->length);
1016 17276215 : cu = string->buffer[n++];
1017 17276223 : if (SAL_RTL_IS_HIGH_SURROGATE(cu) && n != string->length &&
1018 16 : SAL_RTL_IS_LOW_SURROGATE(string->buffer[n]))
1019 : {
1020 8 : ++n;
1021 : }
1022 17276215 : --incrementCodePoints;
1023 : }
1024 : assert(n >= 0 && n <= string->length);
1025 36034257 : *indexUtf16 = n;
1026 36034257 : return cp;
1027 : }
1028 :
1029 17075340 : sal_Bool rtl_convertStringToUString(
1030 : rtl_uString ** target, char const * source, sal_Int32 length,
1031 : rtl_TextEncoding encoding, sal_uInt32 flags) SAL_THROW_EXTERN_C()
1032 : {
1033 : sal_uInt32 info;
1034 17075340 : rtl_string2UString_status(target, source, length, encoding, flags, &info);
1035 17075340 : return (info & RTL_TEXTTOUNICODE_INFO_ERROR) == 0;
1036 : }
1037 :
1038 164803 : void rtl_uString_newReplaceFirst(
1039 : rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
1040 : rtl_uString const * to, sal_Int32 * index) SAL_THROW_EXTERN_C()
1041 : {
1042 : assert(str != 0);
1043 : assert(index != 0);
1044 : assert(*index >= 0 && *index <= str->length);
1045 : assert(from != 0);
1046 : assert(to != 0);
1047 : sal_Int32 i = rtl_ustr_indexOfStr_WithLength(
1048 164803 : str->buffer + *index, str->length - *index, from->buffer, from->length);
1049 164803 : if (i == -1) {
1050 145194 : rtl_uString_assign(newStr, str);
1051 : } else {
1052 : assert(i <= str->length - *index);
1053 19609 : i += *index;
1054 : assert(from->length <= str->length);
1055 19609 : if (str->length - from->length > SAL_MAX_INT32 - to->length) {
1056 0 : std::abort();
1057 : }
1058 19609 : sal_Int32 n = str->length - from->length + to->length;
1059 19609 : rtl_uString_acquire(str); // in case *newStr == str
1060 19609 : rtl_uString_new_WithLength(newStr, n);
1061 19609 : if (n != 0) {
1062 19608 : (*newStr)->length = n;
1063 : assert(i >= 0 && i < str->length);
1064 : memcpy(
1065 19608 : (*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
1066 : memcpy(
1067 19608 : (*newStr)->buffer + i, to->buffer,
1068 39216 : to->length * sizeof (sal_Unicode));
1069 : memcpy(
1070 19608 : (*newStr)->buffer + i + to->length,
1071 19608 : str->buffer + i + from->length,
1072 39216 : (str->length - i - from->length) * sizeof (sal_Unicode));
1073 : }
1074 19609 : rtl_uString_release(str);
1075 : }
1076 164803 : *index = i;
1077 164803 : }
1078 :
1079 23890 : void rtl_uString_newReplaceFirstAsciiL(
1080 : rtl_uString ** newStr, rtl_uString * str, char const * from,
1081 : sal_Int32 fromLength, rtl_uString const * to, sal_Int32 * index)
1082 : SAL_THROW_EXTERN_C()
1083 : {
1084 : assert(str != 0);
1085 : assert(index != 0);
1086 : assert(*index >= 0 && *index <= str->length);
1087 : assert(fromLength >= 0);
1088 : assert(to != 0);
1089 : sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
1090 23890 : str->buffer + *index, str->length - *index, from, fromLength);
1091 23890 : if (i == -1) {
1092 11049 : rtl_uString_assign(newStr, str);
1093 : } else {
1094 : assert(i <= str->length - *index);
1095 12841 : i += *index;
1096 : assert(fromLength <= str->length);
1097 12841 : if (str->length - fromLength > SAL_MAX_INT32 - to->length) {
1098 0 : std::abort();
1099 : }
1100 12841 : sal_Int32 n = str->length - fromLength + to->length;
1101 12841 : rtl_uString_acquire(str); // in case *newStr == str
1102 12841 : if (n != 0) {
1103 12841 : rtl_uString_new_WithLength(newStr, n);
1104 12841 : (*newStr)->length = n;
1105 : assert(i >= 0 && i < str->length);
1106 : memcpy(
1107 12841 : (*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
1108 : memcpy(
1109 12841 : (*newStr)->buffer + i, to->buffer,
1110 25682 : to->length * sizeof (sal_Unicode));
1111 : memcpy(
1112 12841 : (*newStr)->buffer + i + to->length,
1113 12841 : str->buffer + i + fromLength,
1114 25682 : (str->length - i - fromLength) * sizeof (sal_Unicode));
1115 : }
1116 12841 : rtl_uString_release(str);
1117 : }
1118 23890 : *index = i;
1119 23890 : }
1120 :
1121 53679 : void rtl_uString_newReplaceFirstAsciiLAsciiL(
1122 : rtl_uString ** newStr, rtl_uString * str, char const * from,
1123 : sal_Int32 fromLength, char const * to, sal_Int32 toLength,
1124 : sal_Int32 * index) SAL_THROW_EXTERN_C()
1125 : {
1126 : assert(str != 0);
1127 : assert(index != 0);
1128 : assert(*index >= 0 && *index <= str->length);
1129 : assert(fromLength >= 0);
1130 : assert(to != 0);
1131 : assert(toLength >= 0);
1132 : sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
1133 53679 : str->buffer + *index, str->length - *index, from, fromLength);
1134 53679 : if (i == -1) {
1135 13310 : rtl_uString_assign(newStr, str);
1136 : } else {
1137 : assert(i <= str->length - *index);
1138 40369 : i += *index;
1139 : assert(fromLength <= str->length);
1140 40369 : if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
1141 0 : std::abort();
1142 : }
1143 40369 : sal_Int32 n = str->length - fromLength + toLength;
1144 40369 : rtl_uString_acquire(str); // in case *newStr == str
1145 40369 : if (n != 0) {
1146 40369 : rtl_uString_new_WithLength(newStr, n);
1147 40369 : (*newStr)->length = n;
1148 : assert(i >= 0 && i < str->length);
1149 : memcpy(
1150 40369 : (*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
1151 217854 : for (sal_Int32 j = 0; j != toLength; ++j) {
1152 : assert(static_cast< unsigned char >(to[j]) <= 0x7F);
1153 177485 : (*newStr)->buffer[i + j] = to[j];
1154 : }
1155 : memcpy(
1156 40369 : (*newStr)->buffer + i + toLength,
1157 40369 : str->buffer + i + fromLength,
1158 80738 : (str->length - i - fromLength) * sizeof (sal_Unicode));
1159 : }
1160 40369 : rtl_uString_release(str);
1161 : }
1162 53679 : *index = i;
1163 53679 : }
1164 :
1165 0 : void rtl_uString_newReplaceAll(
1166 : rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
1167 : rtl_uString const * to) SAL_THROW_EXTERN_C()
1168 : {
1169 0 : rtl_uString_newReplaceAllFromIndex( newStr, str, from, to, 0 );
1170 0 : }
1171 :
1172 144466 : void rtl_uString_newReplaceAllFromIndex(
1173 : rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
1174 : rtl_uString const * to, sal_Int32 fromIndex) SAL_THROW_EXTERN_C()
1175 : {
1176 : assert(to != 0);
1177 : assert(fromIndex >= 0 && fromIndex <= str->length);
1178 144466 : rtl_uString_assign(newStr, str);
1179 162936 : for (sal_Int32 i = fromIndex;; i += to->length) {
1180 162936 : rtl_uString_newReplaceFirst(newStr, *newStr, from, to, &i);
1181 162936 : if (i == -1) {
1182 144466 : break;
1183 : }
1184 18470 : }
1185 144466 : }
1186 :
1187 10915 : void rtl_uString_newReplaceAllAsciiL(
1188 : rtl_uString ** newStr, rtl_uString * str, char const * from,
1189 : sal_Int32 fromLength, rtl_uString const * to) SAL_THROW_EXTERN_C()
1190 : {
1191 : assert(to != 0);
1192 10915 : rtl_uString_assign(newStr, str);
1193 12371 : for (sal_Int32 i = 0;; i += to->length) {
1194 : rtl_uString_newReplaceFirstAsciiL(
1195 12371 : newStr, *newStr, from, fromLength, to, &i);
1196 12371 : if (i == -1) {
1197 10915 : break;
1198 : }
1199 1456 : }
1200 10915 : }
1201 :
1202 13304 : void rtl_uString_newReplaceAllAsciiLAsciiL(
1203 : rtl_uString ** newStr, rtl_uString * str, char const * from,
1204 : sal_Int32 fromLength, char const * to, sal_Int32 toLength)
1205 : SAL_THROW_EXTERN_C()
1206 : {
1207 : assert(toLength >= 0);
1208 13304 : rtl_uString_assign(newStr, str);
1209 49880 : for (sal_Int32 i = 0;; i += toLength) {
1210 : rtl_uString_newReplaceFirstAsciiLAsciiL(
1211 49880 : newStr, *newStr, from, fromLength, to, toLength, &i);
1212 49880 : if (i == -1) {
1213 13304 : break;
1214 : }
1215 36576 : }
1216 13304 : }
1217 :
1218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|