︙ | | | ︙ | |
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#ifdef OF_HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#import "OFUTF8String.h"
#import "OFUTF8String+Private.h"
#import "OFArray.h"
#import "OFMutableUTF8String.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
|
>
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#ifdef OF_HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#import "OFUTF8String.h"
#import "OFUTF8String+Private.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFMutableUTF8String.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
|
︙ | | | ︙ | |
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
- (instancetype)init
{
self = [super init];
@try {
_s = &_storage;
_s->cString = [self allocMemoryWithSize: 1];
_s->cString[0] = '\0';
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
|
|
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
- (instancetype)init
{
self = [super init];
@try {
_s = &_storage;
_s->cString = of_calloc(1, 1);
_s->freeWhenDone = true;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
︙ | | | ︙ | |
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
cString += 3;
cStringLength -= 3;
}
_s = &_storage;
_s->cString = [self allocMemoryWithSize: cStringLength + 1];
_s->cStringLength = cStringLength;
if (encoding == OF_STRING_ENCODING_UTF_8 ||
encoding == OF_STRING_ENCODING_ASCII) {
switch (of_string_utf8_check(cString, cStringLength,
&_s->length)) {
case 1:
if (encoding == OF_STRING_ENCODING_ASCII)
|
|
>
|
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
cString += 3;
cStringLength -= 3;
}
_s = &_storage;
_s->cString = of_malloc(cStringLength + 1, 1);
_s->cStringLength = cStringLength;
_s->freeWhenDone = true;
if (encoding == OF_STRING_ENCODING_UTF_8 ||
encoding == OF_STRING_ENCODING_ASCII) {
switch (of_string_utf8_check(cString, cStringLength,
&_s->length)) {
case 1:
if (encoding == OF_STRING_ENCODING_ASCII)
|
︙ | | | ︙ | |
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
|
(uint8_t)cString[i], buffer);
if (bytes == 0)
@throw [OFInvalidEncodingException
exception];
_s->cStringLength += bytes - 1;
_s->cString = [self
resizeMemory: _s->cString
size: _s->cStringLength + 1];
memcpy(_s->cString + j, buffer, bytes);
j += bytes;
}
_s->cString[_s->cStringLength] = 0;
return self;
}
switch (encoding) {
#define CASE(encoding, var) \
case encoding: \
table = var; \
tableOffset = var##_offset; \
break;
#ifdef HAVE_ISO_8859_2
CASE(OF_STRING_ENCODING_ISO_8859_2, of_iso_8859_2_table)
#endif
#ifdef HAVE_ISO_8859_3
CASE(OF_STRING_ENCODING_ISO_8859_3, of_iso_8859_3_table)
#endif
#ifdef HAVE_ISO_8859_15
|
|
<
|
|
|
|
|
|
|
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
|
(uint8_t)cString[i], buffer);
if (bytes == 0)
@throw [OFInvalidEncodingException
exception];
_s->cStringLength += bytes - 1;
_s->cString = of_realloc(_s->cString,
_s->cStringLength + 1, 1);
memcpy(_s->cString + j, buffer, bytes);
j += bytes;
}
_s->cString[_s->cStringLength] = 0;
return self;
}
switch (encoding) {
#define CASE(encoding, var) \
case encoding: \
table = var; \
tableOffset = var##_offset; \
break;
#ifdef HAVE_ISO_8859_2
CASE(OF_STRING_ENCODING_ISO_8859_2, of_iso_8859_2_table)
#endif
#ifdef HAVE_ISO_8859_3
CASE(OF_STRING_ENCODING_ISO_8859_3, of_iso_8859_3_table)
#endif
#ifdef HAVE_ISO_8859_15
|
︙ | | | ︙ | |
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
_s->isUTF8 = true;
byteLength = of_string_utf8_encode(unichar, buffer);
if (byteLength == 0)
@throw [OFInvalidEncodingException exception];
_s->cStringLength += byteLength - 1;
_s->cString = [self
resizeMemory: _s->cString
size: _s->cStringLength + 1];
memcpy(_s->cString + j, buffer, byteLength);
j += byteLength;
}
_s->cString[_s->cStringLength] = 0;
} @catch (id e) {
|
|
<
|
|
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
_s->isUTF8 = true;
byteLength = of_string_utf8_encode(unichar, buffer);
if (byteLength == 0)
@throw [OFInvalidEncodingException exception];
_s->cStringLength += byteLength - 1;
_s->cString = of_realloc(_s->cString,
_s->cStringLength + 1, 1);
memcpy(_s->cString + j, buffer, byteLength);
j += byteLength;
}
_s->cString[_s->cStringLength] = 0;
} @catch (id e) {
|
︙ | | | ︙ | |
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
|
}
- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
length: (size_t)UTF8StringLength
freeWhenDone: (bool)freeWhenDone
{
@try {
self = [super init];
} @catch (id e) {
if (freeWhenDone)
free(UTF8String);
@throw e;
}
@try {
_s = &_storage;
if (freeWhenDone)
_s->freeWhenDone = UTF8String;
if (UTF8StringLength >= 3 &&
memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) {
UTF8String += 3;
UTF8StringLength -= 3;
}
_s->cString = (char *)UTF8String;
_s->cStringLength = UTF8StringLength;
switch (of_string_utf8_check(UTF8String, UTF8StringLength,
&_s->length)) {
case 1:
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
<
|
<
<
<
<
<
<
<
<
<
<
<
>
>
>
>
|
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
|
}
- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
length: (size_t)UTF8StringLength
freeWhenDone: (bool)freeWhenDone
{
self = [super init];
@try {
_s = &_storage;
if (UTF8StringLength >= 3 &&
memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) {
UTF8String += 3;
UTF8StringLength -= 3;
}
switch (of_string_utf8_check(UTF8String, UTF8StringLength,
&_s->length)) {
case 1:
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->cString = (char *)UTF8String;
_s->cStringLength = UTF8StringLength;
_s->freeWhenDone = freeWhenDone;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
︙ | | | ︙ | |
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
|
[string isKindOfClass: [OFMutableUTF8String class]])
_s->isUTF8 = ((OFUTF8String *)string)->_s->isUTF8;
else
_s->isUTF8 = true;
_s->length = string.length;
_s->cString = [self allocMemoryWithSize: _s->cStringLength + 1];
memcpy(_s->cString, string.UTF8String, _s->cStringLength + 1);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (instancetype)initWithCharacters: (const of_unichar_t *)characters
length: (size_t)length
{
self = [super init];
@try {
size_t j;
_s = &_storage;
_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
_s->length = length;
j = 0;
for (size_t i = 0; i < length; i++) {
size_t len = of_string_utf8_encode(characters[i],
_s->cString + j);
if (len == 0)
@throw [OFInvalidEncodingException exception];
if (len > 1)
_s->isUTF8 = true;
j += len;
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = [self resizeMemory: _s->cString
size: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
|
>
|
>
|
<
|
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
|
[string isKindOfClass: [OFMutableUTF8String class]])
_s->isUTF8 = ((OFUTF8String *)string)->_s->isUTF8;
else
_s->isUTF8 = true;
_s->length = string.length;
_s->cString = of_malloc(_s->cStringLength + 1, 1);
memcpy(_s->cString, string.UTF8String, _s->cStringLength + 1);
_s->freeWhenDone = true;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (instancetype)initWithCharacters: (const of_unichar_t *)characters
length: (size_t)length
{
self = [super init];
@try {
size_t j;
_s = &_storage;
_s->cString = of_malloc((length * 4) + 1, 1);
_s->length = length;
_s->freeWhenDone = true;
j = 0;
for (size_t i = 0; i < length; i++) {
size_t len = of_string_utf8_encode(characters[i],
_s->cString + j);
if (len == 0)
@throw [OFInvalidEncodingException exception];
if (len > 1)
_s->isUTF8 = true;
j += len;
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = of_realloc(_s->cString, j + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | | ︙ | |
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
string++;
length--;
} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
swap = true;
_s = &_storage;
_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
_s->length = length;
j = 0;
for (size_t i = 0; i < length; i++) {
of_unichar_t character =
(swap ? OF_BSWAP16(string[i]) : string[i]);
size_t len;
|
|
>
|
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
string++;
length--;
} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
swap = true;
_s = &_storage;
_s->cString = of_malloc((length * 4) + 1, 1);
_s->length = length;
_s->freeWhenDone = true;
j = 0;
for (size_t i = 0; i < length; i++) {
of_unichar_t character =
(swap ? OF_BSWAP16(string[i]) : string[i]);
size_t len;
|
︙ | | | ︙ | |
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
j += len;
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = [self resizeMemory: _s->cString
size: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
|
<
|
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
j += len;
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = of_realloc(_s->cString, j + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | | ︙ | |
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
characters++;
length--;
} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
swap = true;
_s = &_storage;
_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
_s->length = length;
j = 0;
for (size_t i = 0; i < length; i++) {
char buffer[4];
size_t len = of_string_utf8_encode(
(swap ? OF_BSWAP32(characters[i]) : characters[i]),
buffer);
|
|
>
|
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
characters++;
length--;
} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
swap = true;
_s = &_storage;
_s->cString = of_malloc((length * 4) + 1, 1);
_s->length = length;
_s->freeWhenDone = true;
j = 0;
for (size_t i = 0; i < length; i++) {
char buffer[4];
size_t len = of_string_utf8_encode(
(swap ? OF_BSWAP32(characters[i]) : characters[i]),
buffer);
|
︙ | | | ︙ | |
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
}
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = [self resizeMemory: _s->cString
size: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
|
<
|
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
}
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = of_realloc(_s->cString, j + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | | ︙ | |
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
|
case 1:
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->cString = [self
allocMemoryWithSize: cStringLength + 1];
memcpy(_s->cString, tmp, cStringLength + 1);
} @finally {
free(tmp);
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
if (_s != NULL && _s->freeWhenDone != NULL)
free(_s->freeWhenDone);
[super dealloc];
}
- (size_t)getCString: (char *)cString
maxLength: (size_t)maxLength
encoding: (of_string_encoding_t)encoding
|
|
<
>
|
|
|
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
|
case 1:
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->cString = of_malloc(cStringLength + 1, 1);
memcpy(_s->cString, tmp, cStringLength + 1);
_s->freeWhenDone = true;
} @finally {
free(tmp);
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
if (_s != NULL && _s->freeWhenDone)
free(_s->cString);
[super dealloc];
}
- (size_t)getCString: (char *)cString
maxLength: (size_t)maxLength
encoding: (of_string_encoding_t)encoding
|
︙ | | | ︙ | |
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
|
else if (_s->cStringLength - i < otherCStringLength - j)
return OF_ORDERED_ASCENDING;
#endif
return OF_ORDERED_SAME;
}
- (uint32_t)hash
{
uint32_t hash;
if (_s->hashed)
return _s->hash;
OF_HASH_INIT(hash);
|
|
|
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
|
else if (_s->cStringLength - i < otherCStringLength - j)
return OF_ORDERED_ASCENDING;
#endif
return OF_ORDERED_SAME;
}
- (unsigned long)hash
{
uint32_t hash;
if (_s->hashed)
return _s->hash;
OF_HASH_INIT(hash);
|
︙ | | | ︙ | |
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
|
objc_autoreleasePoolPop(pool);
return array;
}
- (const of_unichar_t *)characters
{
OFObject *object = [[[OFObject alloc] init] autorelease];
of_unichar_t *ret;
size_t i, j;
ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
count: _s->length];
i = j = 0;
while (i < _s->cStringLength) {
of_unichar_t c;
ssize_t cLen;
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF)
@throw [OFInvalidEncodingException exception];
ret[j++] = c;
i += cLen;
}
return ret;
}
- (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
{
OFObject *object = [[[OFObject alloc] init] autorelease];
of_char32_t *ret;
size_t i, j;
ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
count: _s->length + 1];
i = j = 0;
while (i < _s->cStringLength) {
of_unichar_t c;
ssize_t cLen;
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF)
@throw [OFInvalidEncodingException exception];
if (byteOrder != OF_BYTE_ORDER_NATIVE)
ret[j++] = OF_BSWAP32(c);
else
ret[j++] = c;
i += cLen;
}
ret[j] = 0;
return ret;
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
void *pool;
const char *cString = _s->cString;
|
<
|
|
<
<
<
<
<
|
>
|
>
|
|
>
>
>
<
|
|
<
<
<
<
<
|
|
>
|
>
|
|
|
|
>
>
>
|
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
|
objc_autoreleasePoolPop(pool);
return array;
}
- (const of_unichar_t *)characters
{
of_unichar_t *buffer = of_malloc(_s->length, sizeof(of_unichar_t));
size_t i = 0, j = 0;
while (i < _s->cStringLength) {
of_unichar_t c;
ssize_t cLen;
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF) {
free(buffer);
@throw [OFInvalidEncodingException exception];
}
buffer[j++] = c;
i += cLen;
}
return [[OFData dataWithItemsNoCopy: buffer
count: _s->length
itemSize: sizeof(of_unichar_t)
freeWhenDone: true] items];
}
- (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
{
of_char32_t *buffer = of_malloc(_s->length + 1, sizeof(of_char32_t));
size_t i = 0, j = 0;
while (i < _s->cStringLength) {
of_char32_t c;
ssize_t cLen;
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF) {
free(buffer);
@throw [OFInvalidEncodingException exception];
}
if (byteOrder != OF_BYTE_ORDER_NATIVE)
buffer[j++] = OF_BSWAP32(c);
else
buffer[j++] = c;
i += cLen;
}
buffer[j] = 0;
return [[OFData dataWithItemsNoCopy: buffer
count: _s->length + 1
itemSize: sizeof(of_char32_t)
freeWhenDone: true] items];
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
void *pool;
const char *cString = _s->cString;
|
︙ | | | ︙ | |