Implmented a few more methods requiring ICU. Added code in -stringForObjectValue: to handle NSNumberFormatterBehavior10_4... need to test this code against OS X, I'm not sure I caught every case.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31885 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Stefan Bidigaray 2011-01-13 00:29:30 +00:00
parent 01b431530e
commit c03d1096a6
2 changed files with 450 additions and 260 deletions

View file

@ -1,3 +1,8 @@
2011-01-12 Stefan Bidigaray <stefanbidi@gmail.com>
* Source/NSNumberFormatter.m: Implemented more methods.
([-stringForObjectValue:]): Add code for NSNumberFormatterBehavior10_4.
2011-01-11 Stefan Bidigaray <stefanbidi@gmail.com> 2011-01-11 Stefan Bidigaray <stefanbidi@gmail.com>
* Source/NSLocale.m: Removed NSLog used for debugging. * Source/NSLocale.m: Removed NSLog used for debugging.

View file

@ -50,6 +50,7 @@
#define MAX_SYMBOL_SIZE 32 #define MAX_SYMBOL_SIZE 32
#define MAX_TEXT_ATTRIB_SIZE 512 #define MAX_TEXT_ATTRIB_SIZE 512
#define MAX_BUFFER_SIZE 1024
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
static inline UNumberFormatStyle static inline UNumberFormatStyle
@ -85,7 +86,7 @@ _NSToICUFormatStyle (NSNumberFormatterStyle style)
static inline UNumberFormatPadPosition static inline UNumberFormatPadPosition
_NSToICUPadPosition (NSNumberFormatterPadPosition position) _NSToICUPadPosition (NSNumberFormatterPadPosition position)
{ {
UNumberFormatPadPosition result; UNumberFormatPadPosition result = 0;
switch (position) switch (position)
{ {
@ -109,7 +110,7 @@ _NSToICUPadPosition (NSNumberFormatterPadPosition position)
static inline NSNumberFormatterPadPosition static inline NSNumberFormatterPadPosition
_ICUToNSPadPosition (UNumberFormatPadPosition position) _ICUToNSPadPosition (UNumberFormatPadPosition position)
{ {
NSNumberFormatterPadPosition result; NSNumberFormatterPadPosition result = 0;
switch (position) switch (position)
{ {
@ -613,7 +614,7 @@ static NSUInteger _defaultBehavior = 0;
{ {
if (_behavior == NSNumberFormatterBehavior10_4 if (_behavior == NSNumberFormatterBehavior10_4
#if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST) #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
|| NSNumberFormatterBehaviorDefault) || _behavior == NSNumberFormatterBehaviorDefault)
#endif #endif
{ {
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
@ -622,7 +623,7 @@ static NSUInteger _defaultBehavior = 0;
} }
else if (_behavior == NSNumberFormatterBehavior10_0 else if (_behavior == NSNumberFormatterBehavior10_0
#if OS_API_VERSION(GS_OPENSTEP_V, MAC_OS_X_VERSION_10_3) #if OS_API_VERSION(GS_OPENSTEP_V, MAC_OS_X_VERSION_10_3)
|| NSNumberFormatterBehaviorDefault) || _behavior == NSNumberFormatterBehaviorDefault)
#endif #endif
{ {
if ([newSeparator length] > 0) if ([newSeparator length] > 0)
@ -717,6 +718,56 @@ static NSUInteger _defaultBehavior = 0;
} }
- (NSString*) stringForObjectValue: (id)anObject - (NSString*) stringForObjectValue: (id)anObject
{
if (_behavior == NSNumberFormatterBehavior10_4
#if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
|| _behavior == NSNumberFormatterBehaviorDefault)
#endif
{
#if GS_USE_ICU == 1
#define STRING_FROM_NUMBER(function, number) do \
{ \
UChar *outStr = buffer; \
UErrorCode err = U_ZERO_ERROR; \
int32_t len; \
NSString *result; \
\
len = function (_formatter, number, outStr, MAX_BUFFER_SIZE, NULL, &err); \
if (len > MAX_BUFFER_SIZE) \
outStr = NSZoneMalloc ([self zone], len * sizeof(UChar));\
err = U_ZERO_ERROR; \
function (_formatter, number, outStr, MAX_BUFFER_SIZE, NULL, &err); \
result = [NSString stringWithCharacters: outStr length: len]; \
if (len > MAX_BUFFER_SIZE) \
NSZoneFree ([self zone], outStr); \
return result; \
} while (0)
UChar buffer[MAX_BUFFER_SIZE];
// FIXME: What to do with unsigned types?
if (nil == anObject)
return [self nilSymbol];
else if (![anObject isKindOfClass: [NSNumber class]])
return [self notANumberSymbol];
else if ([anObject objCType] == @encode(int))
STRING_FROM_NUMBER(unum_format, [anObject intValue]);
else if ([anObject objCType] == @encode(long long))
STRING_FROM_NUMBER(unum_formatInt64, [anObject longLongValue]);
else if ([anObject objCType] == @encode(BOOL))
STRING_FROM_NUMBER(unum_format, (int)[anObject boolValue]);
else if ([anObject objCType] == @encode(double))
STRING_FROM_NUMBER(unum_formatDouble, [anObject doubleValue]);
else if ([anObject objCType] == @encode(float))
STRING_FROM_NUMBER(unum_formatDouble, (double)[anObject floatValue]);
#endif
}
else if (_behavior == NSNumberFormatterBehavior10_0
#if OS_API_VERSION(GS_OPENSTEP_V, MAC_OS_X_VERSION_10_3)
|| _behavior == NSNumberFormatterBehaviorDefault)
#endif
// FIXME: Should this method even work for OS X < 10.4?
{ {
NSMutableDictionary *locale; NSMutableDictionary *locale;
NSCharacterSet *formattingCharacters; NSCharacterSet *formattingCharacters;
@ -847,7 +898,8 @@ static NSUInteger _defaultBehavior = 0;
representativeDecimal = [anObject decimalValue]; representativeDecimal = [anObject decimalValue];
NSDecimalRound(&roundedDecimal, &representativeDecimal, decimalPlaces, NSDecimalRound(&roundedDecimal, &representativeDecimal, decimalPlaces,
NSRoundPlain); NSRoundPlain);
roundedNumber = [NSDecimalNumber decimalNumberWithDecimal: roundedDecimal]; roundedNumber =
[NSDecimalNumber decimalNumberWithDecimal: roundedDecimal];
/* Arguably this fiddling could be done by GSDecimalString() but I /* Arguably this fiddling could be done by GSDecimalString() but I
* thought better to leave that behaviour as it is and provide the * thought better to leave that behaviour as it is and provide the
@ -889,7 +941,8 @@ static NSUInteger _defaultBehavior = 0;
{ {
NSRange ipRange; NSRange ipRange;
ipRange = NSMakeRange(0, [intPad length] - [intPartString length] + 1); ipRange =
NSMakeRange(0, [intPad length] - [intPartString length] + 1);
[intPartString insertString: [intPartString insertString:
[intPad substringWithRange: ipRange] atIndex: 0]; [intPad substringWithRange: ipRange] atIndex: 0];
[intPartString replaceOccurrencesOfString: @"_" [intPartString replaceOccurrencesOfString: @"_"
@ -972,6 +1025,9 @@ static NSUInteger _defaultBehavior = 0;
return wholeString; return wholeString;
} }
return nil;
}
- (NSDictionary*) textAttributesForNegativeValues - (NSDictionary*) textAttributesForNegativeValues
{ {
return _attributesForNegativeValues; return _attributesForNegativeValues;
@ -1261,12 +1317,12 @@ static NSUInteger _defaultBehavior = 0;
- (void) setNilSymbol: (NSString *) string - (void) setNilSymbol: (NSString *) string
{ {
return; return; // FIXME
} }
- (NSString *) nilSymbol - (NSString *) nilSymbol
{ {
return nil; return nil; // FIXME
} }
- (void) setNotANumberSymbol: (NSString *) string - (void) setNotANumberSymbol: (NSString *) string
@ -1456,12 +1512,12 @@ static NSUInteger _defaultBehavior = 0;
- (void) setTextAttributesForZero: (NSDictionary *) newAttributes - (void) setTextAttributesForZero: (NSDictionary *) newAttributes
{ {
return; return; // FIXME
} }
- (NSDictionary *) textAttributesForZero - (NSDictionary *) textAttributesForZero
{ {
return nil; return nil; // FIXME
} }
- (void) setTextAttributesForNil: (NSDictionary *) newAttributes - (void) setTextAttributesForNil: (NSDictionary *) newAttributes
@ -1471,37 +1527,37 @@ static NSUInteger _defaultBehavior = 0;
- (NSDictionary *) textAttributesForNil - (NSDictionary *) textAttributesForNil
{ {
return nil; return nil; // FIXME
} }
- (void) setTextAttributesForNotANumber: (NSDictionary *) newAttributes - (void) setTextAttributesForNotANumber: (NSDictionary *) newAttributes
{ {
return; return; // FIXME
} }
- (NSDictionary *) textAttributesForNotANumber - (NSDictionary *) textAttributesForNotANumber
{ {
return nil; return nil; // FIXME
} }
- (void) setTextAttributesForPositiveInfinity: (NSDictionary *) newAttributes - (void) setTextAttributesForPositiveInfinity: (NSDictionary *) newAttributes
{ {
return; return; // FIXME
} }
- (NSDictionary *) textAttributesForPositiveInfinity - (NSDictionary *) textAttributesForPositiveInfinity
{ {
return nil; return nil; // FIXME
} }
- (void) setTextAttributesForNegativeInfinity: (NSDictionary *) newAttributes - (void) setTextAttributesForNegativeInfinity: (NSDictionary *) newAttributes
{ {
return; return; // FIXME
} }
- (NSDictionary *) textAttributesForNegativeInfinity - (NSDictionary *) textAttributesForNegativeInfinity
{ {
return nil; return nil; // FIXME
} }
@ -1525,22 +1581,39 @@ static NSUInteger _defaultBehavior = 0;
- (void) setUsesGroupingSeparator: (BOOL) flag - (void) setUsesGroupingSeparator: (BOOL) flag
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_GROUPING_USED, flag);
#else
return; return;
#endif
} }
- (BOOL) usesGroupingSeparator - (BOOL) usesGroupingSeparator
{ {
#if GS_USE_ICU == 1
return (BOOL)unum_getAttribute (_formatter, UNUM_GROUPING_USED);
#else
return NO; return NO;
#endif
} }
- (void) setAlwaysShowsDecimalSeparator: (BOOL) flag - (void) setAlwaysShowsDecimalSeparator: (BOOL) flag
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_DECIMAL_ALWAYS_SHOWN, flag);
#else
return; return;
#endif
} }
- (BOOL) alwaysShowsDecimalSeparator - (BOOL) alwaysShowsDecimalSeparator
{ {
#if GS_USE_ICU == 1
return (BOOL)unum_getAttribute (_formatter, UNUM_DECIMAL_ALWAYS_SHOWN);
#else
return NO; return NO;
#endif
} }
- (void) setCurrencyDecimalSeparator: (NSString *) string - (void) setCurrencyDecimalSeparator: (NSString *) string
@ -1563,84 +1636,151 @@ static NSUInteger _defaultBehavior = 0;
- (void) setGroupingSize: (NSUInteger) number - (void) setGroupingSize: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_GROUPING_SIZE, number);
#else
return; return;
#endif
} }
- (NSUInteger) groupingSize - (NSUInteger) groupingSize
{ {
return 0; #if GS_USE_ICU == 1
return (NSUInteger)unum_getAttribute (_formatter, UNUM_GROUPING_SIZE);
#else
return 3;
#endif
} }
- (void) setSecondaryGroupingSize: (NSUInteger) number - (void) setSecondaryGroupingSize: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_SECONDARY_GROUPING_SIZE, number);
#else
return; return;
#endif
} }
- (NSUInteger) secondaryGroupingSize - (NSUInteger) secondaryGroupingSize
{ {
return 0; #if GS_USE_ICU == 1
return (NSUInteger)unum_getAttribute (_formatter,
UNUM_SECONDARY_GROUPING_SIZE);
#else
return 3;
#endif
} }
- (void) setPaddingCharacter: (NSString *) string - (void) setPaddingCharacter: (NSString *) string
{ {
#if GS_USE_ICU == 1
[self _setTextAttribute: string : UNUM_PADDING_CHARACTER];
#else
return; return;
#endif
} }
- (NSString *) paddingCharacter - (NSString *) paddingCharacter
{ {
#if GS_USE_ICU == 1
return [self _getTextAttribute: UNUM_PADDING_CHARACTER];
#else
return nil; return nil;
#endif
} }
- (void) setPaddingPosition: (NSNumberFormatterPadPosition) position - (void) setPaddingPosition: (NSNumberFormatterPadPosition) position
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_PADDING_POSITION,
_NSToICUPadPosition (position));
#else
return; return;
#endif
} }
- (NSNumberFormatterPadPosition) paddingPosition - (NSNumberFormatterPadPosition) paddingPosition
{ {
#if GS_USE_ICU == 1
return _ICUToNSPadPosition(unum_getAttribute (_formatter,
UNUM_PADDING_POSITION));
#else
return 0; return 0;
#endif
} }
- (void) setMinimumIntegerDigits: (NSUInteger) number - (void) setMinimumIntegerDigits: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_MIN_INTEGER_DIGITS, number);
#else
return; return;
#endif
} }
- (NSUInteger) minimumIntegerDigits - (NSUInteger) minimumIntegerDigits
{ {
#if GS_USE_ICU == 1
return (NSUInteger)unum_getAttribute (_formatter, UNUM_MIN_INTEGER_DIGITS);
#else
return 0; return 0;
#endif
} }
- (void) setMinimumFractionDigits: (NSUInteger) number - (void) setMinimumFractionDigits: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_MIN_FRACTION_DIGITS, number);
#else
return; return;
#endif
} }
- (NSUInteger) minimumFractionDigits - (NSUInteger) minimumFractionDigits
{ {
#if GS_USE_ICU == 1
return (NSUInteger)unum_getAttribute (_formatter, UNUM_MIN_FRACTION_DIGITS);
#else
return 0; return 0;
#endif
} }
- (void) setMaximumIntegerDigits: (NSUInteger) number - (void) setMaximumIntegerDigits: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_MAX_INTEGER_DIGITS, number);
#else
return; return;
#endif
} }
- (NSUInteger) maximumIntegerDigits - (NSUInteger) maximumIntegerDigits
{ {
#if GS_USE_ICU == 1
return (NSUInteger)unum_getAttribute (_formatter, UNUM_MAX_INTEGER_DIGITS);
#else
return 0; return 0;
#endif
} }
- (void) setMaximumFractionDigits: (NSUInteger) number - (void) setMaximumFractionDigits: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_MAX_FRACTION_DIGITS, number);
#else
return; return;
#endif
} }
- (NSUInteger) maximumFractionDigits - (NSUInteger) maximumFractionDigits
{ {
#if GS_USE_ICU == 1
return (NSUInteger)unum_getAttribute (_formatter, UNUM_MAX_FRACTION_DIGITS);
#else
return 0; return 0;
#endif
} }
@ -1655,32 +1795,56 @@ static NSUInteger _defaultBehavior = 0;
- (void) setUsesSignificantDigits: (BOOL) flag - (void) setUsesSignificantDigits: (BOOL) flag
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_SIGNIFICANT_DIGITS_USED, flag);
#else
return; return;
#endif
} }
- (BOOL) usesSignificantDigits - (BOOL) usesSignificantDigits
{ {
#if GS_USE_ICU == 1
return (BOOL)unum_getAttribute (_formatter, UNUM_SIGNIFICANT_DIGITS_USED);
#else
return NO; return NO;
#endif
} }
- (void) setMinimumSignificantDigits: (NSUInteger) number - (void) setMinimumSignificantDigits: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_MIN_SIGNIFICANT_DIGITS, number);
#else
return; return;
#endif
} }
- (NSUInteger) minimumSignificantDigits - (NSUInteger) minimumSignificantDigits
{ {
#if GS_USE_ICU == 1
return (BOOL)unum_getAttribute (_formatter, UNUM_MIN_SIGNIFICANT_DIGITS);
#else
return 0; return 0;
#endif
} }
- (void) setMaximumSignificantDigits: (NSUInteger) number - (void) setMaximumSignificantDigits: (NSUInteger) number
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_MAX_SIGNIFICANT_DIGITS, number);
#else
return; return;
#endif
} }
- (NSUInteger) maximumSignificantDigits - (NSUInteger) maximumSignificantDigits
{ {
#if GS_USE_ICU == 1
return (BOOL)unum_getAttribute (_formatter, UNUM_MAX_SIGNIFICANT_DIGITS);
#else
return 0; return 0;
#endif
} }
@ -1705,12 +1869,20 @@ static NSUInteger _defaultBehavior = 0;
- (void) setLenient: (BOOL) flag - (void) setLenient: (BOOL) flag
{ {
#if GS_USE_ICU == 1
unum_setAttribute (_formatter, UNUM_LENIENT_PARSE, flag);
#else
return; return;
#endif
} }
- (BOOL) isLenient - (BOOL) isLenient
{ {
#if GS_USE_ICU == 1
return (BOOL)unum_getAttribute (_formatter, UNUM_LENIENT_PARSE);
#else
return NO; return NO;
#endif
} }
@ -1728,7 +1900,20 @@ static NSUInteger _defaultBehavior = 0;
+ (NSString *) localizedStringFromNumber: (NSNumber *) num + (NSString *) localizedStringFromNumber: (NSNumber *) num
numberStyle: (NSNumberFormatterStyle) localizationStyle numberStyle: (NSNumberFormatterStyle) localizationStyle
{ {
#if GS_USE_ICU == 1
NSNumberFormatter *fmt;
NSString *result;
fmt = [[NSNumberFormatter alloc] init];
[fmt setLocale: [NSLocale currentLocale]];
[fmt setNumberStyle: localizationStyle];
result = [fmt stringFromNumber: num];
RELEASE(fmt);
return result;
#else
return nil; return nil;
#endif
} }
@end @end
@ -1771,7 +1956,7 @@ static NSUInteger _defaultBehavior = 0;
length = [string length]; length = [string length];
if (length > MAX_SYMBOL_SIZE) if (length > MAX_SYMBOL_SIZE)
str = (unichar *)NSZoneMalloc ([self zone], length); str = (unichar *)NSZoneMalloc ([self zone], length * sizeof(unichar));
[string getCharacters: str range: NSMakeRange (0, length)]; [string getCharacters: str range: NSMakeRange (0, length)];
unum_setSymbol (_formatter, symbol, str, length, &err); unum_setSymbol (_formatter, symbol, str, length, &err);
@ -1796,7 +1981,7 @@ static NSUInteger _defaultBehavior = 0;
MAX_SYMBOL_SIZE, &err); MAX_SYMBOL_SIZE, &err);
if (length > MAX_SYMBOL_SIZE) if (length > MAX_SYMBOL_SIZE)
{ {
str = (UChar *)NSZoneMalloc ([self zone], length); str = (UChar *)NSZoneMalloc ([self zone], length * sizeof(UChar));
length = unum_getSymbol (_formatter, symbol, str, length = unum_getSymbol (_formatter, symbol, str,
length, &err); length, &err);
} }
@ -1821,7 +2006,7 @@ static NSUInteger _defaultBehavior = 0;
length = [string length]; length = [string length];
if (length > MAX_TEXT_ATTRIB_SIZE) if (length > MAX_TEXT_ATTRIB_SIZE)
str = (unichar *)NSZoneMalloc ([self zone], length); str = (unichar *)NSZoneMalloc ([self zone], length * sizeof(unichar));
[string getCharacters: str range: NSMakeRange (0, length)]; [string getCharacters: str range: NSMakeRange (0, length)];
unum_setTextAttribute (_formatter, attrib, str, length, &err); unum_setTextAttribute (_formatter, attrib, str, length, &err);
@ -1846,7 +2031,7 @@ static NSUInteger _defaultBehavior = 0;
MAX_TEXT_ATTRIB_SIZE, &err); MAX_TEXT_ATTRIB_SIZE, &err);
if (length > MAX_TEXT_ATTRIB_SIZE) if (length > MAX_TEXT_ATTRIB_SIZE)
{ {
str = (UChar *)NSZoneMalloc ([self zone], length); str = (UChar *)NSZoneMalloc ([self zone], length * sizeof(UChar));
length = unum_getTextAttribute (_formatter, attrib, str, length = unum_getTextAttribute (_formatter, attrib, str,
length, &err); length, &err);
} }