ObjFW  Diff

Differences From Artifact [b10086fe78]:

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
889

890
891

892
893
894

895
896
897
898
899
900
901
902
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
			    exceptionWithClass: [self class]
			    exceptionWithPath: path
					  path: path
					  mode: @"rb"];
					 mode: @"rb"];

		if (st.st_size > SIZE_MAX)
			@throw [OFOutOfRangeException
			@throw [OFOutOfRangeException exception];
			    exceptionWithClass: [self class]];

		file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];

		@try {
			tmp = [self allocMemoryWithSize: (size_t)st.st_size];

956
957
958
959
960
961
962
963
964
965


966
967
968
969
970
971
972
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
		    exceptionWithClass: [request class]
			       request: request
				 reply: reply];
		    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
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
1022

1023
1024
1025
1026
1027
1028
1029
1030
1031
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
			@throw [OFTruncatedDataException exception];
			    exceptionWithClass: [self class]];

	self = [[c alloc] initWithCString: (char*)[data items]
				 encoding: encoding
				   length: [data count]];
#else
	@throw [OFUnsupportedProtocolException exceptionWithClass: c];
	@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
			@throw [OFInvalidArgumentException exception];
			    exceptionWithClass: [self class]
				      selector: _cmd];

		if ([self isKindOfClass: [OFMutableString class]]) {
			if (![[element name] isEqual: @"OFMutableString"])
				@throw [OFInvalidArgumentException
				@throw [OFInvalidArgumentException exception];
				    exceptionWithClass: [self class]
					      selector: _cmd];
		} else {
			if (![[element name] isEqual: @"OFString"])
				@throw [OFInvalidArgumentException
				@throw [OFInvalidArgumentException exception];
				    exceptionWithClass: [self class]
					      selector: _cmd];
		}

		self = [self initWithString: [element stringValue]];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

1133
1134
1135
1136
1137
1138
1139
1140
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
				@throw [OFOutOfRangeException exception];
				    exceptionWithClass: [self class]];

			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
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];
			}
		}

		cString[j] = '\0';

		return j;
	case OF_STRING_ENCODING_ASCII:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			@throw [OFOutOfRangeException exception];
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (characters[i] > 0x80)
				@throw [OFInvalidEncodingException
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];

			cString[i] = (char)characters[i];
		}

		cString[i] = '\0';

		return length;
	case OF_STRING_ENCODING_ISO_8859_1:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			@throw [OFOutOfRangeException exception];
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (characters[i] > 0xFF)
				@throw [OFInvalidEncodingException
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];

			cString[i] = (uint8_t)characters[i];
		}

		cString[i] = '\0';

		return length;
	case OF_STRING_ENCODING_ISO_8859_15:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			@throw [OFOutOfRangeException exception];
			    exceptionWithClass: [self class]];

		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
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];
			}

			if OF_UNLIKELY (c > 0xFF) {
				switch (c) {
				case 0x20AC:
					cString[i] = 0xA4;
					break;
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
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
					    exceptionWithClass: [self class]];
					    exception];
				}
			} else
				cString[i] = (uint8_t)c;
		}

		cString[i] = '\0';

		return length;
	case OF_STRING_ENCODING_WINDOWS_1252:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			@throw [OFOutOfRangeException exception];
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			of_unichar_t c = characters[i];

			if OF_UNLIKELY (c >= 0x80 && c <= 0x9F)
				@throw [OFInvalidEncodingException
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];

			if OF_UNLIKELY (c > 0xFF) {
				switch (c) {
				case 0x20AC:
					cString[i] = 0x80;
					break;
				case 0x201A:
1263
1264
1265
1266
1267
1268
1269
1270

1271
1272
1273
1274
1275
1276
1277
1278
1279
1280

1281
1282

1283
1284
1285
1286
1287
1288
1289
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
					    exceptionWithClass: [self class]];
					    exception];
				}
			} else
				cString[i] = (uint8_t)c;
		}

		cString[i] = '\0';

		return length;
	default:
		@throw [OFNotImplementedException
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
		    exceptionWithClass: [self class]
			      selector: _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
1322

1323
1324

1325
1326
1327
1328
1329
1330
1331
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
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
		    exceptionWithClass: [self class]
			      selector: _cmd];
								 object: self];
	}

	return cString;
}

- (const char*)UTF8String
{
1350
1351
1352
1353
1354
1355
1356
1357

1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370

1371
1372

1373
1374
1375
1376
1377
1378
1379
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
				@throw [OFInvalidEncodingException exception];
				    exceptionWithClass: [self class]];

			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
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
		    exceptionWithClass: [self class]
			      selector: _cmd];
								 object: self];
	}
}

- (size_t)UTF8StringLength
{
	return [self cStringLengthWithEncoding: OF_STRING_ENCODING_UTF_8];
}
1445
1446
1447
1448
1449
1450
1451
1452

1453
1454
1455
1456
1457
1458
1459
1460
1461
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
		@throw [OFInvalidArgumentException exception];
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = (OFString*)object;
	minimumLength = ([self length] > [otherString length]
	    ? [otherString length] : [self length]);

	pool = objc_autoreleasePoolPush();

1656
1657
1658
1659
1660
1661
1662
1663

1664
1665
1666
1667
1668
1669
1670
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 exceptionWithClass: [self class]];
		@throw [OFOutOfRangeException exception];

	[data addItems: [self UTF8String]
		 count: length];

	return data;
}

1695
1696
1697
1698
1699
1700
1701
1702

1703
1704
1705
1706
1707
1708
1709
1710

1711
1712

1713
1714
1715
1716
1717
1718
1719
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 exceptionWithClass: [self class]];
		@throw [OFOutOfRangeException exception];

	pool = objc_autoreleasePoolPush();

	searchCharacters = [string characters];
	characters = malloc(range.length * sizeof(of_unichar_t));

	if (characters == NULL)
		@throw [OFOutOfMemoryException
		@throw [OFOutOfMemoryException exceptionWithRequestedSize:
		    exceptionWithClass: [self class]
			 requestedSize: range.length * sizeof(of_unichar_t)];
		    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
1787

1788
1789
1790
1791
1792
1793
1794
1795
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
		@throw [OFOutOfRangeException exception];
		    exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();
	ret = [[OFString alloc]
	    initWithCharacters: [self characters] + range.location
			length: range.length];
	objc_autoreleasePoolPop(pool);

2242
2243
2244
2245
2246
2247
2248
2249

2250
2251
2252
2253
2254
2255
2256
2257

2258
2259
2260
2261
2262
2263
2264
2265
2266

2267
2268
2269
2270
2271
2272
2273
2274
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
				@throw [OFInvalidFormatException exception];
				    exceptionWithClass: [self class]];
			continue;
		}

		if (characters[i] >= '0' && characters[i] <= '9') {
			if (INTMAX_MAX / 10 < value ||
			    INTMAX_MAX - value * 10 < characters[i] - '0')
				@throw [OFOutOfRangeException
				@throw [OFOutOfRangeException exception];
				    exceptionWithClass: [self class]];

			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
			@throw [OFInvalidFormatException exception];
			    exceptionWithClass: [self class]];
	}

	if (characters[0] == '-')
		value *= -1;

	objc_autoreleasePoolPop(pool);

2304
2305
2306
2307
2308
2309
2310
2311

2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331

2332
2333
2334
2335

2336
2337
2338
2339
2340
2341
2342

2343
2344
2345
2346
2347
2348
2349
2350
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
				@throw [OFInvalidFormatException exception];
				    exceptionWithClass: [self class]];
			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
			@throw [OFInvalidFormatException exception];
			    exceptionWithClass: [self class]];

		if (newValue < value)
			@throw [OFOutOfRangeException
			@throw [OFOutOfRangeException exception];
			    exceptionWithClass: [self class]];

		value = newValue;
	}

	if (!foundValue)
		@throw [OFInvalidFormatException
		@throw [OFInvalidFormatException exception];
		    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (float)floatValue
2362
2363
2364
2365
2366
2367
2368
2369

2370
2371
2372
2373
2374
2375
2376
2377
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
				@throw [OFInvalidFormatException exception];
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (double)doubleValue
2389
2390
2391
2392
2393
2394
2395
2396

2397
2398
2399
2400
2401
2402
2403
2404
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
				@throw [OFInvalidFormatException exception];
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (const of_unichar_t*)characters
2436
2437
2438
2439
2440
2441
2442
2443

2444
2445
2446
2447
2448
2449
2450
2451
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
			@throw [OFInvalidEncodingException exception];
			    exceptionWithClass: [self class]];

		if (swap) {
			if (c > 0xFFFF) {
				c -= 0x10000;
				ret[j++] = OF_BSWAP16(0xD800 | (c >> 10));
				ret[j++] = OF_BSWAP16(0xDC00 | (c & 0x3FF));
			} else