1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
|
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
|
-
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
+
|
encoding: encoding
lossy: true];
}
- (const char *)of_cStringWithEncoding: (of_string_encoding_t)encoding
lossy: (bool)lossy
{
OFObject *object = [[[OFObject alloc] init] autorelease];
size_t length = self.length;
char *cString;
size_t cStringLength;
switch (encoding) {
case OF_STRING_ENCODING_UTF_8:;
size_t cStringLength;
case OF_STRING_ENCODING_UTF_8:
cString = of_malloc((length * 4) + 1, 1);
cString = [object allocMemoryWithSize: (length * 4) + 1];
cStringLength = [self of_getCString: cString
maxLength: (length * 4) + 1
encoding: OF_STRING_ENCODING_UTF_8
lossy: lossy];
@try {
cStringLength = [self
of_getCString: cString
maxLength: (length * 4) + 1
encoding: OF_STRING_ENCODING_UTF_8
lossy: lossy];
} @catch (id e) {
free(cString);
@throw e;
}
@try {
cString = [object resizeMemory: cString
size: cStringLength + 1];
cString = of_realloc(cString, cStringLength + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
break;
case OF_STRING_ENCODING_ASCII:
case OF_STRING_ENCODING_ISO_8859_1:
case OF_STRING_ENCODING_ISO_8859_2:
case OF_STRING_ENCODING_ISO_8859_3:
case OF_STRING_ENCODING_ISO_8859_15:
case OF_STRING_ENCODING_WINDOWS_1251:
case OF_STRING_ENCODING_WINDOWS_1252:
case OF_STRING_ENCODING_CODEPAGE_437:
case OF_STRING_ENCODING_CODEPAGE_850:
case OF_STRING_ENCODING_CODEPAGE_858:
case OF_STRING_ENCODING_MAC_ROMAN:
case OF_STRING_ENCODING_KOI8_R:
case OF_STRING_ENCODING_KOI8_U:
cString = [object allocMemoryWithSize: length + 1];
cString = of_malloc(length + 1, 1);
@try {
[self of_getCString: cString
maxLength: length + 1
encoding: encoding
lossy: lossy];
cStringLength = [self of_getCString: cString
maxLength: length + 1
encoding: encoding
lossy: lossy];
} @catch (id e) {
free(cString);
@throw e;
}
break;
default:
@throw [OFInvalidEncodingException exception];
}
return [OFData dataWithItemsNoCopy: cString
return cString;
count: cStringLength + 1
freeWhenDone: true].items;
}
- (const char *)cStringWithEncoding: (of_string_encoding_t)encoding
{
return [self of_cStringWithEncoding: encoding
lossy: false];
}
|
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
|
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
|
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
-
+
-
+
+
+
-
-
+
+
-
+
-
-
+
+
-
+
-
+
-
-
+
-
-
+
+
+
+
|
objc_autoreleasePoolPop(pool);
return value;
}
- (const of_unichar_t *)characters
{
OFObject *object = [[[OFObject alloc] init] autorelease];
size_t length = self.length;
of_unichar_t *ret;
of_unichar_t *buffer;
ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
count: length];
[self getCharacters: ret
inRange: of_range(0, length)];
return ret;
buffer = of_malloc(length, sizeof(of_unichar_t));
@try {
[self getCharacters: buffer
inRange: of_range(0, length)];
} @catch (id e) {
free(buffer);
@throw e;
}
return [OFData dataWithItemsNoCopy: buffer
itemSize: sizeof(of_unichar_t)
count: length
freeWhenDone: true].items;
}
- (const of_char16_t *)UTF16String
{
return [self UTF16StringWithByteOrder: OF_BYTE_ORDER_NATIVE];
}
- (const of_char16_t *)UTF16StringWithByteOrder: (of_byte_order_t)byteOrder
{
OFObject *object = [[[OFObject alloc] init] autorelease];
void *pool = objc_autoreleasePoolPush();
const of_unichar_t *characters = self.characters;
size_t length = self.length;
of_char16_t *ret;
of_char16_t *buffer;
size_t j;
bool swap = (byteOrder != OF_BYTE_ORDER_NATIVE);
/* Allocate memory for the worst case */
ret = [object allocMemoryWithSize: sizeof(of_char16_t)
count: (length + 1) * 2];
buffer = of_malloc((length + 1) * 2, sizeof(of_char16_t));
j = 0;
for (size_t i = 0; i < length; i++) {
of_unichar_t c = characters[i];
if (c > 0x10FFFF)
if (c > 0x10FFFF) {
free(buffer);
@throw [OFInvalidEncodingException exception];
}
if (swap) {
if (c > 0xFFFF) {
c -= 0x10000;
ret[j++] = OF_BSWAP16(0xD800 | (c >> 10));
ret[j++] = OF_BSWAP16(0xDC00 | (c & 0x3FF));
buffer[j++] = OF_BSWAP16(0xD800 | (c >> 10));
buffer[j++] = OF_BSWAP16(0xDC00 | (c & 0x3FF));
} else
ret[j++] = OF_BSWAP16(c);
buffer[j++] = OF_BSWAP16(c);
} else {
if (c > 0xFFFF) {
c -= 0x10000;
ret[j++] = 0xD800 | (c >> 10);
ret[j++] = 0xDC00 | (c & 0x3FF);
buffer[j++] = 0xD800 | (c >> 10);
buffer[j++] = 0xDC00 | (c & 0x3FF);
} else
ret[j++] = c;
buffer[j++] = c;
}
}
ret[j] = 0;
buffer[j] = 0;
@try {
ret = [object resizeMemory: ret
size: sizeof(of_char16_t)
buffer = of_realloc(buffer, j + 1, sizeof(of_char16_t));
count: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
objc_autoreleasePoolPop(pool);
return ret;
return [OFData dataWithItemsNoCopy: buffer
itemSize: sizeof(of_char16_t)
count: j + 1
freeWhenDone: true].items;
}
- (size_t)UTF16StringLength
{
const of_unichar_t *characters = self.characters;
size_t length, UTF16StringLength;
|
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
|
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
|
-
-
+
-
+
-
-
-
-
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
|
- (const of_char32_t *)UTF32String
{
return [self UTF32StringWithByteOrder: OF_BYTE_ORDER_NATIVE];
}
- (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
{
OFObject *object = [[[OFObject alloc] init] autorelease];
size_t length = self.length;
of_char32_t *ret;
of_char32_t *buffer;
ret = [object allocMemoryWithSize: sizeof(of_char32_t)
buffer = of_malloc(length + 1, sizeof(of_char32_t));
count: length + 1];
[self getCharacters: ret
inRange: of_range(0, length)];
ret[length] = 0;
@try {
[self getCharacters: buffer
inRange: of_range(0, length)];
buffer[length] = 0;
} @catch (id e) {
free(buffer);
@throw e;
}
if (byteOrder != OF_BYTE_ORDER_NATIVE)
for (size_t i = 0; i < length; i++)
ret[i] = OF_BSWAP32(ret[i]);
buffer[i] = OF_BSWAP32(buffer[i]);
return ret;
return [OFData dataWithItemsNoCopy: buffer
itemSize: sizeof(of_char32_t)
count: length + 1
freeWhenDone: true].items;
}
- (OFData *)dataWithEncoding: (of_string_encoding_t)encoding
{
void *pool = objc_autoreleasePoolPush();
OFData *data =
[OFData dataWithItems: [self cStringWithEncoding: encoding]
|