Differences From Artifact [b10086fe78]:
- File
src/OFString.m
— part of check-in
[cba771824e]
at
2013-06-19 19:14:11
on branch trunk
— Implement the new MessagePack specification.
No support for extensions yet.
This obsoletes BinaryPack. (user: js, size: 56601) [annotate] [blame] [check-ins using]
To Artifact [dc3e1b10e9]:
- File
src/OFString.m
— part of check-in
[3d16a30f41]
at
2013-06-22 12:12:36
on branch trunk
— Rework exceptions.
This mostly removes the argument for the class in which the exception
occurred. As backtraces were recently added for all platforms, the
passed class does not give any extra information on where the exception
occurred anymore.This also removes a few other arguments which were not too helpful. In
the past, the idea was to pass as many arguments as possible so that it
is easier to find the origin of the exception. However, as backtraces
are a much better way to find the origin, those are not useful anymore
and just make the exception more cumbersome to use. The rule is now to
only pass arguments that might help in recovering from the exception or
provide information that is otherwise not easily accessible. (user: js, size: 55435) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
882 883 884 885 886 887 888 | @try { OFFile *file; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &st) == -1) @throw [OFOpenFileFailedException | | < | | < | 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 | @try { OFFile *file; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &st) == -1) @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"rb"]; if (st.st_size > SIZE_MAX) @throw [OFOutOfRangeException exception]; file = [[OFFile alloc] initWithPath: path mode: @"rb"]; @try { tmp = [self allocMemoryWithSize: (size_t)st.st_size]; |
︙ | ︙ | |||
956 957 958 959 960 961 962 | #ifdef OF_HAVE_SOCKETS client = [OFHTTPClient client]; request = [OFHTTPRequest requestWithURL: URL]; reply = [client performRequest: request]; if ([reply statusCode] != 200) @throw [OFHTTPRequestFailedException | < | | | 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 | #ifdef OF_HAVE_SOCKETS client = [OFHTTPClient client]; request = [OFHTTPRequest requestWithURL: URL]; reply = [client performRequest: request]; if ([reply statusCode] != 200) @throw [OFHTTPRequestFailedException exceptionWithRequest: request reply: reply]; headers = [reply headers]; if (encoding == OF_STRING_ENCODING_AUTODETECT && (contentType = [headers objectForKey: @"Content-Type"]) != nil) { contentType = [contentType lowercaseString]; |
︙ | ︙ | |||
983 984 985 986 987 988 989 | if (encoding == OF_STRING_ENCODING_AUTODETECT) encoding = OF_STRING_ENCODING_UTF_8; data = [reply readDataArrayTillEndOfStream]; if ((contentLength = [headers objectForKey: @"Content-Length"]) != nil) if ([data count] != (size_t)[contentLength decimalValue]) | | < | | < < | < < | < < | 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 | if (encoding == OF_STRING_ENCODING_AUTODETECT) encoding = OF_STRING_ENCODING_UTF_8; data = [reply readDataArrayTillEndOfStream]; if ((contentLength = [headers objectForKey: @"Content-Length"]) != nil) if ([data count] != (size_t)[contentLength decimalValue]) @throw [OFTruncatedDataException exception]; self = [[c alloc] initWithCString: (char*)[data items] encoding: encoding length: [data count]]; #else @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; #endif objc_autoreleasePoolPop(pool); return self; } - initWithSerialization: (OFXMLElement*)element { @try { void *pool = objc_autoreleasePoolPush(); if (![[element namespace] isEqual: OF_SERIALIZATION_NS]) @throw [OFInvalidArgumentException exception]; if ([self isKindOfClass: [OFMutableString class]]) { if (![[element name] isEqual: @"OFMutableString"]) @throw [OFInvalidArgumentException exception]; } else { if (![[element name] isEqual: @"OFString"]) @throw [OFInvalidArgumentException exception]; } self = [self initWithString: [element stringValue]]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; |
︙ | ︙ | |||
1052 1053 1054 1055 1056 1057 1058 | buffer); /* * Check for one more than the current index, as we * need one for the terminating zero. */ if (j + len >= maxLength) | | < | < | < | < | < | < | < | < | 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 | buffer); /* * Check for one more than the current index, as we * need one for the terminating zero. */ if (j + len >= maxLength) @throw [OFOutOfRangeException exception]; switch (len) { case 1: cString[j++] = buffer[0]; break; case 2: case 3: case 4: memcpy(cString + j, buffer, len); j += len; break; default: @throw [OFInvalidEncodingException exception]; } } cString[j] = '\0'; return j; case OF_STRING_ENCODING_ASCII: if (length + 1 > maxLength) @throw [OFOutOfRangeException exception]; for (i = 0; i < length; i++) { if OF_UNLIKELY (characters[i] > 0x80) @throw [OFInvalidEncodingException exception]; cString[i] = (char)characters[i]; } cString[i] = '\0'; return length; case OF_STRING_ENCODING_ISO_8859_1: if (length + 1 > maxLength) @throw [OFOutOfRangeException exception]; for (i = 0; i < length; i++) { if OF_UNLIKELY (characters[i] > 0xFF) @throw [OFInvalidEncodingException exception]; cString[i] = (uint8_t)characters[i]; } cString[i] = '\0'; return length; case OF_STRING_ENCODING_ISO_8859_15: if (length + 1 > maxLength) @throw [OFOutOfRangeException exception]; for (i = 0; i < length; i++) { of_unichar_t c = characters[i]; switch (c) { case 0xA4: case 0xA6: case 0xA8: case 0xB4: case 0xB8: case 0xBC: case 0xBD: case 0xBE: @throw [OFInvalidEncodingException exception]; } if OF_UNLIKELY (c > 0xFF) { switch (c) { case 0x20AC: cString[i] = 0xA4; break; |
︙ | ︙ | |||
1157 1158 1159 1160 1161 1162 1163 | cString[i] = 0xBD; break; case 0x178: cString[i] = 0xBE; break; default: @throw [OFInvalidEncodingException | | | < | < | 1139 1140 1141 1142 1143 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 | cString[i] = 0xBD; break; case 0x178: cString[i] = 0xBE; break; default: @throw [OFInvalidEncodingException exception]; } } else cString[i] = (uint8_t)c; } cString[i] = '\0'; return length; case OF_STRING_ENCODING_WINDOWS_1252: if (length + 1 > maxLength) @throw [OFOutOfRangeException exception]; for (i = 0; i < length; i++) { of_unichar_t c = characters[i]; if OF_UNLIKELY (c >= 0x80 && c <= 0x9F) @throw [OFInvalidEncodingException exception]; if OF_UNLIKELY (c > 0xFF) { switch (c) { case 0x20AC: cString[i] = 0x80; break; case 0x201A: |
︙ | ︙ | |||
1263 1264 1265 1266 1267 1268 1269 | cString[i] = 0x9E; break; case 0x178: cString[i] = 0x9F; break; default: @throw [OFInvalidEncodingException | | | < | | 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 | cString[i] = 0x9E; break; case 0x178: cString[i] = 0x9F; break; default: @throw [OFInvalidEncodingException exception]; } } else cString[i] = (uint8_t)c; } cString[i] = '\0'; return length; default: @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; } } - (const char*)cStringWithEncoding: (of_string_encoding_t)encoding { OFObject *object = [[[OFObject alloc] init] autorelease]; size_t length = [self length]; |
︙ | ︙ | |||
1315 1316 1317 1318 1319 1320 1321 | [self getCString: cString maxLength: length + 1 encoding: encoding]; break; default: | | < | | 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 | [self getCString: cString maxLength: length + 1 encoding: encoding]; break; default: @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; } return cString; } - (const char*)UTF8String { |
︙ | ︙ | |||
1350 1351 1352 1353 1354 1355 1356 | for (i = 0; i < length; i++) { char buffer[4]; size_t len = of_string_utf8_encode(characters[i], buffer); if (len == 0) | | < | < | | 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 | for (i = 0; i < length; i++) { char buffer[4]; size_t len = of_string_utf8_encode(characters[i], buffer); if (len == 0) @throw [OFInvalidEncodingException exception]; UTF8StringLength += len; } return UTF8StringLength; case OF_STRING_ENCODING_ASCII: case OF_STRING_ENCODING_ISO_8859_1: case OF_STRING_ENCODING_ISO_8859_15: case OF_STRING_ENCODING_WINDOWS_1252: return [self length]; default: @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; } } - (size_t)UTF8StringLength { return [self cStringLengthWithEncoding: OF_STRING_ENCODING_UTF_8]; } |
︙ | ︙ | |||
1445 1446 1447 1448 1449 1450 1451 | const of_unichar_t *characters, *otherCharacters; size_t i, minimumLength; if (object == self) return OF_ORDERED_SAME; if (![object isKindOfClass: [OFString class]]) | | < < | 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 | const of_unichar_t *characters, *otherCharacters; size_t i, minimumLength; if (object == self) return OF_ORDERED_SAME; if (![object isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException exception]; otherString = (OFString*)object; minimumLength = ([self length] > [otherString length] ? [otherString length] : [self length]); pool = objc_autoreleasePoolPush(); |
︙ | ︙ | |||
1656 1657 1658 1659 1660 1661 1662 | data = [OFDataArray dataArrayWithItemSize: 1 capacity: length + 5]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else | | | 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 | data = [OFDataArray dataArrayWithItemSize: 1 capacity: length + 5]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else @throw [OFOutOfRangeException exception]; [data addItems: [self UTF8String] count: length]; return data; } |
︙ | ︙ | |||
1695 1696 1697 1698 1699 1700 1701 | if ((searchLength = [string length]) == 0) return of_range(0, 0); if (searchLength > range.length) return of_range(OF_NOT_FOUND, 0); if (range.length > SIZE_MAX / sizeof(of_unichar_t)) | | | < | | 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 | if ((searchLength = [string length]) == 0) return of_range(0, 0); if (searchLength > range.length) return of_range(OF_NOT_FOUND, 0); if (range.length > SIZE_MAX / sizeof(of_unichar_t)) @throw [OFOutOfRangeException exception]; pool = objc_autoreleasePoolPush(); searchCharacters = [string characters]; characters = malloc(range.length * sizeof(of_unichar_t)); if (characters == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: range.length * sizeof(of_unichar_t)]; @try { [self getCharacters: characters inRange: range]; if (options & OF_STRING_SEARCH_BACKWARDS) { for (i = range.length - searchLength;; i--) { |
︙ | ︙ | |||
1780 1781 1782 1783 1784 1785 1786 | - (OFString*)substringWithRange: (of_range_t)range { void *pool; OFString *ret; if (range.length > SIZE_MAX - range.location || range.location + range.length > [self length]) | | < | 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 | - (OFString*)substringWithRange: (of_range_t)range { void *pool; OFString *ret; if (range.length > SIZE_MAX - range.location || range.location + range.length > [self length]) @throw [OFOutOfRangeException exception]; pool = objc_autoreleasePoolPush(); ret = [[OFString alloc] initWithCharacters: [self characters] + range.location length: range.length]; objc_autoreleasePoolPop(pool); |
︙ | ︙ | |||
2242 2243 2244 2245 2246 2247 2248 | i++; for (; i < length; i++) { if (expectWhitespace) { if (characters[i] != ' ' && characters[i] != '\t' && characters[i] != '\n' && characters[i] != '\r' && characters[i] != '\f') | | < | < | < | 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 | i++; for (; i < length; i++) { if (expectWhitespace) { if (characters[i] != ' ' && characters[i] != '\t' && characters[i] != '\n' && characters[i] != '\r' && characters[i] != '\f') @throw [OFInvalidFormatException exception]; continue; } if (characters[i] >= '0' && characters[i] <= '9') { if (INTMAX_MAX / 10 < value || INTMAX_MAX - value * 10 < characters[i] - '0') @throw [OFOutOfRangeException exception]; value = (value * 10) + (characters[i] - '0'); } else if (characters[i] == ' ' || characters[i] == '\t' || characters[i] == '\n' || characters[i] == '\r' || characters[i] == '\f') expectWhitespace = true; else @throw [OFInvalidFormatException exception]; } if (characters[0] == '-') value *= -1; objc_autoreleasePoolPop(pool); |
︙ | ︙ | |||
2304 2305 2306 2307 2308 2309 2310 | for (; i < length; i++) { uintmax_t newValue; if (expectWhitespace) { if (characters[i] != ' ' && characters[i] != '\t' && characters[i] != '\n' && characters[i] != '\r' && characters[i] != '\f') | | < | < | < | < | 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 | for (; i < length; i++) { uintmax_t newValue; if (expectWhitespace) { if (characters[i] != ' ' && characters[i] != '\t' && characters[i] != '\n' && characters[i] != '\r' && characters[i] != '\f') @throw [OFInvalidFormatException exception]; continue; } if (characters[i] >= '0' && characters[i] <= '9') { newValue = (value << 4) | (characters[i] - '0'); foundValue = true; } else if (characters[i] >= 'A' && characters[i] <= 'F') { newValue = (value << 4) | (characters[i] - 'A' + 10); foundValue = true; } else if (characters[i] >= 'a' && characters[i] <= 'f') { newValue = (value << 4) | (characters[i] - 'a' + 10); foundValue = true; } else if (characters[i] == 'h' || characters[i] == ' ' || characters[i] == '\t' || characters[i] == '\n' || characters[i] == '\r' || characters[i] == '\f') { expectWhitespace = true; continue; } else @throw [OFInvalidFormatException exception]; if (newValue < value) @throw [OFOutOfRangeException exception]; value = newValue; } if (!foundValue) @throw [OFInvalidFormatException exception]; objc_autoreleasePoolPop(pool); return value; } - (float)floatValue |
︙ | ︙ | |||
2362 2363 2364 2365 2366 2367 2368 | /* 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') | | < | 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 | /* 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 exception]; objc_autoreleasePoolPop(pool); return value; } - (double)doubleValue |
︙ | ︙ | |||
2389 2390 2391 2392 2393 2394 2395 | /* 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') | | < | 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 | /* 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 exception]; objc_autoreleasePoolPop(pool); return value; } - (const of_unichar_t*)characters |
︙ | ︙ | |||
2436 2437 2438 2439 2440 2441 2442 | j = 0; for (i = 0; i < length; i++) { of_unichar_t c = characters[i]; if (c > 0x10FFFF) | | < | 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 | j = 0; for (i = 0; i < length; i++) { of_unichar_t c = characters[i]; if (c > 0x10FFFF) @throw [OFInvalidEncodingException exception]; if (swap) { if (c > 0xFFFF) { c -= 0x10000; ret[j++] = OF_BSWAP16(0xD800 | (c >> 10)); ret[j++] = OF_BSWAP16(0xDC00 | (c & 0x3FF)); } else |
︙ | ︙ |