From 4d082e85fab882134bf526006f33f157c52c076a Mon Sep 17 00:00:00 2001 From: rfm Date: Wed, 20 Feb 2008 09:56:25 +0000 Subject: [PATCH] Change behavior of -boolValue to match the new MacOS-X method of the same name. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@26110 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 2 ++ Source/GSString.m | 78 +++++++++++------------------------------------ Source/NSString.m | 23 ++++++++------ 3 files changed, 34 insertions(+), 69 deletions(-) diff --git a/ChangeLog b/ChangeLog index cd0963d74..3e81cdef0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,8 @@ * Source/Additions/Unicode.m: add lockign for nl_langinfo * Source/Additions/GSMime.m: New method to parse headers * Headers/Additions/GNUstepBase/GSMime.h: ditto + * Source/NSString.m: Change ([-boolValue]) to match new MacOS-X method. + * Source/GSString.m: ditto 2008-02-19 Nicola Pero diff --git a/Source/GSString.m b/Source/GSString.m index 62cebecd5..69cdef3c5 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -938,75 +938,33 @@ UTF8String_u(GSStr self) static inline BOOL boolValue_c(GSStr self) { - if (self->_count == 0) - { - return NO; - } - else - { - unsigned len = self->_count < 10 ? self->_count : 9; + unsigned c = self->_count; + unsigned i; - if (len == 3 - && (self->_contents.c[0] == 'Y' || self->_contents.c[0] == 'y') - && (self->_contents.c[1] == 'E' || self->_contents.c[1] == 'e') - && (self->_contents.c[2] == 'S' || self->_contents.c[2] == 's')) - { - return YES; - } - else if (len == 4 - && (self->_contents.c[0] == 'T' || self->_contents.c[0] == 't') - && (self->_contents.c[1] == 'R' || self->_contents.c[1] == 'r') - && (self->_contents.c[2] == 'U' || self->_contents.c[2] == 'u') - && (self->_contents.c[3] == 'E' || self->_contents.c[3] == 'e')) - { - return YES; - } - else - { - unsigned char buf[len+1]; - - memcpy(buf, self->_contents.c, len); - buf[len] = '\0'; - return atoi((const char*)buf); - } + for (i = 0; i < c; i++) + { + if (strchr("123456789yYtT", self->_contents.c[i]) != 0) + { + return YES; + } } + return NO; } static inline BOOL boolValue_u(GSStr self) { - if (self->_count == 0) - { - return NO; - } - else - { - unsigned int l = self->_count < 10 ? self->_count : 9; - unsigned char buf[l+1]; - unsigned char *b = buf; + unsigned c = self->_count; + unsigned i; - GSFromUnicode(&b, &l, self->_contents.u, l, internalEncoding, - 0, GSUniTerminate); - if (l == 3 - && (buf[0] == 'Y' || buf[0] == 'y') - && (buf[1] == 'E' || buf[1] == 'e') - && (buf[2] == 'S' || buf[2] == 's')) - { - return YES; - } - else if (l == 4 - && (buf[0] == 'T' || buf[0] == 't') - && (buf[1] == 'R' || buf[1] == 'r') - && (buf[2] == 'U' || buf[2] == 'u') - && (buf[3] == 'E' || buf[3] == 'e')) - { - return YES; - } - else - { - return atoi((const char*)buf); - } + for (i = 0; i < c; i++) + { + if (strchr("123456789yYtT", self->_contents.u[i]) != 0) + { + return YES; + } } + return NO; } static inline BOOL diff --git a/Source/NSString.m b/Source/NSString.m index b25d55ed1..a42641691 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -2615,21 +2615,26 @@ handle_printf_atsign (FILE *stream, // xxx Should we use NSScanner here ? /** - * If the string consists of the words 'true' or 'yes' (case insensitive) - * or begins with a non-zero numeric value, return YES, otherwise return - * NO. + * Returns YES when scanning the receiver's text from left to right finds a + * digit in the range 1-9 or a letter in the set ('Y', 'y', 'T', 't').
+ * Any trailing characters are ignored.
+ * Any leading whitespace or zeros or signs are also ignored.
+ * Returns NO if the above conditions are not met. */ - (BOOL) boolValue { - if ([self caseInsensitiveCompare: @"YES"] == NSOrderedSame) + static NSCharacterSet *yes = nil; + + if (yes == nil) + { + yes = RETAIN([NSCharacterSet characterSetWithCharactersInString: + @"123456789yYtT"]); + } + if ([self rangeOfCharacterFromSet: yes].length > 0) { return YES; } - if ([self caseInsensitiveCompare: @"true"] == NSOrderedSame) - { - return YES; - } - return [self intValue] != 0 ? YES : NO; + return NO; } /**