Differences From Artifact [5fcda1615b]:
- File
src/OFString.m
— part of check-in
[f173477bef]
at
2011-09-19 16:34:04
on branch trunk
— Rename -[allocMemoryForNItems:withSize:] and friends.
It is now -[allocMemoryForNItems:ofSize:]. (user: js, size: 43440) [annotate] [blame] [check-ins using]
To Artifact [93030ae4fe]:
- File
src/OFString.m
— part of check-in
[e1e7ffa903]
at
2011-09-22 23:25:42
on branch trunk
— Exceptions are now autoreleased.
This is safe as an "exception loop" can't happen, since if allocating
an exception fails, it throws an OFAllocFailedException which is
preallocated and can always be thrown.So, the worst case would be that an autorelease of an exception fails,
triggering an OFOutOfMemoryException for which there is no memory,
resulting in an OFAllocFailedException to be thrown. (user: js, size: 43700) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
494 495 496 497 498 499 500 | if (encoding == OF_STRING_ENCODING_UTF_8 || encoding == OF_STRING_ENCODING_ASCII) { switch (of_string_check_utf8(cString, cStringLength, &s->length)) { case 1: if (encoding == OF_STRING_ENCODING_ASCII) @throw [OFInvalidEncodingException | | | | 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | if (encoding == OF_STRING_ENCODING_UTF_8 || encoding == OF_STRING_ENCODING_ASCII) { switch (of_string_check_utf8(cString, cStringLength, &s->length)) { case 1: if (encoding == OF_STRING_ENCODING_ASCII) @throw [OFInvalidEncodingException exceptionWithClass: isa]; s->UTF8 = YES; break; case -1: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } memcpy(s->cString, cString, cStringLength); s->cString[cStringLength] = 0; return self; } |
︙ | ︙ | |||
528 529 530 531 532 533 534 | s->UTF8 = YES; bytes = of_string_unicode_to_utf8( (uint8_t)cString[i], buffer); if (bytes == 0) @throw [OFInvalidEncodingException | | | 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | s->UTF8 = YES; bytes = of_string_unicode_to_utf8( (uint8_t)cString[i], buffer); if (bytes == 0) @throw [OFInvalidEncodingException exceptionWithClass: isa]; s->cStringLength += bytes - 1; s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; memcpy(s->cString + j, buffer, bytes); |
︙ | ︙ | |||
552 553 554 555 556 557 558 | case OF_STRING_ENCODING_ISO_8859_15: table = of_iso_8859_15; break; case OF_STRING_ENCODING_WINDOWS_1252: table = of_windows_1252; break; default: | | > | | | 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 | case OF_STRING_ENCODING_ISO_8859_15: table = of_iso_8859_15; break; case OF_STRING_ENCODING_WINDOWS_1252: table = of_windows_1252; break; default: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } for (i = j = 0; i < cStringLength; i++) { char buffer[4]; of_unichar_t character; size_t characterBytes; if (!(cString[i] & 0x80)) { s->cString[j++] = cString[i]; continue; } character = table[(uint8_t)cString[i]]; if (character == 0xFFFD) @throw [OFInvalidEncodingException exceptionWithClass: isa]; s->UTF8 = YES; characterBytes = of_string_unicode_to_utf8(character, buffer); if (characterBytes == 0) @throw [OFInvalidEncodingException exceptionWithClass: isa]; s->cStringLength += characterBytes - 1; s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; memcpy(s->cString + j, buffer, characterBytes); j += characterBytes; |
︙ | ︙ | |||
708 709 710 711 712 713 714 | memcpy(s->cString + j, buffer, 4); j += 4; break; default: @throw [OFInvalidEncodingException | | < | 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 | memcpy(s->cString + j, buffer, 4); j += 4; break; default: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } } s->cString[j] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @throw e; } return self; |
︙ | ︙ | |||
788 789 790 791 792 793 794 | of_unichar_t character = (swap ? of_bswap16(string[i]) : string[i]); size_t characterLen; /* Missing high surrogate */ if ((character & 0xFC00) == 0xDC00) @throw [OFInvalidEncodingException | | | | 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | of_unichar_t character = (swap ? of_bswap16(string[i]) : string[i]); size_t characterLen; /* Missing high surrogate */ if ((character & 0xFC00) == 0xDC00) @throw [OFInvalidEncodingException exceptionWithClass: isa]; if ((character & 0xFC00) == 0xD800) { uint16_t nextCharacter; if (length <= i + 1) @throw [OFInvalidEncodingException exceptionWithClass: isa]; nextCharacter = (swap ? of_bswap16(string[i + 1]) : string[i + 1]); character = (((character & 0x3FF) << 10) | (nextCharacter & 0x3FF)) + 0x10000; |
︙ | ︙ | |||
841 842 843 844 845 846 847 | memcpy(s->cString + j, buffer, 4); j += 4; break; default: @throw [OFInvalidEncodingException | | < | 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | memcpy(s->cString + j, buffer, 4); j += 4; break; default: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } } s->cString[j] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @throw e; } return self; |
︙ | ︙ | |||
884 885 886 887 888 889 890 | { self = [super init]; @try { int cStringLength; if (format == nil) | | > | | > | > | 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 | { self = [super init]; @try { int cStringLength; if (format == nil) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); if ((cStringLength = of_vasprintf(&s->cString, [format UTF8String], arguments)) == -1) @throw [OFInvalidFormatException exceptionWithClass: isa]; s->cStringLength = cStringLength; @try { switch (of_string_check_utf8(s->cString, cStringLength, &s->length)) { case 1: s->UTF8 = YES; break; case -1: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } [self addMemoryToPool: s->cString]; } @catch (id e) { free(s->cString); @throw e; } } @catch (id e) { [self release]; @throw e; } return self; |
︙ | ︙ | |||
1011 1012 1013 1014 1015 1016 1017 | struct stat st; @try { OFFile *file; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &st) == -1) | | > | | | | 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 | struct stat st; @try { OFFile *file; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &st) == -1) @throw [OFOpenFileFailedException exceptionWithClass: isa path: path mode: @"rb"]; if (st.st_size > SIZE_MAX) @throw [OFOutOfRangeException exceptionWithClass: isa]; file = [[OFFile alloc] initWithPath: path mode: @"rb"]; @try { tmp = [self allocMemoryWithSize: (size_t)st.st_size]; |
︙ | ︙ | |||
1077 1078 1079 1080 1081 1082 1083 | } request = [OFHTTPRequest requestWithURL: URL]; result = [request perform]; if ([result statusCode] != 200) @throw [OFHTTPRequestFailedException | | | | | 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 | } request = [OFHTTPRequest requestWithURL: URL]; result = [request perform]; if ([result statusCode] != 200) @throw [OFHTTPRequestFailedException exceptionWithClass: [request class] HTTPRequest: request result: result]; if (encoding == OF_STRING_ENCODING_AUTODETECT && (contentType = [[result headers] objectForKey: @"Content-Type"])) { contentType = [[contentType mutableCopy] autorelease]; [contentType lower]; if ([contentType hasSuffix: @"charset=UTF-8"]) |
︙ | ︙ | |||
1114 1115 1116 1117 1118 1119 1120 | - initWithSerialization: (OFXMLElement*)element { @try { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; if (![[element name] isEqual: [self className]] || ![[element namespace] isEqual: OF_SERIALIZATION_NS]) | | > | | 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 | - initWithSerialization: (OFXMLElement*)element { @try { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; if (![[element name] isEqual: [self className]] || ![[element namespace] isEqual: OF_SERIALIZATION_NS]) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; self = [self initWithString: [element stringValue]]; [pool release]; } @catch (id e) { [self release]; @throw e; |
︙ | ︙ | |||
1140 1141 1142 1143 1144 1145 1146 | - (const char*)cStringWithEncoding: (of_string_encoding_t)encoding { switch (encoding) { case OF_STRING_ENCODING_UTF_8: return s->cString; case OF_STRING_ENCODING_ASCII: if (s->UTF8) | | > | | | > | | | 1144 1145 1146 1147 1148 1149 1150 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 | - (const char*)cStringWithEncoding: (of_string_encoding_t)encoding { switch (encoding) { case OF_STRING_ENCODING_UTF_8: return s->cString; case OF_STRING_ENCODING_ASCII: if (s->UTF8) @throw [OFInvalidEncodingException exceptionWithClass: isa]; return s->cString; default: @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } } - (size_t)length { return s->length; } - (size_t)UTF8StringLength { return s->cStringLength; } - (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding { switch (encoding) { case OF_STRING_ENCODING_UTF_8: return s->cStringLength; case OF_STRING_ENCODING_ASCII: if (s->UTF8) @throw [OFInvalidEncodingException exceptionWithClass: isa]; return s->cStringLength; default: @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } } - (BOOL)isEqual: (id)object { OFString *otherString; |
︙ | ︙ | |||
1211 1212 1213 1214 1215 1216 1217 | - (of_comparison_result_t)compare: (id)object { OFString *otherString; size_t otherCStringLength, minimumCStringLength; int compare; if (![object isKindOfClass: [OFString class]]) | | | | 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 | - (of_comparison_result_t)compare: (id)object { OFString *otherString; size_t otherCStringLength, minimumCStringLength; int compare; if (![object isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; otherString = object; otherCStringLength = [otherString UTF8StringLength]; minimumCStringLength = (s->cStringLength > otherCStringLength ? otherCStringLength : s->cStringLength); if ((compare = memcmp(s->cString, [otherString UTF8String], |
︙ | ︙ | |||
1241 1242 1243 1244 1245 1246 1247 | - (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString { const char *otherCString; size_t i, j, otherCStringLength, minimumCStringLength; int compare; if (![otherString isKindOfClass: [OFString class]]) | | | | 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 | - (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString { const char *otherCString; size_t i, j, otherCStringLength, minimumCStringLength; int compare; if (![otherString isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; otherCString = [otherString UTF8String]; otherCStringLength = [otherString UTF8StringLength]; if (!s->UTF8) { minimumCStringLength = (s->cStringLength > otherCStringLength ? otherCStringLength : s->cStringLength); |
︙ | ︙ | |||
1278 1279 1280 1281 1282 1283 1284 | l1 = of_string_utf8_to_unicode(s->cString + i, s->cStringLength - i, &c1); l2 = of_string_utf8_to_unicode(otherCString + j, otherCStringLength - j, &c2); if (l1 == 0 || l2 == 0 || c1 > 0x10FFFF || c2 > 0x10FFFF) | | > | 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 | l1 = of_string_utf8_to_unicode(s->cString + i, s->cStringLength - i, &c1); l2 = of_string_utf8_to_unicode(otherCString + j, otherCStringLength - j, &c2); if (l1 == 0 || l2 == 0 || c1 > 0x10FFFF || c2 > 0x10FFFF) @throw [OFInvalidEncodingException exceptionWithClass: isa]; if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) { of_unichar_t tc = of_unicode_casefolding_table[c1 >> 8][c1 & 0xFF]; if (tc) c1 = tc; |
︙ | ︙ | |||
1358 1359 1360 1361 1362 1363 1364 | } - (of_unichar_t)characterAtIndex: (size_t)index { of_unichar_t character; if (index >= s->length) | | | | 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 | } - (of_unichar_t)characterAtIndex: (size_t)index { of_unichar_t character; if (index >= s->length) @throw [OFOutOfRangeException exceptionWithClass: isa]; if (!s->UTF8) return s->cString[index]; index = of_string_index_to_position(s->cString, index, s->cStringLength); if (!of_string_utf8_to_unicode(s->cString + index, s->cStringLength - index, &character)) @throw [OFInvalidEncodingException exceptionWithClass: isa]; return character; } - (size_t)indexOfFirstOccurrenceOfString: (OFString*)string { const char *cString = [string UTF8String]; |
︙ | ︙ | |||
1437 1438 1439 1440 1441 1442 1443 | - (OFString*)substringWithRange: (of_range_t)range { size_t start = range.start; size_t end = range.start + range.length; if (end > s->length) | | | 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 | - (OFString*)substringWithRange: (of_range_t)range { size_t start = range.start; size_t end = range.start + range.length; if (end > s->length) @throw [OFOutOfRangeException exceptionWithClass: isa]; 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); } |
︙ | ︙ | |||
1743 1744 1745 1746 1747 1748 1749 | for (; i < cStringLength; i++) { if (expectWhitespace) { if (cString[i] != ' ' && cString[i] != '\t' && cString[i] != '\n' && cString[i] != '\r' && cString[i] != '\f') @throw [OFInvalidFormatException | | | | > | 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 | for (; i < cStringLength; i++) { if (expectWhitespace) { if (cString[i] != ' ' && cString[i] != '\t' && cString[i] != '\n' && cString[i] != '\r' && cString[i] != '\f') @throw [OFInvalidFormatException exceptionWithClass: isa]; continue; } if (cString[i] >= '0' && cString[i] <= '9') { if (INTMAX_MAX / 10 < value || INTMAX_MAX - value * 10 < cString[i] - '0') @throw [OFOutOfRangeException exceptionWithClass: isa]; value = (value * 10) + (cString[i] - '0'); } else if (cString[i] == ' ' || cString[i] == '\t' || cString[i] == '\n' || cString[i] == '\r' || cString[i] == '\f') expectWhitespace = YES; else @throw [OFInvalidFormatException exceptionWithClass: isa]; } if (cString[0] == '-') value *= -1; return value; } |
︙ | ︙ | |||
1798 1799 1800 1801 1802 1803 1804 | uintmax_t newValue; if (expectWhitespace) { if (cString[i] != ' ' && cString[i] != '\t' && cString[i] != '\n' && cString[i] != '\r' && cString[i] != '\f') @throw [OFInvalidFormatException | | | > | | | 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 | uintmax_t newValue; if (expectWhitespace) { if (cString[i] != ' ' && cString[i] != '\t' && cString[i] != '\n' && cString[i] != '\r' && cString[i] != '\f') @throw [OFInvalidFormatException exceptionWithClass: isa]; continue; } if (cString[i] >= '0' && cString[i] <= '9') { newValue = (value << 4) | (cString[i] - '0'); foundValue = YES; } else if (cString[i] >= 'A' && cString[i] <= 'F') { newValue = (value << 4) | (cString[i] - 'A' + 10); foundValue = YES; } else if (cString[i] >= 'a' && cString[i] <= 'f') { newValue = (value << 4) | (cString[i] - 'a' + 10); foundValue = YES; } else if (cString[i] == 'h' || cString[i] == ' ' || cString[i] == '\t' || cString[i] == '\n' || cString[i] == '\r' || cString[i] == '\f') { expectWhitespace = YES; continue; } else @throw [OFInvalidFormatException exceptionWithClass: isa]; if (newValue < value) @throw [OFOutOfRangeException exceptionWithClass: isa]; value = newValue; } if (!foundValue) @throw [OFInvalidFormatException exceptionWithClass: isa]; return value; } - (float)floatValue { const char *cString = s->cString; |
︙ | ︙ | |||
1850 1851 1852 1853 1854 1855 1856 | /* Check if there are any invalid chars left */ if (endPointer != NULL) for (; *endPointer != '\0'; endPointer++) if (*endPointer != ' ' && *endPointer != '\t' && *endPointer != '\n' && *endPointer != '\r' && *endPointer != '\f') @throw [OFInvalidFormatException | | | 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 | /* Check if there are any invalid chars left */ if (endPointer != NULL) for (; *endPointer != '\0'; endPointer++) if (*endPointer != ' ' && *endPointer != '\t' && *endPointer != '\n' && *endPointer != '\r' && *endPointer != '\f') @throw [OFInvalidFormatException exceptionWithClass: isa]; return value; } - (double)doubleValue { const char *cString = s->cString; |
︙ | ︙ | |||
1874 1875 1876 1877 1878 1879 1880 | /* Check if there are any invalid chars left */ if (endPointer != NULL) for (; *endPointer != '\0'; endPointer++) if (*endPointer != ' ' && *endPointer != '\t' && *endPointer != '\n' && *endPointer != '\r' && *endPointer != '\f') @throw [OFInvalidFormatException | | | 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 | /* Check if there are any invalid chars left */ if (endPointer != NULL) for (; *endPointer != '\0'; endPointer++) if (*endPointer != ' ' && *endPointer != '\t' && *endPointer != '\n' && *endPointer != '\r' && *endPointer != '\f') @throw [OFInvalidFormatException exceptionWithClass: isa]; return value; } - (of_unichar_t*)unicodeString { OFObject *object = [[[OFObject alloc] init] autorelease]; |
︙ | ︙ | |||
1901 1902 1903 1904 1905 1906 1907 | of_unichar_t c; size_t cLen; cLen = of_string_utf8_to_unicode(s->cString + i, s->cStringLength - i, &c); if (cLen == 0 || c > 0x10FFFF) | | > | 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 | of_unichar_t c; size_t cLen; cLen = of_string_utf8_to_unicode(s->cString + i, s->cStringLength - i, &c); if (cLen == 0 || c > 0x10FFFF) @throw [OFInvalidEncodingException exceptionWithClass: isa]; ret[j++] = c; i += cLen; } ret[j] = 0; |
︙ | ︙ |