ObjFW  Check-in [e0a8e01190]

Overview
Comment:Ignore leading and trailing whitespaces in -[(hexa)decimalValue].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e0a8e011904884e8a5119687d640ebb37ef3242d952cb11d121641a3e02ce4c7
User & Date: js on 2011-03-29 18:20:23
Other Links: manifest | tags
Context
2011-03-29
19:03
Add -[floatValue] and -[doubleValue]. check-in: 9c59bc9aee user: js tags: trunk
18:20
Ignore leading and trailing whitespaces in -[(hexa)decimalValue]. check-in: e0a8e01190 user: js tags: trunk
17:51
Add +[requestWithURL:] to OFHTTPRequest. check-in: bfcd3875fe user: js tags: trunk
Changes

Modified src/OFString.h from [6689e2f31d] to [5f4a684d91].

434
435
436
437
438
439
440
441





442
443
444
445
446
447
448
449






450
451
452
453
454
455
456
434
435
436
437
438
439
440

441
442
443
444
445
446
447
448
449
450
451


452
453
454
455
456
457
458
459
460
461
462
463
464







-
+
+
+
+
+






-
-
+
+
+
+
+
+







/**
 * \return The directory name of the path
 */
- (OFString*)stringByDeletingLastPathComponent;

/**
 * Returns the decimal value of the string as an intmax_t or throws an
 * OFInvalidEncoding exception if the string contains any non-number characters.
 * OFInvalidEncodingException if the string contains any non-number characters.
 * Leading and trailing whitespaces are ignored.
 *
 * If the number is too big to fit into an intmax_t, an OFOutOfRangeException
 * is thrown.
 *
 * \return An intmax_t with the value of the string
 */
- (intmax_t)decimalValue;

/**
 * Returns the hexadecimal value of the string as an intmax_t or throws an
 * OFInvalidEncoding exception if the string contains any non-number characters.
 * Returns the hexadecimal value of the string as an uintmax_t or throws an
 * OFInvalidEncodingException if the string contains any non-number characters.
 * Leading and trailing whitespaces are ignored.
 *
 * If the number is too big to fit into an uintmax_t, an OFOutOfRangeException
 * is thrown.
 *
 * \return A uintmax_t with the value of the string
 */
- (uintmax_t)hexadecimalValue;

/**
 * Returns the string as an array of of_unichar_t. The result needs to be

Modified src/OFString.m from [52284dc401] to [2defafe592].

1246
1247
1248
1249
1250
1251
1252


1253
1254

1255





1256

1257
1258
1259
1260










1261
1262

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
1290
1291
1292
1293
1294

1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307




















1308
1309
1310
1311
1312
1313
1314
1315
1316
1317



1318
1319
1320
1321
1322
1323
1324
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
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

1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302



1303
1304
1305
1306
1307
1308
1309
1310
1311

1312
1313

1314
1315
1316




1317
1318
1319











1320
1321
1322
1323
1324
1325
1326
1327
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
1356
1357
1358
1359







+
+


+

+
+
+
+
+
-
+


-
-
+
+
+
+
+
+
+
+
+
+

-
+



-
-
+
+
+
+
+



-
+







+
+


+
-
-
-
+
+
+
+
+
+
+


-
+

-
+


-
-
-
-
+


-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+










+
+
+







					    length: 1];

	return @".";
}

- (intmax_t)decimalValue
{
	const char *str = string;
	size_t len = length;
	int i = 0;
	intmax_t num = 0;
	BOOL expectWhitespace = NO;

	while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') {
		str++;
		len--;
	}

	if (string[0] == '-' || string[0] == '+')
	if (str[0] == '-' || str[0] == '+')
		i++;

	for (; i < length; i++) {
		if (string[i] >= '0' && string[i] <= '9') {
	for (; i < len; i++) {
		if (expectWhitespace) {
			if (str[i] != ' ' && str[i] != '\t' &&
			    str[i] != '\n' && str[i] != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
			continue;
		}

		if (str[i] >= '0' && str[i] <= '9') {
			if (INTMAX_MAX / 10 < num ||
			    INTMAX_MAX - num * 10 < string[i] - '0')
			    INTMAX_MAX - num * 10 < str[i] - '0')
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			num = (num * 10) + (string[i] - '0');
		} else
			num = (num * 10) + (str[i] - '0');
		} else if (str[i] == ' ' || str[i] == '\t' ||
		    str[i] == '\n' || str[i] == '\r')
			expectWhitespace = YES;
		else
			@throw [OFInvalidFormatException newWithClass: isa];
	}

	if (string[0] == '-')
	if (str[0] == '-')
		num *= -1;

	return num;
}

- (uintmax_t)hexadecimalValue
{
	const char *str = string;
	size_t len = length;
	int i = 0;
	uintmax_t num = 0;
	BOOL expectWhitespace = NO, gotNumber = NO;
	BOOL suffix = NO;

	if (length == 0)

	while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') {
		str++;
		len--;
	}

	if (len == 0)
		return 0;

	if (length >= 2 && string[0] == '0' && string[1] == 'x')
	if (len >= 2 && str[0] == '0' && str[1] == 'x')
		i = 2;
	else if (length >= 1 && (string[0] == 'x' || string[0] == '$'))
	else if (len >= 1 && (str[0] == 'x' || str[0] == '$'))
		i = 1;

	if (i == length)
		@throw [OFInvalidFormatException newWithClass: isa];

	for (; i < length; i++) {
	for (; i < len; i++) {
		uintmax_t newnum;

		if (suffix)
			@throw [OFInvalidFormatException newWithClass: isa];

		if (string[i] >= '0' && string[i] <= '9')
			newnum = (num << 4) | (string[i] - '0');
		else if (string[i] >= 'A' && string[i] <= 'F')
			newnum = (num << 4) | (string[i] - 'A' + 10);
		else if (string[i] >= 'a' && string[i] <= 'f')
			newnum = (num << 4) | (string[i] - 'a' + 10);
		else if (string[i] == 'h') {
			suffix = YES;
		if (expectWhitespace) {
			if (str[i] != ' ' && str[i] != '\t' &&
			    str[i] != '\n' && str[i] != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
			continue;
		}

		if (str[i] >= '0' && str[i] <= '9') {
			newnum = (num << 4) | (str[i] - '0');
			gotNumber = YES;
		} else if (str[i] >= 'A' && str[i] <= 'F') {
			newnum = (num << 4) | (str[i] - 'A' + 10);
			gotNumber = YES;
		} else if (str[i] >= 'a' && str[i] <= 'f') {
			newnum = (num << 4) | (str[i] - 'a' + 10);
			gotNumber = YES;
		} else if (str[i] == 'h' || str[i] == ' ' || str[i] == '\t' ||
		    str[i] == '\n' || str[i] == '\r') {
			expectWhitespace = YES;
			continue;
		} else
			@throw [OFInvalidFormatException newWithClass: isa];

		if (newnum < num)
			@throw [OFOutOfRangeException newWithClass: isa];

		num = newnum;
	}

	if (!gotNumber)
		@throw [OFInvalidFormatException newWithClass: isa];

	return num;
}

- (of_unichar_t*)unicodeString
{
	of_unichar_t *ret;
	size_t i, j, len;

Modified tests/OFStringTests.m from [2602b81521] to [65c18fe8c4].

279
280
281
282
283
284
285
286
287
288



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
279
280
281
282
283
284
285



286
287
288
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







-
-
-
+
+
+



-
-
+
+

-
-
+
+





+
+







+
+







	    isEqual: @"/tmp"] &&
	    [[@"foo/bar" stringByDeletingLastPathComponent] isEqual: @"foo"] &&
	    [[@"/" stringByDeletingLastPathComponent] isEqual: @"/"] &&
	    [[@"foo" stringByDeletingLastPathComponent] isEqual: @"."])

	TEST(@"-[decimalValue]",
	    [@"1234" decimalValue] == 1234 &&
	    [@"+123" decimalValue] == 123 &&
	    [@"-500" decimalValue] == -500 &&
	    [@"" decimalValue] == 0)
	    [@"\r\n+123  " decimalValue] == 123 &&
	    [@"-500\t" decimalValue] == -500 &&
	    [@"\t\t\r\n" decimalValue] == 0)

	TEST(@"-[hexadecimalValue]",
	    [@"123f" hexadecimalValue] == 0x123f &&
	    [@"0xABcd" hexadecimalValue] == 0xABCD &&
	    [@"xbCDE" hexadecimalValue] == 0xBCDE &&
	    [@"\t\n0xABcd\r" hexadecimalValue] == 0xABCD &&
	    [@"  xbCDE" hexadecimalValue] == 0xBCDE &&
	    [@"$CdEf" hexadecimalValue] == 0xCDEF &&
	    [@"Feh" hexadecimalValue] == 0xFE &&
	    [@"" hexadecimalValue] == 0)
	    [@"\rFeh " hexadecimalValue] == 0xFE &&
	    [@"\r\t" hexadecimalValue] == 0)

	EXPECT_EXCEPTION(@"Detect invalid characters in -[decimalValue] #1",
	    OFInvalidFormatException, [@"abc" decimalValue])
	EXPECT_EXCEPTION(@"Detect invalid characters in -[decimalValue] #2",
	    OFInvalidFormatException, [@"0a" decimalValue])
	EXPECT_EXCEPTION(@"Detect invalid characters in -[decimalValue] #3",
			 OFInvalidFormatException, [@"0 1" decimalValue])

	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #1",
	    OFInvalidFormatException, [@"0xABCDEFG" hexadecimalValue])
	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #2",
	    OFInvalidFormatException, [@"0x" hexadecimalValue])
	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #3",
	    OFInvalidFormatException, [@"$" hexadecimalValue])
	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #4",
	    OFInvalidFormatException, [@"$ " hexadecimalValue])

	EXPECT_EXCEPTION(@"Detect out of range in -[decimalValue]",
	    OFOutOfRangeException,
	    [@"12345678901234567890123456789012345678901234567890"
	     @"12345678901234567890123456789012345678901234567890"
	    decimalValue])