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
|
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
|
-
-
+
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
+
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-
-
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
+
-
+
+
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#import "OFString.h"
#import "OFMutableString_UTF8.h"
#import "OFAutoreleasePool.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"
#import "macros.h"
#import "of_asprintf.h"
#import "unicode.h"
static struct {
Class isa;
} placeholder;
@interface OFMutableString_placeholder: OFMutableString
@end
@implementation OFMutableString_placeholder
- init
{
return (id)[[OFMutableString_UTF8 alloc] init];
}
- initWithUTF8String: (const char*)UTF8String
{
return (id)[[OFMutableString_UTF8 alloc]
initWithUTF8String: UTF8String];
}
- initWithUTF8String: (const char*)UTF8String
length: (size_t)UTF8StringLength
{
return (id)[[OFMutableString_UTF8 alloc]
initWithUTF8String: UTF8String
length: UTF8StringLength];
}
- initWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
{
return (id)[[OFMutableString_UTF8 alloc] initWithCString: cString
encoding: encoding];
}
- initWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
length: (size_t)cStringLength
{
return (id)[[OFMutableString_UTF8 alloc]
initWithCString: cString
encoding: encoding
length: cStringLength];
}
- initWithString: (OFString*)string
{
return (id)[[OFMutableString_UTF8 alloc] initWithString: string];
}
- initWithUnicodeString: (const of_unichar_t*)string
{
return (id)[[OFMutableString_UTF8 alloc] initWithUnicodeString: string];
}
- initWithUnicodeString: (const of_unichar_t*)string
byteOrder: (of_endianess_t)byteOrder
{
return (id)[[OFMutableString_UTF8 alloc]
initWithUnicodeString: string
byteOrder: byteOrder];
}
- initWithUnicodeString: (const of_unichar_t*)string
length: (size_t)length
{
return (id)[[OFMutableString_UTF8 alloc] initWithUnicodeString: string
length: length];
}
- initWithUnicodeString: (const of_unichar_t*)string
byteOrder: (of_endianess_t)byteOrder
length: (size_t)length
{
return (id)[[OFMutableString_UTF8 alloc]
initWithUnicodeString: string
byteOrder: byteOrder
length: length];
}
- initWithUTF16String: (const uint16_t*)string
{
return (id)[[OFMutableString_UTF8 alloc] initWithUTF16String: string];
}
- initWithUTF16String: (const uint16_t*)string
byteOrder: (of_endianess_t)byteOrder
{
return (id)[[OFMutableString_UTF8 alloc]
initWithUTF16String: string
byteOrder: byteOrder];
}
- initWithUTF16String: (const uint16_t*)string
length: (size_t)length
{
return (id)[[OFMutableString_UTF8 alloc] initWithUTF16String: string
length: length];
}
- initWithUTF16String: (const uint16_t*)string
byteOrder: (of_endianess_t)byteOrder
length: (size_t)length
{
return (id)[[OFMutableString_UTF8 alloc]
initWithUTF16String: string
byteOrder: byteOrder
length: length];
}
- initWithFormat: (OFConstantString*)format, ...
{
id ret;
va_list arguments;
va_start(arguments, format);
ret = [[OFMutableString_UTF8 alloc] initWithFormat: format
arguments: arguments];
va_end(arguments);
return ret;
}
- initWithFormat: (OFConstantString*)format
arguments: (va_list)arguments
{
return (id)[[OFMutableString_UTF8 alloc] initWithFormat: format
arguments: arguments];
}
- initWithPath: (OFString*)firstComponent, ...
{
id ret;
va_list arguments;
va_start(arguments, firstComponent);
ret = [[OFMutableString_UTF8 alloc] initWithPath: firstComponent
arguments: arguments];
va_end(arguments);
return ret;
}
- initWithPath: (OFString*)firstComponent
arguments: (va_list)arguments
{
return (id)[[OFMutableString_UTF8 alloc] initWithPath: firstComponent
arguments: arguments];
}
- initWithContentsOfFile: (OFString*)path
{
return (id)[[OFMutableString_UTF8 alloc] initWithContentsOfFile: path];
}
- initWithContentsOfFile: (OFString*)path
encoding: (of_string_encoding_t)encoding
{
return (id)[[OFMutableString_UTF8 alloc]
initWithContentsOfFile: path
encoding: encoding];
}
- initWithContentsOfURL: (OFURL*)URL
{
return (id)[[OFMutableString_UTF8 alloc] initWithContentsOfURL: URL];
}
- initWithContentsOfURL: (OFURL*)URL
encoding: (of_string_encoding_t)encoding
{
return (id)[[OFMutableString_UTF8 alloc]
initWithContentsOfURL: URL
encoding: encoding];
}
- initWithSerialization: (OFXMLElement*)element
{
return (id)[[OFMutableString_UTF8 alloc]
initWithSerialization: element];
}
- retain
{
return self;
}
- autorelease
{
return self;
}
- (void)release
{
}
- (void)dealloc
{
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
[super dealloc]; /* Get rid of a stupid warning */
}
@end
@implementation OFMutableString
+ (void)initialize
{
if (self == [OFMutableString class])
placeholder.isa = [OFMutableString_placeholder class];
}
+ alloc
{
if (self == [OFMutableString class])
return (id)&placeholder;
return [super alloc];
}
- (void)_applyTable: (const of_unichar_t* const[])table
withSize: (size_t)tableSize
{
of_unichar_t c;
of_unichar_t *unicodeString;
size_t unicodeLen, newCStringLength, cLen;
size_t i, j, d;
char *newCString;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if (!s->UTF8) {
assert(tableSize >= 1);
const of_unichar_t *string = [self unicodeString];
uint8_t *p = (uint8_t*)s->cString + s->cStringLength;
uint8_t t;
while (--p >= (uint8_t*)s->cString)
if ((t = table[0][*p]) != 0)
*p = t;
return;
}
unicodeLen = [self length];
size_t i, length = [self length];
unicodeString = [self allocMemoryForNItems: unicodeLen
ofSize: sizeof(of_unichar_t)];
i = j = 0;
newCStringLength = 0;
for (i = 0; i < length; i++) {
while (i < s->cStringLength) {
cLen = of_string_utf8_to_unicode(s->cString + i,
s->cStringLength - i, &c);
of_unichar_t c = string[i];
if (cLen == 0 || c > 0x10FFFF) {
[self freeMemory: unicodeString];
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
}
if (c >> 8 < tableSize) {
of_unichar_t tc = table[c >> 8][c & 0xFF];
if (c >> 8 < tableSize && table[c >> 8][c & 0xFF])
[self setCharacter: table[c >> 8][c & 0xFF]
atIndex: i];
if (tc)
c = tc;
}
}
unicodeString[j++] = c;
if (c < 0x80)
newCStringLength++;
else if (c < 0x800)
newCStringLength += 2;
else if (c < 0x10000)
newCStringLength += 3;
else if (c < 0x110000)
newCStringLength += 4;
else {
[self freeMemory: unicodeString];
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
}
[pool release];
i += cLen;
}
}
@try {
newCString = [self allocMemoryWithSize: newCStringLength + 1];
} @catch (id e) {
[self freeMemory: unicodeString];
- (void)setCharacter: (of_unichar_t)character
@throw e;
}
atIndex: (size_t)index
j = 0;
{
for (i = 0; i < unicodeLen; i++) {
if ((d = of_string_unicode_to_utf8(unicodeString[i],
newCString + j)) == 0) {
[self freeMemory: unicodeString];
[self freeMemory: newCString];
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
@throw [OFNotImplementedException exceptionWithClass: isa
}
j += d;
}
selector: _cmd];
assert(j == newCStringLength);
newCString[j] = 0;
[self freeMemory: unicodeString];
[self freeMemory: s->cString];
s->cString = newCString;
s->cStringLength = newCStringLength;
/*
* Even though cStringLength can change, length cannot, therefore no
* need to change it.
*/
}
- (void)appendUTF8String: (const char*)UTF8String
{
size_t UTF8StringLength = strlen(UTF8String);
size_t length;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
UTF8String += 3;
UTF8StringLength -= 3;
}
[self appendString: [OFString stringWithUTF8String: UTF8String]];
switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
case 1:
s->UTF8 = YES;
break;
case -1:
@throw [OFInvalidEncodingException exceptionWithClass: isa];
}
[pool release];
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength +
UTF8StringLength + 1];
memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1);
}
s->cStringLength += UTF8StringLength;
s->length += length;
}
- (void)appendUTF8String: (const char*)UTF8String
withLength: (size_t)UTF8StringLength
{
size_t length;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
UTF8String += 3;
UTF8StringLength -= 3;
}
[self appendString: [OFString stringWithUTF8String: UTF8String
switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
case 1:
s->UTF8 = YES;
break;
case -1:
@throw [OFInvalidEncodingException exceptionWithClass: isa];
}
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength +
UTF8StringLength + 1];
length: UTF8StringLength]];
memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength);
s->cStringLength += UTF8StringLength;
s->length += length;
[pool release];
s->cString[s->cStringLength] = 0;
}
- (void)appendCString: (const char*)cString
withEncoding: (of_string_encoding_t)encoding
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
return [self appendCString: cString
withEncoding: encoding
length: strlen(cString)];
[self appendString: [OFString stringWithCString: cString
encoding: encoding]];
[pool release];
}
- (void)appendCString: (const char*)cString
withEncoding: (of_string_encoding_t)encoding
length: (size_t)cStringLength
{
if (encoding == OF_STRING_ENCODING_UTF_8)
[self appendUTF8String: cString
withLength: cStringLength];
else {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
[self appendString:
[OFString stringWithCString: cString
encoding: encoding
length: cStringLength]];
[pool release];
}
}
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
[self appendString: [OFString stringWithCString: cString
encoding: encoding
length: cStringLength]];
[pool release];
}
- (void)appendString: (OFString*)string
{
size_t UTF8StringLength;
return [self insertString: string
if (string == nil)
@throw [OFInvalidArgumentException exceptionWithClass: isa
selector: _cmd];
atIndex: [self length]];
UTF8StringLength = [string UTF8StringLength];
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength +
UTF8StringLength + 1];
memcpy(s->cString + s->cStringLength, string->s->cString,
UTF8StringLength);
s->cStringLength += UTF8StringLength;
s->length += string->s->length;
s->cString[s->cStringLength] = 0;
if (string->s->UTF8)
s->UTF8 = YES;
}
- (void)appendFormat: (OFConstantString*)format, ...
{
va_list arguments;
va_start(arguments, format);
|
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
|
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
|
-
+
-
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
+
-
-
-
+
-
+
+
-
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
+
-
-
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
+
-
+
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
+
+
+
+
+
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-
|
{
return [self insertString: string
atIndex: 0];
}
- (void)reverse
{
size_t i, j;
size_t i, j, length = [self length];
/* We reverse all bytes and restore UTF-8 later, if necessary */
for (i = 0, j = s->cStringLength - 1; i < s->cStringLength / 2;
i++, j--) {
for (i = 0, j = length - 1; i < length / 2; i++, j--) {
s->cString[i] ^= s->cString[j];
s->cString[j] ^= s->cString[i];
s->cString[i] ^= s->cString[j];
}
of_unichar_t tmp = [self characterAtIndex: j];
if (!s->UTF8)
return;
[self setCharacter: [self characterAtIndex: i]
for (i = 0; i < s->cStringLength; i++) {
/* ASCII */
if (OF_LIKELY(!(s->cString[i] & 0x80)))
continue;
atIndex: j];
/* A start byte can't happen first as we reversed everything */
if (OF_UNLIKELY(s->cString[i] & 0x40))
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
[self setCharacter: tmp
/* Next byte must not be ASCII */
if (OF_UNLIKELY(s->cStringLength < i + 1 ||
!(s->cString[i + 1] & 0x80)))
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
atIndex: i];
/* Next byte is the start byte */
if (OF_LIKELY(s->cString[i + 1] & 0x40)) {
s->cString[i] ^= s->cString[i + 1];
s->cString[i + 1] ^= s->cString[i];
s->cString[i] ^= s->cString[i + 1];
i++;
continue;
}
/* Second next byte must not be ASCII */
if (OF_UNLIKELY(s->cStringLength < i + 2 ||
!(s->cString[i + 2] & 0x80)))
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
/* Second next byte is the start byte */
if (OF_LIKELY(s->cString[i + 2] & 0x40)) {
s->cString[i] ^= s->cString[i + 2];
s->cString[i + 2] ^= s->cString[i];
s->cString[i] ^= s->cString[i + 2];
i += 2;
continue;
}
/* Third next byte must not be ASCII */
if (OF_UNLIKELY(s->cStringLength < i + 3 ||
!(s->cString[i + 3] & 0x80)))
@throw [OFInvalidEncodingException
exceptionWithClass: isa];
/* Third next byte is the start byte */
if (OF_LIKELY(s->cString[i + 3] & 0x40)) {
s->cString[i] ^= s->cString[i + 3];
s->cString[i + 3] ^= s->cString[i];
s->cString[i] ^= s->cString[i + 3];
s->cString[i + 1] ^= s->cString[i + 2];
s->cString[i + 2] ^= s->cString[i + 1];
s->cString[i + 1] ^= s->cString[i + 2];
i += 3;
continue;
}
/* UTF-8 does not allow more than 4 bytes per character */
@throw [OFInvalidEncodingException exceptionWithClass: isa];
}
}
- (void)upper
{
[self _applyTable: of_unicode_upper_table
withSize: OF_UNICODE_UPPER_TABLE_SIZE];
}
- (void)lower
{
[self _applyTable: of_unicode_lower_table
withSize: OF_UNICODE_LOWER_TABLE_SIZE];
}
- (void)insertString: (OFString*)string
atIndex: (size_t)index
{
size_t newCStringLength;
if (index > s->length)
@throw [OFOutOfRangeException exceptionWithClass: isa];
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
if (s->UTF8)
index = of_string_index_to_position(s->cString, index,
s->cStringLength);
}
newCStringLength = s->cStringLength + [string UTF8StringLength];
s->cString = [self resizeMemory: s->cString
toSize: newCStringLength + 1];
memmove(s->cString + index + string->s->cStringLength,
s->cString + index, s->cStringLength - index);
memcpy(s->cString + index, string->s->cString,
string->s->cStringLength);
s->cString[newCStringLength] = '\0';
s->cStringLength = newCStringLength;
s->length += string->s->length;
}
- (void)deleteCharactersInRange: (of_range_t)range
{
size_t start = range.start;
size_t end = range.start + range.length;
if (start > end)
@throw [OFInvalidArgumentException exceptionWithClass: isa
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
if (end > s->length)
@throw [OFOutOfRangeException exceptionWithClass: isa];
s->length -= end - start;
if (s->UTF8) {
start = of_string_index_to_position(s->cString, start,
s->cStringLength);
end = of_string_index_to_position(s->cString, end,
s->cStringLength);
}
memmove(s->cString + start, s->cString + end, s->cStringLength - end);
s->cStringLength -= end - start;
s->cString[s->cStringLength] = 0;
@try {
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- (void)replaceCharactersInRange: (of_range_t)range
withString: (OFString*)replacement
{
size_t start = range.start;
size_t end = range.start + range.length;
size_t newCStringLength, newLength;
[self deleteCharactersInRange: range];
if (start > end)
@throw [OFInvalidArgumentException exceptionWithClass: isa
selector: _cmd];
if (end > s->length)
@throw [OFOutOfRangeException exceptionWithClass: isa];
newLength = s->length - (end - start) + [replacement length];
if (s->UTF8) {
start = of_string_index_to_position(s->cString, start,
s->cStringLength);
end = of_string_index_to_position(s->cString, end,
s->cStringLength);
}
newCStringLength = s->cStringLength - (end - start) +
replacement->s->cStringLength;
[self insertString: replacement
s->cString = [self resizeMemory: s->cString
toSize: newCStringLength + 1];
atIndex: range.start];
memmove(s->cString + end, s->cString + start +
replacement->s->cStringLength, s->cStringLength - end);
memcpy(s->cString + start, replacement->s->cString,
replacement->s->cStringLength);
s->cString[newCStringLength] = '\0';
}
s->cStringLength = newCStringLength;
s->length = newLength;
}
- (void)replaceOccurrencesOfString: (OFString*)string
withString: (OFString*)replacement
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init], *pool2;
const of_unichar_t *unicodeString;
const char *UTF8String = [string UTF8String];
const of_unichar_t *searchString = [string unicodeString];
const char *replacementUTF8String = [replacement UTF8String];
size_t UTF8StringLength = string->s->cStringLength;
size_t replacementUTF8StringLength = replacement->s->cStringLength;
size_t i, last, newCStringLength, newLength;
size_t length = [self length];
size_t searchLength = [string length];
size_t replacementLength = [replacement length];
size_t i, last;
char *newCString;
if (UTF8StringLength > s->cStringLength)
if (searchLength > length) {
[pool release];
return;
newCString = NULL;
newCStringLength = 0;
}
pool2 = [[OFAutoreleasePool alloc] init];
unicodeString = [self unicodeString];
newLength = s->length;
for (i = 0, last = 0; i <= s->cStringLength - UTF8StringLength; i++) {
if (memcmp(s->cString + i, UTF8String, UTF8StringLength))
for (i = 0, last = 0; i <= length - searchLength; i++) {
if (memcmp(unicodeString + i, searchString,
searchLength * sizeof(of_unichar_t)))
continue;
@try {
newCString = [self
[self replaceCharactersInRange: of_range(i, searchLength)
resizeMemory: newCString
toSize: newCStringLength + i - last +
replacementUTF8StringLength + 1];
withString: replacement];
} @catch (id e) {
[self freeMemory: newCString];
@throw e;
}
memcpy(newCString + newCStringLength, s->cString + last,
i - last);
memcpy(newCString + newCStringLength + i - last,
replacementUTF8String, replacementUTF8StringLength);
length -= searchLength;
newCStringLength += i - last + replacementUTF8StringLength;
newLength = newLength - string->s->length +
replacement->s->length;
length += replacementLength;
i += UTF8StringLength - 1;
i += replacementLength - 1;
last = i + 1;
}
[pool2 releaseObjects];
@try {
newCString = [self
unicodeString = [self unicodeString];
resizeMemory: newCString
toSize: newCStringLength +
s->cStringLength - last + 1];
} @catch (id e) {
[self freeMemory: newCString];
@throw e;
}
memcpy(newCString + newCStringLength, s->cString + last,
s->cStringLength - last);
newCStringLength += s->cStringLength - last;
newCString[newCStringLength] = 0;
[self freeMemory: s->cString];
s->cString = newCString;
s->cStringLength = newCStringLength;
s->length = newLength;
[pool release];
}
- (void)deleteLeadingWhitespaces
{
size_t i;
size_t i, length = [self length];
for (i = 0; i < s->cStringLength; i++)
if (s->cString[i] != ' ' && s->cString[i] != '\t' &&
s->cString[i] != '\n' && s->cString[i] != '\r' &&
s->cString[i] != '\f')
for (i = 0; i < length; i++) {
of_unichar_t c = [self characterAtIndex: i];
if (c != ' ' && c != '\t' && c != '\n' && c != '\r' &&
c != '\f')
break;
}
s->cStringLength -= i;
s->length -= i;
memmove(s->cString, s->cString + i, s->cStringLength);
s->cString[s->cStringLength] = '\0';
[self deleteCharactersInRange: of_range(0, i)];
@try {
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- (void)deleteTrailingWhitespaces
{
size_t d;
size_t length = [self length];
char *p;
ssize_t i;
d = 0;
for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' &&
*p != '\f')
break;
*p = '\0';
d++;
}
for (i = length - 1; i >= 0; i--) {
s->cStringLength -= d;
s->length -= d;
of_unichar_t c = [self characterAtIndex: i];
@try {
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
if (c != ' ' && c != '\t' && c != '\n' && c != '\r' &&
- (void)deleteEnclosingWhitespaces
{
size_t d, i;
char *p;
d = 0;
for (p = s->cString + s->cStringLength - 1; p >= s->cString; p--) {
if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' &&
*p != '\f')
c != '\f')
break;
}
*p = '\0';
d++;
}
[self deleteCharactersInRange: of_range(i + 1, length - i - 1)];
s->cStringLength -= d;
s->length -= d;
}
for (i = 0; i < s->cStringLength; i++)
if (s->cString[i] != ' ' && s->cString[i] != '\t' &&
s->cString[i] != '\n' && s->cString[i] != '\r' &&
s->cString[i] != '\f')
break;
s->cStringLength -= i;
- (void)deleteEnclosingWhitespaces
s->length -= i;
{
memmove(s->cString, s->cString + i, s->cStringLength);
s->cString[s->cStringLength] = '\0';
[self deleteLeadingWhitespaces];
@try {
s->cString = [self resizeMemory: s->cString
[self deleteTrailingWhitespaces];
toSize: s->cStringLength + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- copy
{
return [[OFString alloc] initWithString: self];
}
- (void)makeImmutable
{
isa = [OFString class];
}
@end
|