1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <string.h>
#include "boost/static_assert.hpp"
#include <osl/interlck.h>
#include <rtl/alloc.h>
#include <rtl/memory.h>
#include <rtl/tencinfo.h>
#include <rtl/instance.hxx>
#include <tools/string.hxx>
#include <impstrg.hxx>
#include <tools/debug.hxx>
DBG_NAME( UniString )
#define STRCODE sal_Unicode
#define STRCODEU sal_Unicode
#define STRING UniString
#define STRINGDATA UniStringData
#define DBGCHECKSTRING DbgCheckUniString
#define STRING_TYPE rtl_uString
#define STRING_ACQUIRE rtl_uString_acquire
#define STRING_RELEASE rtl_uString_release
#define STRING_NEW rtl_uString_new
#include <strimp.cxx>
#include <strucvt.cxx>
#include <strascii.cxx>
UniString::UniString(char c): mpData(ImplAllocData(1)) { mpData->maStr[0] = c; }
UniString UniString::CreateFromInt32( sal_Int32 n, sal_Int16 nRadix )
{
return rtl::OUString::valueOf(n, nRadix);
}
namespace { struct Empty : public rtl::Static< const UniString, Empty> {}; }
const UniString& UniString::EmptyString()
{
return Empty::get();
}
sal_Int32 UniString::ToInt32() const
{
DBG_CHKTHIS( UniString, DbgCheckUniString );
return rtl_ustr_toInt32( mpData->maStr, 10 );
}
sal_Int64 UniString::ToInt64() const
{
DBG_CHKTHIS( UniString, DbgCheckUniString );
return rtl_ustr_toInt64( mpData->maStr, 10 );
}
xub_StrLen STRING::SearchChar( const STRCODE* pChars, xub_StrLen nIndex ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
sal_Int32 nLen = mpData->mnLen;
const STRCODE* pStr = mpData->maStr;
pStr += nIndex;
while ( nIndex < nLen )
{
STRCODE c = *pStr;
const STRCODE* pCompStr = pChars;
while ( *pCompStr )
{
if ( *pCompStr == c )
return nIndex;
++pCompStr;
}
++pStr,
++nIndex;
}
return STRING_NOTFOUND;
}
xub_StrLen STRING::SearchAndReplace( STRCODE c, STRCODE cRep, xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
sal_Int32 nLen = mpData->mnLen;
const STRCODE* pStr = mpData->maStr;
pStr += nIndex;
while ( nIndex < nLen )
{
if ( *pStr == c )
{
ImplCopyData();
mpData->maStr[nIndex] = cRep;
return nIndex;
}
++pStr,
++nIndex;
}
return STRING_NOTFOUND;
}
STRING& STRING::Insert( const STRING& rStr, xub_StrLen nPos, xub_StrLen nLen,
xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// Determine string length
if ( nPos > rStr.mpData->mnLen )
nLen = 0;
else
{
// Correct length if necessary
sal_Int32 nMaxLen = rStr.mpData->mnLen-nPos;
if ( nLen > nMaxLen )
nLen = static_cast< xub_StrLen >(nMaxLen);
}
// Detect overflow
sal_Int32 nCopyLen = ImplGetCopyLen( mpData->mnLen, nLen );
if ( !nCopyLen )
return *this;
// Correct index if necessary
if ( nIndex > mpData->mnLen )
nIndex = static_cast< xub_StrLen >(mpData->mnLen);
// Determine new length and allocate string
STRINGDATA* pNewData = ImplAllocData( mpData->mnLen+nCopyLen );
// copy string to newdata
memcpy( pNewData->maStr, mpData->maStr, nIndex*sizeof( STRCODE ) );
memcpy( pNewData->maStr+nIndex, rStr.mpData->maStr+nPos, nCopyLen*sizeof( STRCODE ) );
memcpy( pNewData->maStr+nIndex+nCopyLen, mpData->maStr+nIndex,
(mpData->mnLen-nIndex)*sizeof( STRCODE ) );
// release old data
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
return *this;
}
static sal_Int32 ImplStringICompareWithoutZero( const STRCODE* pStr1, const STRCODE* pStr2,
sal_Int32 nCount )
{
sal_Int32 nRet = 0;
STRCODE c1;
STRCODE c2;
do
{
if ( !nCount )
break;
// convert if char is between 'A' and 'Z'
c1 = *pStr1;
c2 = *pStr2;
if ( (c1 >= 65) && (c1 <= 90) )
c1 += 32;
if ( (c2 >= 65) && (c2 <= 90) )
c2 += 32;
nRet = ((sal_Int32)((STRCODEU)c1))-((sal_Int32)((STRCODEU)c2));
++pStr1,
++pStr2,
--nCount;
}
while ( nRet == 0 );
return nRet;
}
sal_Bool STRING::EqualsIgnoreCaseAscii( const STRING& rStr, xub_StrLen nIndex, xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// Are there enough codes for comparing?
if ( nIndex > mpData->mnLen )
return (rStr.mpData->mnLen == 0);
sal_Int32 nMaxLen = mpData->mnLen-nIndex;
if ( nMaxLen < nLen )
{
if ( rStr.mpData->mnLen != nMaxLen )
return sal_False;
nLen = static_cast< xub_StrLen >(nMaxLen);
}
return (ImplStringICompareWithoutZero( mpData->maStr+nIndex, rStr.mpData->maStr, nLen ) == 0);
}
StringCompare STRING::CompareIgnoreCaseToAscii( const STRING& rStr,
xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
if ( mpData == rStr.mpData )
return COMPARE_EQUAL;
// determine maximal length
if ( mpData->mnLen < nLen )
nLen = static_cast< xub_StrLen >(mpData->mnLen+1);
if ( rStr.mpData->mnLen < nLen )
nLen = static_cast< xub_StrLen >(rStr.mpData->mnLen+1);
sal_Int32 nCompare = ImplStringICompareWithoutZero( mpData->maStr, rStr.mpData->maStr, nLen );
if ( nCompare == 0 )
return COMPARE_EQUAL;
else if ( nCompare < 0 )
return COMPARE_LESS;
else
return COMPARE_GREATER;
}
STRING& STRING::Fill( xub_StrLen nCount, STRCODE cFillChar )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
if ( !nCount )
return *this;
// extend string if fill length is larger
if ( nCount > mpData->mnLen )
{
// allocate string of new length
STRINGDATA* pNewData = ImplAllocData( nCount );
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
}
else
ImplCopyData();
STRCODE* pStr = mpData->maStr;
do
{
*pStr = cFillChar;
++pStr,
--nCount;
}
while ( nCount );
return *this;
}
STRING& STRING::Expand( xub_StrLen nCount, STRCODE cExpandChar )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// return if string doesn't need expanding
sal_Int32 nLen = mpData->mnLen;
if ( nCount <= nLen )
return *this;
// allocate string of new size
STRINGDATA* pNewData = ImplAllocData( nCount );
// copy from old string
memcpy( pNewData->maStr, mpData->maStr, nLen*sizeof( STRCODE ) );
// and expand using the given character
STRCODE* pStr = pNewData->maStr;
pStr += nLen;
for (sal_Int32 i = nCount - nLen; i > 0; --i) {
*pStr++ = cExpandChar;
}
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
return *this;
}
STRCODE* STRING::GetBufferAccess()
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// Copy data if necessary
if ( mpData->mnLen )
ImplCopyData();
// return pointer to string data
return mpData->maStr;
}
void STRING::ReleaseBufferAccess( xub_StrLen nLen )
{
// String not consinstent, thus no functionality test
DBG_CHKTHIS( STRING, NULL );
DBG_ASSERT( mpData->mnRefCount == 1, "String::ReleaseCharStr() called for String with RefCount" );
if ( nLen > mpData->mnLen )
nLen = ImplStringLen( mpData->maStr );
OSL_ASSERT(nLen <= mpData->mnLen);
if ( !nLen )
{
STRING_NEW((STRING_TYPE **)&mpData);
}
// shorten buffer is difference > 8 chars
else if ( mpData->mnLen - nLen > 8 )
{
STRINGDATA* pNewData = ImplAllocData( nLen );
memcpy( pNewData->maStr, mpData->maStr, nLen*sizeof( STRCODE ) );
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
}
else
mpData->mnLen = nLen;
}
STRCODE* STRING::AllocBuffer( xub_StrLen nLen )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
STRING_RELEASE((STRING_TYPE *)mpData);
if ( nLen )
mpData = ImplAllocData( nLen );
else
{
mpData = NULL;
STRING_NEW((STRING_TYPE **)&mpData);
}
return mpData->maStr;
}
STRING::STRING( STRCODE c )
{
DBG_CTOR( STRING, DBGCHECKSTRING );
DBG_ASSERT( c, "String::String() - c is 0" );
// Initalize maintenance data
mpData = ImplAllocData( 1 );
mpData->maStr[0] = c;
}
STRING& STRING::Insert( STRCODE c, xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// Don't insert 0 char or string size is maximum
if ( !c || (mpData->mnLen == STRING_MAXLEN) )
return *this;
// Adjust string index
if ( nIndex > mpData->mnLen )
nIndex = static_cast< xub_StrLen >(mpData->mnLen);
// allocate string of new size
STRINGDATA* pNewData = ImplAllocData( mpData->mnLen+1 );
// copy string
memcpy( pNewData->maStr, mpData->maStr, nIndex*sizeof( STRCODE ) );
pNewData->maStr[nIndex] = c;
memcpy( pNewData->maStr+nIndex+1, mpData->maStr+nIndex,
(mpData->mnLen-nIndex)*sizeof( STRCODE ) );
// free old data
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
return *this;
}
STRING& STRING::ToUpperAscii()
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
sal_Int32 nIndex = 0;
sal_Int32 nLen = mpData->mnLen;
STRCODE* pStr = mpData->maStr;
while ( nIndex < nLen )
{
// convert char if between 'a' and 'z'
if ( (*pStr >= 97) && (*pStr <= 122) )
{
// allocate string of new size
pStr = ImplCopyStringData( pStr );
*pStr -= 32;
}
++pStr,
++nIndex;
}
return *this;
}
StringCompare STRING::CompareTo( const STRING& rStr, xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
if ( mpData == rStr.mpData )
return COMPARE_EQUAL;
// determine maximal length
if ( mpData->mnLen < nLen )
nLen = static_cast< xub_StrLen >(mpData->mnLen+1);
if ( rStr.mpData->mnLen < nLen )
nLen = static_cast< xub_StrLen >(rStr.mpData->mnLen+1);
sal_Int32 nCompare = ImplStringCompareWithoutZero( mpData->maStr, rStr.mpData->maStr, nLen );
if ( nCompare == 0 )
return COMPARE_EQUAL;
else if ( nCompare < 0 )
return COMPARE_LESS;
else
return COMPARE_GREATER;
}
sal_Bool STRING::Equals( const STRING& rStr ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
if ( mpData == rStr.mpData )
return sal_True;
if ( mpData->mnLen != rStr.mpData->mnLen )
return sal_False;
return (ImplStringCompareWithoutZero( mpData->maStr, rStr.mpData->maStr, mpData->mnLen ) == 0);
}
sal_Bool STRING::EqualsIgnoreCaseAscii( const STRING& rStr ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
if ( mpData == rStr.mpData )
return sal_True;
if ( mpData->mnLen != rStr.mpData->mnLen )
return sal_False;
// compare string while ignoring case
return (ImplStringICompareWithoutZero( mpData->maStr, rStr.mpData->maStr, mpData->mnLen ) == 0);
}
sal_Bool STRING::Equals( const STRING& rStr, xub_StrLen nIndex, xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// Are there enough codes for comparing?
if ( nIndex > mpData->mnLen )
return (rStr.mpData->mnLen == 0);
sal_Int32 nMaxLen = mpData->mnLen-nIndex;
if ( nMaxLen < nLen )
{
if ( rStr.mpData->mnLen != nMaxLen )
return sal_False;
nLen = static_cast< xub_StrLen >(nMaxLen);
}
return (ImplStringCompareWithoutZero( mpData->maStr+nIndex, rStr.mpData->maStr, nLen ) == 0);
}
sal_Bool STRING::Equals( const STRCODE* pCharStr, xub_StrLen nIndex, xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// Are there enough codes for comparing?
if ( nIndex > mpData->mnLen )
return (*pCharStr == 0);
return (ImplStringCompare( mpData->maStr+nIndex, pCharStr, nLen ) == 0);
}
xub_StrLen STRING::Match( const STRING& rStr ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// return if string is empty
if ( !mpData->mnLen )
return STRING_MATCH;
// Search the string for unmatching chars
const STRCODE* pStr1 = mpData->maStr;
const STRCODE* pStr2 = rStr.mpData->maStr;
xub_StrLen i = 0;
while ( i < mpData->mnLen )
{
// Abort on the first unmatching char
if ( *pStr1 != *pStr2 )
return i;
++pStr1,
++pStr2,
++i;
}
return STRING_MATCH;
}
xub_StrLen STRING::SearchBackward( STRCODE c, xub_StrLen nIndex ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
if ( nIndex > mpData->mnLen )
nIndex = (xub_StrLen)mpData->mnLen;
const STRCODE* pStr = mpData->maStr;
pStr += nIndex;
while ( nIndex )
{
nIndex--;
pStr--;
if ( *pStr == c )
return nIndex;
}
return STRING_NOTFOUND;
}
void STRING::SearchAndReplaceAll( const STRING& rStr, const STRING& rRepStr )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rRepStr, STRING, DBGCHECKSTRING );
xub_StrLen nSPos = Search( rStr, 0 );
while ( nSPos != STRING_NOTFOUND )
{
Replace( nSPos, rStr.Len(), rRepStr );
nSPos = nSPos + rRepStr.Len();
nSPos = Search( rStr, nSPos );
}
}
void STRING::SetToken( xub_StrLen nToken, STRCODE cTok, const STRING& rStr,
xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
const STRCODE* pStr = mpData->maStr;
xub_StrLen nLen = (xub_StrLen)mpData->mnLen;
xub_StrLen nTok = 0;
xub_StrLen nFirstChar = nIndex;
xub_StrLen i = nFirstChar;
// Determine token position and length
pStr += i;
while ( i < nLen )
{
// Increase token count if match
if ( *pStr == cTok )
{
++nTok;
if ( nTok == nToken )
nFirstChar = i+1;
else
{
if ( nTok > nToken )
break;
}
}
++pStr,
++i;
}
if ( nTok >= nToken )
Replace( nFirstChar, i-nFirstChar, rStr );
}
STRING STRING::GetToken( xub_StrLen nToken, STRCODE cTok, xub_StrLen& rIndex ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
const STRCODE* pStr = mpData->maStr;
xub_StrLen nLen = (xub_StrLen)mpData->mnLen;
xub_StrLen nTok = 0;
xub_StrLen nFirstChar = rIndex;
xub_StrLen i = nFirstChar;
// Determine token position and length
pStr += i;
while ( i < nLen )
{
// Increase token count if match
if ( *pStr == cTok )
{
++nTok;
if ( nTok == nToken )
nFirstChar = i+1;
else
{
if ( nTok > nToken )
break;
}
}
++pStr,
++i;
}
if ( nTok >= nToken )
{
if ( i < nLen )
rIndex = i+1;
else
rIndex = STRING_NOTFOUND;
return Copy( nFirstChar, i-nFirstChar );
}
else
{
rIndex = STRING_NOTFOUND;
return STRING();
}
}
STRING& STRING::Append( const STRCODE* pCharStr, xub_StrLen nCharLen )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_ASSERT( pCharStr, "String::Append() - pCharStr is NULL" );
if ( nCharLen == STRING_LEN )
nCharLen = ImplStringLen( pCharStr );
#ifdef DBG_UTIL
if ( DbgIsAssert() )
{
for ( xub_StrLen i = 0; i < nCharLen; i++ )
{
if ( !pCharStr[i] )
{
OSL_FAIL( "String::Append() : nLen is wrong" );
}
}
}
#endif
// Catch overflow
sal_Int32 nLen = mpData->mnLen;
sal_Int32 nCopyLen = ImplGetCopyLen( nLen, nCharLen );
if ( nCopyLen )
{
// allocate string of new size
STRINGDATA* pNewData = ImplAllocData( nLen+nCopyLen );
// copy string
memcpy( pNewData->maStr, mpData->maStr, nLen*sizeof( STRCODE ) );
memcpy( pNewData->maStr+nLen, pCharStr, nCopyLen*sizeof( STRCODE ) );
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
}
return *this;
}
STRING& STRING::Append( STRCODE c )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// don't append null characters and keep string length < maxlen
sal_Int32 nLen = mpData->mnLen;
if ( c && (nLen < STRING_MAXLEN) )
{
// allocate string of new size
STRINGDATA* pNewData = ImplAllocData( nLen+1 );
// copy string
memcpy( pNewData->maStr, mpData->maStr, nLen*sizeof( STRCODE ) );
pNewData->maStr[nLen] = c;
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
}
return *this;
}
STRING& STRING::Assign( const STRCODE* pCharStr, xub_StrLen nLen )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_ASSERT( pCharStr, "String::Assign() - pCharStr is NULL" );
if ( nLen == STRING_LEN )
nLen = ImplStringLen( pCharStr );
#ifdef DBG_UTIL
if ( DbgIsAssert() )
{
for ( xub_StrLen i = 0; i < nLen; i++ )
{
if ( !pCharStr[i] )
{
OSL_FAIL( "String::Assign() : nLen is wrong" );
}
}
}
#endif
if ( !nLen )
{
STRING_NEW((STRING_TYPE **)&mpData);
}
else
{
// copy without allocation if string length is identical
if ( (nLen == mpData->mnLen) && (mpData->mnRefCount == 1) )
memcpy( mpData->maStr, pCharStr, nLen*sizeof( STRCODE ) );
else
{
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
// allocate string of new size and copy
mpData = ImplAllocData( nLen );
memcpy( mpData->maStr, pCharStr, nLen*sizeof( STRCODE ) );
}
}
return *this;
}
STRING& STRING::Assign( STRCODE c )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_ASSERT( c, "String::Assign() - c is 0" );
// initialize maintenance data
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = ImplAllocData( 1 );
mpData->maStr[0] = c;
return *this;
}
xub_StrLen STRING::SearchAndReplace( const STRING& rStr, const STRING& rRepStr,
xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rRepStr, STRING, DBGCHECKSTRING );
xub_StrLen nSPos = Search( rStr, nIndex );
if ( nSPos != STRING_NOTFOUND )
Replace( nSPos, rStr.Len(), rRepStr );
return nSPos;
}
static sal_Int32 ImplStringICompare( const STRCODE* pStr1, const STRCODE* pStr2 )
{
sal_Int32 nRet;
STRCODE c1;
STRCODE c2;
do
{
// Convert char if between 'A' and 'Z'
c1 = *pStr1;
c2 = *pStr2;
if ( (c1 >= 65) && (c1 <= 90) )
c1 += 32;
if ( (c2 >= 65) && (c2 <= 90) )
c2 += 32;
nRet = ((sal_Int32)((STRCODEU)c1))-((sal_Int32)((STRCODEU)c2));
if ( nRet != 0 )
break;
++pStr1,
++pStr2;
}
while ( c2 );
return nRet;
}
sal_Bool STRING::EqualsIgnoreCaseAscii( const STRCODE* pCharStr ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
return (ImplStringICompare( mpData->maStr, pCharStr ) == 0);
}
STRING& STRING::Assign( const STRCODE* pCharStr )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_ASSERT( pCharStr, "String::Assign() - pCharStr is NULL" );
xub_StrLen nLen = ImplStringLen( pCharStr );
if ( !nLen )
{
STRING_NEW((STRING_TYPE **)&mpData);
}
else
{
// copy without allocation if string length is identical
if ( (nLen == mpData->mnLen) && (mpData->mnRefCount == 1) )
memcpy( mpData->maStr, pCharStr, nLen*sizeof( STRCODE ) );
else
{
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
// allocate string of new size and copy
mpData = ImplAllocData( nLen );
memcpy( mpData->maStr, pCharStr, nLen*sizeof( STRCODE ) );
}
}
return *this;
}
xub_StrLen ImplStringLen( const sal_Char* pStr )
{
const sal_Char* pTempStr = pStr;
while( *pTempStr )
++pTempStr;
return (xub_StrLen)(pTempStr-pStr);
}
xub_StrLen ImplStringLen( const sal_Unicode* pStr )
{
const sal_Unicode* pTempStr = pStr;
while( *pTempStr )
++pTempStr;
return (xub_StrLen)(pTempStr-pStr);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|