diff --git a/Source/NSNumberFormatter.m b/Source/NSNumberFormatter.m index 710924d2c..74130b18f 100644 --- a/Source/NSNumberFormatter.m +++ b/Source/NSNumberFormatter.m @@ -59,6 +59,7 @@ #import "Foundation/NSCharacterSet.h" #import "GNUstepBase/GSLocale.h" +#import "GSPrivate.h" @class NSDoubleNumber; @@ -1407,55 +1408,57 @@ - (NSString *) stringFromNumber: (NSNumber *)number - (NSNumber *) numberFromString: (NSString *)string { + NSNumber *result = nil; // This is a 10.4 and above method and should not work with earlier version. #if GS_USE_ICU == 1 - NSNumber *result; - NSUInteger length; - NSRange range; - UErrorCode err = U_ZERO_ERROR; - unichar *ustring; - int64_t intNum; - double doubleNum; - - if (string == nil) - return nil; - - length = [string length]; - ustring = NSZoneMalloc ([self zone], sizeof(unichar) * length); - if (ustring == NULL) - return nil; + NSUInteger length = [string length]; + + if (length > 0) + { + NSRange range; + UErrorCode err = U_ZERO_ERROR; + int64_t intNum; + double doubleNum; + GS_BEGINITEMBUF(ustring, length * sizeof(unichar), unichar) - [string getCharacters: ustring range: NSMakeRange(0, length)]; + [string getCharacters: ustring range: NSMakeRange(0, length)]; - // FIXME: Not sure if this is correct.... - range = [string rangeOfString: @"."]; - if (range.location == NSNotFound) - { - intNum = unum_parseInt64(internal->_formatter, - ustring, length, NULL, &err); - if (U_FAILURE(err)) - return nil; - if (intNum == 0 || intNum == 1) - result = [NSNumber numberWithBool: (BOOL) intNum]; - else if (intNum < INT_MAX && intNum > INT_MIN) - result = [NSNumber numberWithInt: (int32_t)intNum]; + // FIXME: Not sure if this is correct.... + range = [string rangeOfString: @"."]; + if (range.location == NSNotFound) + { + intNum = unum_parseInt64(internal->_formatter, + ustring, length, NULL, &err); + if (!U_FAILURE(err)) + { + if (intNum == 0 || intNum == 1) + { + result = [NSNumber numberWithBool: (BOOL) intNum]; + } + else if (intNum < INT_MAX && intNum > INT_MIN) + { + result = [NSNumber numberWithInt: (int32_t)intNum]; + } + else + { + result = [NSNumber numberWithLongLong: intNum]; + } + } + } else - result = [NSNumber numberWithLongLong: intNum]; - } - else - { - doubleNum = unum_parseDouble(internal->_formatter, - ustring, length, NULL, &err); - if (U_FAILURE(err)) - return nil; - result = [NSNumber numberWithDouble: doubleNum]; + { + doubleNum = unum_parseDouble(internal->_formatter, + ustring, length, NULL, &err); + if (!U_FAILURE(err)) + { + result = [NSNumber numberWithDouble: doubleNum]; + } + } + + GS_ENDITEMBUF() } - - NSZoneFree ([self zone], ustring); - return result; -#else - return nil; #endif + return result; }