ObjFW  Check-in [fe5b5de3b6]

Overview
Comment:Workaround for libnix strtod not parsing INFINITY
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fe5b5de3b6d013d04c0ec3dabb70132ebc12260dff92fbd93923704c9cce7df9
User & Date: js on 2017-06-03 16:17:45
Other Links: manifest | tags
Context
2017-06-03
16:19
configure: Force-enable files on MorphOS + libnix check-in: a497302c08 user: js tags: trunk
16:17
Workaround for libnix strtod not parsing INFINITY check-in: fe5b5de3b6 user: js tags: trunk
15:57
-[OFApplication environment] for MorphOS + libnix check-in: 6d0a185364 user: js tags: trunk
Changes

Modified src/OFString.m from [094992d7f8] to [41447adc66].

13
14
15
16
17
18
19

20
21
22
23
24
25
26
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <errno.h>

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#if defined(HAVE_STRTOF_L) || defined(HAVE_STRTOD_L)
# include <locale.h>
#endif







>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.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)
# include <locale.h>
#endif
2515
2516
2517
2518
2519
2520
2521












2522
2523
2524
2525
2526
2527
2528

	return value;
}

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












#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.
	 */







>
>
>
>
>
>
>
>
>
>
>
>







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

	return value;
}

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

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
	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.
	 */
2556
2557
2558
2559
2560
2561
2562












2563
2564
2565
2566
2567
2568
2569

	return value;
}

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












#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.
	 */







>
>
>
>
>
>
>
>
>
>
>
>







2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594

	return value;
}

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

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
	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.
	 */

Modified tests/OFStringTests.m from [0bb9c53c1a] to [f7406dd54d].

651
652
653
654
655
656
657

658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

677
678
679
680
681
682
683
684

	/*
	 * These test numbers can be generated without rounding if we have IEEE
	 * floating point numbers, thus we can use == on them.
	 */
	TEST(@"-[floatValue]",
	    [C(@"\t-0.25 ") floatValue] == -0.25 &&

	    [C(@"\r-INFINITY\n") floatValue] == -INFINITY &&
	    isnan([C(@"   NAN\t\t") floatValue]))

#if !defined(OF_ANDROID) && !defined(OF_SOLARIS) && !defined(OF_DJGPP)
# define INPUT @"\t-0x1.FFFFFFFFFFFFFP-1020 "
# define EXPECTED -0x1.FFFFFFFFFFFFFP-1020
#else
/* Android, Solaris and DJGPP do not accept 0x for strtod() */
# if !defined(OF_SOLARIS) || !defined(OF_X86)
#  define INPUT @"\t-0.123456789 "
#  define EXPECTED -0.123456789
# else
/* Solaris' strtod() has weird rounding on x86, but not on x86_64 */
#  define INPUT @"\t-0.125 "
#  define EXPECTED -0.125
# endif
#endif
	TEST(@"-[doubleValue]",
	    [INPUT doubleValue] == EXPECTED &&

	    [C(@"\r-INFINITY\n") doubleValue] == -INFINITY &&
	    isnan([C(@"   NAN\t\t") doubleValue]))
#undef INPUT
#undef EXPECTED

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







>
|


















>
|







651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

	/*
	 * These test numbers can be generated without rounding if we have IEEE
	 * floating point numbers, thus we can use == on them.
	 */
	TEST(@"-[floatValue]",
	    [C(@"\t-0.25 ") floatValue] == -0.25 &&
	    [C(@"\r\n\tINF\t\n") doubleValue] == INFINITY &&
	    [C(@"\r -INFINITY\n") floatValue] == -INFINITY &&
	    isnan([C(@"   NAN\t\t") floatValue]))

#if !defined(OF_ANDROID) && !defined(OF_SOLARIS) && !defined(OF_DJGPP)
# define INPUT @"\t-0x1.FFFFFFFFFFFFFP-1020 "
# define EXPECTED -0x1.FFFFFFFFFFFFFP-1020
#else
/* Android, Solaris and DJGPP do not accept 0x for strtod() */
# if !defined(OF_SOLARIS) || !defined(OF_X86)
#  define INPUT @"\t-0.123456789 "
#  define EXPECTED -0.123456789
# else
/* Solaris' strtod() has weird rounding on x86, but not on x86_64 */
#  define INPUT @"\t-0.125 "
#  define EXPECTED -0.125
# endif
#endif
	TEST(@"-[doubleValue]",
	    [INPUT doubleValue] == EXPECTED &&
	    [C(@"\r\n\tINF\t\n") doubleValue] == INFINITY &&
	    [C(@"\r -INFINITY\n") doubleValue] == -INFINITY &&
	    isnan([C(@"   NAN\t\t") doubleValue]))
#undef INPUT
#undef EXPECTED

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