Skip to content

Commit 6450221

Browse files
committed
SR-1464: NSNumber.description is not compatible between OS X and linux
- Use Swift's stringification of numbers for .description and .stringValue - Use String(format:locale:...) with the correct format for the NSNumber type if locale is not nil. - Enable use of __CFStringFormatLocalizedNumber() on DEPLOYMENT_TARGET_LINUX.
1 parent 2d675f0 commit 6450221

File tree

4 files changed

+166
-42
lines changed

4 files changed

+166
-42
lines changed

CoreFoundation/String.subproj/CFString.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6013,7 +6013,7 @@ enum {
60136013
CFFormatDummyPointerType = 42 /* special case for %n */
60146014
};
60156015

6016-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
6016+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
60176017
/* Only come in here if spec->type is CFFormatLongType or CFFormatDoubleType. Pass in 0 for width or precision if not specified. Returns false if couldn't do the format (with the assumption the caller falls back to unlocalized).
60186018
*/
60196019
static Boolean __CFStringFormatLocalizedNumber(CFMutableStringRef output, CFLocaleRef locale, const CFPrintValue *values, const CFFormatSpec *spec, SInt32 width, SInt32 precision, Boolean hasPrecision) {
@@ -7109,7 +7109,7 @@ static Boolean __CFStringAppendFormatCore(CFMutableStringRef outputString, CFStr
71097109
switch (specs[curSpec].type) {
71107110
case CFFormatLongType:
71117111
case CFFormatDoubleType:
7112-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
7112+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
71137113
if (localizedFormatting && (specs[curSpec].flags & kCFStringFormatLocalizable)) { // We have a locale, so we do localized formatting
71147114
if (__CFStringFormatLocalizedNumber(outputString, (CFLocaleRef)formatOptions, values, &specs[curSpec], width, precision, hasPrecision)) break;
71157115
}

Foundation/NSNumber.swift

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ open class NSNumber : NSValue {
915915
}
916916

917917
open var stringValue: String {
918-
return description(withLocale: nil)
918+
return self.description
919919
}
920920

921921
/// Create an instance initialized to `value`.
@@ -958,39 +958,86 @@ open class NSNumber : NSValue {
958958
}
959959
}
960960

961-
private static let _numberFormatterForNilLocale: CFNumberFormatter = {
962-
let formatter: CFNumberFormatter
963-
formatter = CFNumberFormatterCreate(nil, CFLocaleCopyCurrent(), kCFNumberFormatterNoStyle)
964-
CFNumberFormatterSetProperty(formatter, kCFNumberFormatterMaxFractionDigits, 15._bridgeToObjectiveC())
965-
return formatter
966-
}()
967-
968961
open func description(withLocale locale: Locale?) -> String {
969-
// CFNumberFormatterCreateStringWithNumber() does not like numbers of type
970-
// SInt128Type, as it loses the type when looking it up and treats it as
971-
// an SInt64Type, so special case them.
972-
if _CFNumberGetType2(_cfObject) == kCFNumberSInt128Type {
973-
return String(format: "%@", unsafeBitCast(_cfObject, to: UnsafePointer<CFNumber>.self))
974-
}
962+
guard let locale = locale else { return self.description }
963+
switch _CFNumberGetType2(_cfObject) {
964+
case kCFNumberSInt8Type, kCFNumberCharType:
965+
return String(format: "%d", locale: locale, self.int8Value)
975966

976-
let aLocale = locale
977-
let formatter: CFNumberFormatter
978-
if (aLocale == nil) {
979-
formatter = NSNumber._numberFormatterForNilLocale
980-
} else {
981-
formatter = CFNumberFormatterCreate(nil, aLocale?._cfObject, kCFNumberFormatterDecimalStyle)
967+
case kCFNumberSInt16Type, kCFNumberShortType:
968+
return String(format: "%hi", locale: locale, self.int16Value)
969+
970+
case kCFNumberSInt32Type:
971+
return String(format: "%d", locale: locale, self.int32Value)
972+
973+
case kCFNumberIntType, kCFNumberLongType, kCFNumberNSIntegerType, kCFNumberCFIndexType:
974+
return String(format: "%ld", locale: locale, self.intValue)
975+
976+
case kCFNumberSInt64Type, kCFNumberLongLongType:
977+
return String(format: "%lld", locale: locale, self.int64Value)
978+
979+
case kCFNumberSInt128Type:
980+
let value = self.int128Value
981+
if value.high == 0 {
982+
return value.low.description // BUG: "%llu" doesnt work correctly and treats number as signed
983+
} else {
984+
// BUG: Note the locale is actually ignored here as this is converted using CFNumber.c:emit128()
985+
return String(format: "%@", locale: locale, unsafeBitCast(_cfObject, to: UnsafePointer<CFNumber>.self))
986+
}
987+
988+
case kCFNumberFloatType, kCFNumberCGFloatType, kCFNumberFloatType:
989+
return String(format: "%0.7g", locale: locale, self.floatValue)
990+
991+
case kCFNumberFloat64Type, kCFNumberDoubleType:
992+
return String(format: "%0.16g", locale: locale, self.doubleValue)
993+
994+
case kCFNumberCGFloatType:
995+
if Int.max == Int32.max {
996+
return String(format: "%0.7g", locale: locale, self.floatValue)
997+
} else {
998+
return String(format: "%0.16g", locale: locale, self.doubleValue)
999+
}
1000+
default: fatalError("Unknown NSNumber Type")
9821001
}
983-
return CFNumberFormatterCreateStringWithNumber(nil, formatter, self._cfObject)._swiftObject
9841002
}
9851003

9861004
override open var _cfTypeID: CFTypeID {
9871005
return CFNumberGetTypeID()
9881006
}
9891007

9901008
open override var description: String {
991-
return description(withLocale: nil)
1009+
switch _CFNumberGetType2(_cfObject) {
1010+
case kCFNumberSInt8Type, kCFNumberCharType, kCFNumberSInt16Type, kCFNumberShortType,
1011+
kCFNumberSInt32Type, kCFNumberIntType, kCFNumberLongType, kCFNumberNSIntegerType, kCFNumberCFIndexType:
1012+
return self.intValue.description
1013+
1014+
case kCFNumberSInt64Type, kCFNumberLongLongType:
1015+
return self.int64Value.description
1016+
1017+
case kCFNumberSInt128Type:
1018+
let value = self.int128Value
1019+
if value.high == 0 {
1020+
return value.low.description
1021+
} else {
1022+
return String(format: "%@", locale: nil, unsafeBitCast(_cfObject, to: UnsafePointer<CFNumber>.self))
1023+
}
1024+
1025+
case kCFNumberFloatType, kCFNumberCGFloatType, kCFNumberFloatType:
1026+
return self.floatValue.description
1027+
1028+
case kCFNumberFloat64Type, kCFNumberDoubleType:
1029+
return self.doubleValue.description
1030+
1031+
case kCFNumberCGFloatType:
1032+
if Int.max == Int32.max {
1033+
return self.floatValue.description
1034+
} else {
1035+
return self.doubleValue.description
1036+
}
1037+
default: fatalError("Unknown NSNumber Type")
1038+
}
9921039
}
993-
1040+
9941041
internal func _cfNumberType() -> CFNumberType {
9951042
switch objCType.pointee {
9961043
case 0x42: return kCFNumberCharType

TestFoundation/TestNSNumber.swift

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,21 +1016,100 @@ class TestNSNumber : XCTestCase {
10161016

10171017

10181018
func test_description() {
1019-
let nsnumber: NSNumber = 1000
1020-
let expectedDesc = "1000"
1021-
XCTAssertEqual(nsnumber.description, expectedDesc, "expected \(expectedDesc) but received \(nsnumber.description)")
1019+
XCTAssertEqual(NSNumber(value: 1000).description, "1000")
1020+
XCTAssertEqual(NSNumber(value: 0.001).description, "0.001")
1021+
1022+
XCTAssertEqual(NSNumber(value: Int8.min).description, "-128")
1023+
XCTAssertEqual(NSNumber(value: Int8.max).description, "127")
1024+
XCTAssertEqual(NSNumber(value: Int16.min).description, "-32768")
1025+
XCTAssertEqual(NSNumber(value: Int16.max).description, "32767")
1026+
XCTAssertEqual(NSNumber(value: Int32.min).description, "-2147483648")
1027+
XCTAssertEqual(NSNumber(value: Int32.max).description, "2147483647")
1028+
XCTAssertEqual(NSNumber(value: Int64.min).description, "-9223372036854775808")
1029+
XCTAssertEqual(NSNumber(value: Int64.max).description, "9223372036854775807")
1030+
1031+
XCTAssertEqual(NSNumber(value: UInt8.min).description, "0")
1032+
XCTAssertEqual(NSNumber(value: UInt8.max).description, "255")
1033+
XCTAssertEqual(NSNumber(value: UInt16.min).description, "0")
1034+
XCTAssertEqual(NSNumber(value: UInt16.max).description, "65535")
1035+
XCTAssertEqual(NSNumber(value: UInt32.min).description, "0")
1036+
XCTAssertEqual(NSNumber(value: UInt32.max).description, "4294967295")
1037+
XCTAssertEqual(NSNumber(value: UInt64.min).description, "0")
1038+
XCTAssertEqual(NSNumber(value: UInt64.max).description, "18446744073709551615")
10221039
}
1023-
1040+
10241041
func test_descriptionWithLocale() {
1025-
let nsnumber: NSNumber = 1000
1026-
let values : Dictionary = [
1027-
Locale(identifier: "en_GB") : "1,000",
1028-
Locale(identifier: "de_DE") : "1.000",
1029-
]
1030-
for (locale, expectedDesc) in values {
1031-
let receivedDesc = nsnumber.description(withLocale: locale)
1032-
XCTAssertEqual(receivedDesc, expectedDesc, "expected \(expectedDesc) but received \(receivedDesc)")
1033-
}
1042+
// nil Locale
1043+
XCTAssertEqual(NSNumber(value: 1000).description(withLocale: nil), "1000")
1044+
XCTAssertEqual(NSNumber(value: 0.001).description(withLocale: nil), "0.001")
1045+
1046+
XCTAssertEqual(NSNumber(value: Int8.min).description(withLocale: nil), "-128")
1047+
XCTAssertEqual(NSNumber(value: Int8.max).description(withLocale: nil), "127")
1048+
XCTAssertEqual(NSNumber(value: Int16.min).description(withLocale: nil), "-32768")
1049+
XCTAssertEqual(NSNumber(value: Int16.max).description(withLocale: nil), "32767")
1050+
XCTAssertEqual(NSNumber(value: Int32.min).description(withLocale: nil), "-2147483648")
1051+
XCTAssertEqual(NSNumber(value: Int32.max).description(withLocale: nil), "2147483647")
1052+
XCTAssertEqual(NSNumber(value: Int64.min).description(withLocale: nil), "-9223372036854775808")
1053+
XCTAssertEqual(NSNumber(value: Int64.max).description(withLocale: nil), "9223372036854775807")
1054+
1055+
XCTAssertEqual(NSNumber(value: UInt8.min).description(withLocale: nil), "0")
1056+
XCTAssertEqual(NSNumber(value: UInt8.max).description(withLocale: nil), "255")
1057+
XCTAssertEqual(NSNumber(value: UInt16.min).description(withLocale: nil), "0")
1058+
XCTAssertEqual(NSNumber(value: UInt16.max).description(withLocale: nil), "65535")
1059+
XCTAssertEqual(NSNumber(value: UInt32.min).description(withLocale: nil), "0")
1060+
XCTAssertEqual(NSNumber(value: UInt32.max).description(withLocale: nil), "4294967295")
1061+
XCTAssertEqual(NSNumber(value: UInt64.min).description(withLocale: nil), "0")
1062+
XCTAssertEqual(NSNumber(value: UInt64.max).description(withLocale: nil), "18446744073709551615")
1063+
1064+
// en_GB Locale
1065+
XCTAssertEqual(NSNumber(value: 1000).description(withLocale: Locale(identifier: "en_GB")), "1,000")
1066+
XCTAssertEqual(NSNumber(value: 0.001).description(withLocale: Locale(identifier: "en_GB")), "0.001")
1067+
1068+
XCTAssertEqual(NSNumber(value: Int8.min).description(withLocale: Locale(identifier: "en_GB")), "-128")
1069+
XCTAssertEqual(NSNumber(value: Int8.max).description(withLocale: Locale(identifier: "en_GB")), "127")
1070+
XCTAssertEqual(NSNumber(value: Int16.min).description(withLocale: Locale(identifier: "en_GB")), "-32,768")
1071+
XCTAssertEqual(NSNumber(value: Int16.max).description(withLocale: Locale(identifier: "en_GB")), "32,767")
1072+
XCTAssertEqual(NSNumber(value: Int32.min).description(withLocale: Locale(identifier: "en_GB")), "-2,147,483,648")
1073+
XCTAssertEqual(NSNumber(value: Int32.max).description(withLocale: Locale(identifier: "en_GB")), "2,147,483,647")
1074+
XCTAssertEqual(NSNumber(value: Int64.min).description(withLocale: Locale(identifier: "en_GB")), "-9,223,372,036,854,775,808")
1075+
XCTAssertEqual(NSNumber(value: Int64.max).description(withLocale: Locale(identifier: "en_GB")), "9,223,372,036,854,775,807")
1076+
1077+
XCTAssertEqual(NSNumber(value: UInt8.min).description(withLocale: Locale(identifier: "en_GB")), "0")
1078+
XCTAssertEqual(NSNumber(value: UInt8.max).description(withLocale: Locale(identifier: "en_GB")), "255")
1079+
XCTAssertEqual(NSNumber(value: UInt16.min).description(withLocale: Locale(identifier: "en_GB")), "0")
1080+
XCTAssertEqual(NSNumber(value: UInt16.max).description(withLocale: Locale(identifier: "en_GB")), "65,535")
1081+
XCTAssertEqual(NSNumber(value: UInt32.min).description(withLocale: Locale(identifier: "en_GB")), "0")
1082+
XCTAssertEqual(NSNumber(value: UInt32.max).description(withLocale: Locale(identifier: "en_GB")), "4,294,967,295")
1083+
XCTAssertEqual(NSNumber(value: UInt64.min).description(withLocale: Locale(identifier: "en_GB")), "0")
1084+
1085+
// This is the correct value but currently buggy and the locale is not used
1086+
// XCTAssertEqual(NSNumber(value: UInt64.max).description(withLocale: Locale(identifier: "en_GB")), "18,446,744,073,709,551,615")
1087+
XCTAssertEqual(NSNumber(value: UInt64.max).description(withLocale: Locale(identifier: "en_GB")), "18446744073709551615")
1088+
1089+
// de_DE Locale
1090+
XCTAssertEqual(NSNumber(value: 1000).description(withLocale: Locale(identifier: "de_DE")), "1.000")
1091+
XCTAssertEqual(NSNumber(value: 0.001).description(withLocale: Locale(identifier: "de_DE")), "0,001")
1092+
1093+
XCTAssertEqual(NSNumber(value: Int8.min).description(withLocale: Locale(identifier: "de_DE")), "-128")
1094+
XCTAssertEqual(NSNumber(value: Int8.max).description(withLocale: Locale(identifier: "de_DE")), "127")
1095+
XCTAssertEqual(NSNumber(value: Int16.min).description(withLocale: Locale(identifier: "de_DE")), "-32.768")
1096+
XCTAssertEqual(NSNumber(value: Int16.max).description(withLocale: Locale(identifier: "de_DE")), "32.767")
1097+
XCTAssertEqual(NSNumber(value: Int32.min).description(withLocale: Locale(identifier: "de_DE")), "-2.147.483.648")
1098+
XCTAssertEqual(NSNumber(value: Int32.max).description(withLocale: Locale(identifier: "de_DE")), "2.147.483.647")
1099+
XCTAssertEqual(NSNumber(value: Int64.min).description(withLocale: Locale(identifier: "de_DE")), "-9.223.372.036.854.775.808")
1100+
XCTAssertEqual(NSNumber(value: Int64.max).description(withLocale: Locale(identifier: "de_DE")), "9.223.372.036.854.775.807")
1101+
1102+
XCTAssertEqual(NSNumber(value: UInt8.min).description(withLocale: Locale(identifier: "de_DE")), "0")
1103+
XCTAssertEqual(NSNumber(value: UInt8.max).description(withLocale: Locale(identifier: "de_DE")), "255")
1104+
XCTAssertEqual(NSNumber(value: UInt16.min).description(withLocale: Locale(identifier: "de_DE")), "0")
1105+
XCTAssertEqual(NSNumber(value: UInt16.max).description(withLocale: Locale(identifier: "de_DE")), "65.535")
1106+
XCTAssertEqual(NSNumber(value: UInt32.min).description(withLocale: Locale(identifier: "de_DE")), "0")
1107+
XCTAssertEqual(NSNumber(value: UInt32.max).description(withLocale: Locale(identifier: "de_DE")), "4.294.967.295")
1108+
XCTAssertEqual(NSNumber(value: UInt64.min).description(withLocale: Locale(identifier: "de_DE")), "0")
1109+
1110+
// This is the correct value but currently buggy and the locale is not used
1111+
//XCTAssertEqual(NSNumber(value: UInt64.max).description(withLocale: Locale(identifier: "de_DE")), "18.446.744.073.709.551.615")
1112+
XCTAssertEqual(NSNumber(value: UInt64.max).description(withLocale: Locale(identifier: "de_DE")), "18446744073709551615")
10341113
}
10351114

10361115
func test_objCType() {

TestFoundation/TestNSString.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,19 +832,17 @@ class TestNSString: LoopbackServerTest {
832832
XCTAssertEqual(string, "Default value is 1000 (42.0)")
833833
}
834834

835-
#if false // these two tests expose bugs in icu4c's localization on some linux builds (disable until we can get a uniform fix for this)
836835
withVaList(argument) {
837836
pointer in
838-
let string = NSString(format: "en_GB value is %d (%.1f)", locale: Locale.init(localeIdentifier: "en_GB"), arguments: pointer)
837+
let string = NSString(format: "en_GB value is %d (%.1f)", locale: Locale.init(identifier: "en_GB") as AnyObject, arguments: pointer)
839838
XCTAssertEqual(string, "en_GB value is 1,000 (42.0)")
840839
}
841840

842841
withVaList(argument) {
843842
pointer in
844-
let string = NSString(format: "de_DE value is %d (%.1f)", locale: Locale.init(localeIdentifier: "de_DE"), arguments: pointer)
843+
let string = NSString(format: "de_DE value is %d (%.1f)", locale: Locale.init(identifier: "de_DE") as AnyObject, arguments: pointer)
845844
XCTAssertEqual(string, "de_DE value is 1.000 (42,0)")
846845
}
847-
#endif
848846

849847
withVaList(argument) {
850848
pointer in

0 commit comments

Comments
 (0)