ObjFW  Diff

Differences From Artifact [e1b9c425c7]:

To Artifact [d974e2e6b6]:


13
14
15
16
17
18
19

20
21
22
23
24
25
26
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27







+







 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#if defined(HAVE_STRTOF_L) || defined(HAVE_STRTOD_L)
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
2351
2352





2353
2354

2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365

2366
2367
2368
2369
2370

2371
2372

2373
2374
2375
2376
2377



2378
2379

2380
2381
2382

2383
2384
2385
2386
2387

2388
2389
2390

2391
2392
2393
2394
2395
2396
2397

2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413

2414
2415
2416

2417
2418
2419


2420
2421
2422
2423
2424

2425
2426
2427


2428
2429
2430
2431
2432
2433
2434
2435

2436
2437
2438
2439
2440


2441
2442
2443
2444
2445

2446
2447
2448

2449
2450
2451
2452

2453
2454
2455

2456
2457
2458
2459
2460
2461
2462

2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
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
2565
2566

2567

2568
2569
2570
2571
2572
2573
2574
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
2351
2352


2353



2354





2355



2356



2357



2358
















2359



2360



2361
2362

2363
2364


2365



2366
2367








2368





2369
2370





2371



2372




2373



2374







2375






2376
2377
2378
2379
2380
2381
2382
2383
2384


2385
2386
2387
2388
2389
2390
2391
2392

2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408



2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421

2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432


2433
2434
2435
2436
2437
2438
2439
2440

2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456



2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469

2470
2471
2472
2473
2474
2475
2476
2477







-
+

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

-
-
-
+
+
+
-
-
-
-
+

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






-
+

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

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


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









-
-








-
















-
-
-






+
+
+



+
-
+










-
-








-
















-
-
-






+
+
+



+
-
+







	[array makeImmutable];

	objc_autoreleasePoolPop(pool);

	return array;
}

- (intmax_t)decimalValue
- (long long)longLongValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = self.characters;
	size_t i = 0, length = self.length;
	intmax_t value = 0;
	return [self longLongValueWithBase: 10];
	bool expectWhitespace = false;

}
	while (length > 0 && of_ascii_isspace(*characters)) {
		characters++;
		length--;
	}


- (long long)longLongValueWithBase: (int)base
{
	if (length == 0) {
		objc_autoreleasePoolPop(pool);
	void *pool = objc_autoreleasePoolPush();
		return 0;
	}

	const char *UTF8String = self.UTF8String;
	if (characters[0] == '-' || characters[0] == '+')
		i++;

	char *endPointer = NULL;
	for (; i < length; i++) {
		if (expectWhitespace) {
			if (of_ascii_isspace(characters[i]))
				continue;
	long long value;

			@throw [OFInvalidFormatException exception];
		}

	value = strtoll(UTF8String, &endPointer, base);

	if ((value == LLONG_MIN || value == LLONG_MAX) && errno == ERANGE)
		if (characters[i] >= '0' && characters[i] <= '9') {
			if (INTMAX_MAX / 10 < value ||
			    INTMAX_MAX - value * 10 < characters[i] - '0')
				@throw [OFOutOfRangeException exception];
		@throw [OFOutOfRangeException exception];

			value = (value * 10) + (characters[i] - '0');
		} else if (of_ascii_isspace(characters[i]))
			expectWhitespace = true;
	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			/* Use isspace since strtoll uses the same. */
			if (!isspace(*endPointer))
		else
			@throw [OFInvalidFormatException exception];
				@throw [OFInvalidFormatException exception];
	}

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

	objc_autoreleasePoolPop(pool);

	return value;
}

- (uintmax_t)hexadecimalValue
- (unsigned long long)unsignedLongLongValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = self.characters;
	size_t i = 0, length = self.length;
	uintmax_t value = 0;
	return [self unsignedLongLongValueWithBase: 10];
	bool expectWhitespace = false, foundValue = false;

}
	while (length > 0 && of_ascii_isspace(*characters)) {
		characters++;
		length--;
	}


- (unsigned long long)unsignedLongLongValueWithBase: (int)base
{
	if (length == 0) {
		objc_autoreleasePoolPop(pool);
	void *pool = objc_autoreleasePoolPush();
		return 0;
	}

	const char *UTF8String = self.UTF8String;
	if (length >= 2 && characters[0] == '0' && characters[1] == 'x')
		i = 2;
	else if (length >= 1 && (characters[0] == 'x' || characters[0] == '$'))
		i = 1;

	char *endPointer = NULL;
	for (; i < length; i++) {
		uintmax_t newValue;

	unsigned long long value;
		if (expectWhitespace) {
			if (of_ascii_isspace(characters[i]))
				continue;

			@throw [OFInvalidFormatException exception];
		}

	/* Use isspace since strtoull uses the same. */
		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' ||
		    of_ascii_isspace(characters[i])) {
			expectWhitespace = true;
			continue;
		} else
			@throw [OFInvalidFormatException exception];

	while (isspace(*UTF8String))
		if (newValue < value)
			@throw [OFOutOfRangeException exception];

		UTF8String++;
		value = newValue;
	}


	if (*UTF8String == '-')
	if (!foundValue)
		@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);

	value = strtoull(UTF8String, &endPointer, base);
	return value;
}


	if (value == ULLONG_MAX && errno == ERANGE)
- (uintmax_t)octalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = self.characters;
	size_t i = 0, length = self.length;
	uintmax_t value = 0;
	bool expectWhitespace = false;

		@throw [OFOutOfRangeException exception];
	while (length > 0 && of_ascii_isspace(*characters)) {
		characters++;
		length--;
	}


	/* Check if there are any invalid chars left */
	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (endPointer != NULL)
	for (; i < length; i++) {
		uintmax_t newValue;

		for (; *endPointer != '\0'; endPointer++)
		if (expectWhitespace) {
			if (of_ascii_isspace(characters[i]))
				continue;

			/* Use isspace since strtoull uses the same. */
			@throw [OFInvalidFormatException exception];
		}

			if (!isspace(*endPointer))
		if (characters[i] >= '0' && characters[i] <= '7')
			newValue = (value << 3) | (characters[i] - '0');
		else if (of_ascii_isspace(characters[i])) {
			expectWhitespace = true;
			continue;
		} else
			@throw [OFInvalidFormatException exception];
				@throw [OFInvalidFormatException exception];

		if (newValue < value)
			@throw [OFOutOfRangeException exception];

		value = newValue;
	}

	objc_autoreleasePoolPop(pool);

	return value;
}

- (float)floatValue
{
	void *pool = objc_autoreleasePoolPush();

#if defined(OF_AMIGAOS_M68K) || defined(OF_MORPHOS)
	OFString *stripped = self.stringByDeletingEnclosingWhitespaces;

	if ([stripped caseInsensitiveCompare: @"INF"] == OF_ORDERED_SAME ||
	    [stripped caseInsensitiveCompare: @"INFINITY"] == OF_ORDERED_SAME)
		return INFINITY;
	if ([stripped caseInsensitiveCompare: @"-INF"] == OF_ORDERED_SAME ||
	    [stripped caseInsensitiveCompare: @"-INFINITY"] == OF_ORDERED_SAME)
		return -INFINITY;
#endif

#ifdef HAVE_STRTOF_L
	const char *UTF8String = self.UTF8String;
#else
	/*
	 * If we have no strtof_l, we have no other choice but to replace "."
	 * with the locale's decimal point.
	 */
	OFString *decimalPoint = [OFLocale decimalPoint];
	const char *UTF8String = [self
	    stringByReplacingOccurrencesOfString: @"."
				      withString: decimalPoint].UTF8String;
#endif
	char *endPointer = NULL;
	float value;

	while (of_ascii_isspace(*UTF8String))
		UTF8String++;

#ifdef HAVE_STRTOF_L
	value = strtof_l(UTF8String, &endPointer, cLocale);
#else
	value = strtof(UTF8String, &endPointer);
#endif

	if (value == HUGE_VALF)
		@throw [OFOutOfRangeException exception];

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			/* Use isspace since strtof uses the same. */
			if (!of_ascii_isspace(*endPointer))
			if (!isspace(*endPointer))
				@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (double)doubleValue
{
	void *pool = objc_autoreleasePoolPush();

#if defined(OF_AMIGAOS_M68K) || defined(OF_MORPHOS)
	OFString *stripped = self.stringByDeletingEnclosingWhitespaces;

	if ([stripped caseInsensitiveCompare: @"INF"] == OF_ORDERED_SAME ||
	    [stripped caseInsensitiveCompare: @"INFINITY"] == OF_ORDERED_SAME)
		return INFINITY;
	if ([stripped caseInsensitiveCompare: @"-INF"] == OF_ORDERED_SAME ||
	    [stripped caseInsensitiveCompare: @"-INFINITY"] == OF_ORDERED_SAME)
		return -INFINITY;
#endif

#ifdef HAVE_STRTOD_L
	const char *UTF8String = self.UTF8String;
#else
	/*
	 * If we have no strtod_l, we have no other choice but to replace "."
	 * with the locale's decimal point.
	 */
	OFString *decimalPoint = [OFLocale decimalPoint];
	const char *UTF8String = [self
	    stringByReplacingOccurrencesOfString: @"."
				      withString: decimalPoint].UTF8String;
#endif
	char *endPointer = NULL;
	double value;

	while (of_ascii_isspace(*UTF8String))
		UTF8String++;

#ifdef HAVE_STRTOD_L
	value = strtod_l(UTF8String, &endPointer, cLocale);
#else
	value = strtod(UTF8String, &endPointer);
#endif

	if (value == HUGE_VAL)
		@throw [OFOutOfRangeException exception];

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			/* Use isspace since strtod uses the same. */
			if (!of_ascii_isspace(*endPointer))
			if (!isspace(*endPointer))
				@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);

	return value;
}