ObjFW  Diff

Differences From Artifact [e1b9c425c7]:

To Artifact [a28b46c55c]:


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)
122
123
124
125
126
127
128
129

130
131
132
133

134
135
136
137
138
139
140
123
124
125
126
127
128
129

130
131
132
133

134
135
136
137
138
139
140
141







-
+



-
+







    size_t, bool);

/* References for static linking */
void
_references_to_categories_of_OFString(void)
{
	_OFString_CryptoHashing_reference = 1;
	_OFString_JSONValue_reference = 1;
	_OFString_JSONParsing_reference = 1;
#ifdef OF_HAVE_FILES
	_OFString_PathAdditions_reference = 1;
#endif
	_OFString_PropertyListValue_reference = 1;
	_OFString_PropertyListParsing_reference = 1;
	_OFString_Serialization_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;
}

void
831
832
833
834
835
836
837


838
839

840
841
842
843


844
845
846
847
848
849


850
851
852


853
854
855
856


857
858
859
860
861
862
863
832
833
834
835
836
837
838
839
840
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
866
867
868
869
870
871
872







+
+

-
+




+
+






+
+

-
-
+
+




+
+







			    encoding: OF_STRING_ENCODING_UTF_8
			      length: UTF8StringLength];
}

- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
			    freeWhenDone: (bool)freeWhenDone
{
	id ret;

	@try {
		return [self initWithUTF8String: UTF8String];
		ret = [self initWithUTF8String: UTF8String];
	} @finally {
		if (freeWhenDone)
			free(UTF8String);
	}

	return ret;
}

- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
				  length: (size_t)UTF8StringLength
			    freeWhenDone: (bool)freeWhenDone
{
	id ret;

	@try {
		return [self initWithUTF8String: UTF8String
					 length: UTF8StringLength];
		ret = [self initWithUTF8String: UTF8String
					length: UTF8StringLength];
	} @finally {
		if (freeWhenDone)
			free(UTF8String);
	}

	return ret;
}

- (instancetype)initWithCString: (const char *)cString
		       encoding: (of_string_encoding_t)encoding
{
	return [self initWithCString: cString
			    encoding: encoding
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
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







-
+

















-
+







				   encoding: OF_STRING_ENCODING_UTF_8];
}

- (instancetype)initWithContentsOfFile: (OFString *)path
			      encoding: (of_string_encoding_t)encoding
{
	char *tmp;
	uintmax_t fileSize;
	unsigned long long fileSize;

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFFile *file = nil;

		@try {
			fileSize = [[OFFileManager defaultManager]
			    attributesOfItemAtPath: path].fileSize;
		} @catch (OFRetrieveItemAttributesFailedException *e) {
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: @"r"
					errNo: e.errNo];
		}

		objc_autoreleasePoolPop(pool);

# if UINTMAX_MAX > SIZE_MAX
# if ULLONG_MAX > SIZE_MAX
		if (fileSize > SIZE_MAX)
			@throw [OFOutOfRangeException exception];
#endif

		/*
		 * We need one extra byte for the terminating zero if we want
		 * to use -[initWithUTF8StringNoCopy:length:freeWhenDone:].
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
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







-
+

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

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

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






-
+

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

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


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









-
-








-
















-
-
-
+





+
+
+




+
-
+










-
-








-
















-
-
-
+






+
+
+



+
-
+







	[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;
	}

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

	for (; i < length; i++) {
		if (expectWhitespace) {
			if (of_ascii_isspace(characters[i]))
				continue;

	errno = 0;
	value = strtoll(UTF8String, &endPointer, base);
			@throw [OFInvalidFormatException exception];
		}


	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((unsigned char)*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((unsigned char)*UTF8String))
		if (newValue < value)
			@throw [OFOutOfRangeException exception];

		UTF8String++;
		value = newValue;
	}


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

	objc_autoreleasePoolPop(pool);

	errno = 0;
	return value;
}

	value = strtoull(UTF8String, &endPointer, base);
- (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;
	if (value == ULLONG_MAX && errno == ERANGE)
	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((unsigned char)*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++;

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

	if (value == HUGE_VALF && errno == ERANGE)
		@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((unsigned char)*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++;

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

	if (value == HUGE_VAL && errno == ERANGE)
		@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((unsigned char)*endPointer))
				@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);

	return value;
}