Skip to content

[SR-789] Remove localization of NSNumber.description #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,15 @@ public class NSNumber : NSValue {
}

public func descriptionWithLocale(locale: AnyObject?) -> String {
guard let aLocale = locale else { return description }
let formatter = CFNumberFormatterCreate(nil, (aLocale as! NSLocale)._cfObject, kCFNumberFormatterDecimalStyle)
let aLocale = locale
let formatter: CFNumberFormatter
if (aLocale == nil) {
formatter = CFNumberFormatterCreate(nil, CFLocaleCopyCurrent(), kCFNumberFormatterNoStyle)
CFNumberFormatterSetProperty(formatter, kCFNumberFormatterMaxFractionDigits, 15._bridgeToObject())

} else {
formatter = CFNumberFormatterCreate(nil, (aLocale as! NSLocale)._cfObject, kCFNumberFormatterDecimalStyle)
}
return CFNumberFormatterCreateStringWithNumber(nil, formatter, self._cfObject)._swiftObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the easier and more compatible version would be to use the locale formatter method (this is how the objc version does it)

    public func descriptionWithLocale(locale: AnyObject?) -> String {
        switch objCType.memory {
        case 0x42: // B
            fallthrough
        case 0x63: // c
            return NSString(format: "%d", locale: locale, arguments: getVaList([Int32(self.charValue)])).bridge()
        case 0x43: // C
            return NSString(format: "%u", locale: locale, arguments: getVaList([UInt32(self.unsignedCharValue)])).bridge()
        case 0x73: // s
            return NSString(format: "%hi", locale: locale, arguments: getVaList([self.shortValue])).bridge()
        case 0x53: // S
            return NSString(format: "%hu", locale: locale, arguments: getVaList([self.unsignedShortValue])).bridge()
        case 0x69: // i
            return NSString(format: "%d", locale: locale, arguments: getVaList([self.intValue])).bridge()
        case 0x49: // I
            return NSString(format: "%u", locale: locale, arguments: getVaList([self.unsignedIntValue])).bridge()
        case 0x6C: // l
            return NSString(format: "%ld", locale: locale, arguments: getVaList([self.longValue])).bridge()
        case 0x4C: // L
            return NSString(format: "%lu", locale: locale, arguments: getVaList([self.unsignedLongValue])).bridge()
        case 0x66: // f
            return NSString(format: "%0.7g", locale: locale, arguments: getVaList([self.floatValue])).bridge()
        case 0x64: // d
            return NSString(format: "%0.16g", locale: locale, arguments: getVaList([self.doubleValue])).bridge()
        case 0x71: // q
            return NSString(format: "%lld", locale: locale, arguments: getVaList([self.longLongValue])).bridge()
        case 0x51: // Q
            return NSString(format: "%llu", locale: locale, arguments: getVaList([self.unsignedLongLongValue])).bridge()
        default:
            fatalError("Bad number type")
        }
    }

    public override var description: String {
        return descriptionWithLocale(nil)
    }

}

Expand All @@ -468,11 +475,13 @@ public class NSNumber : NSValue {
}

public override var description: String {
let locale = CFLocaleCopyCurrent()
let formatter = CFNumberFormatterCreate(nil, locale, kCFNumberFormatterDecimalStyle)
CFNumberFormatterSetProperty(formatter, kCFNumberFormatterMaxFractionDigits, 15._bridgeToObject())
return CFNumberFormatterCreateStringWithNumber(nil, formatter, self._cfObject)._swiftObject
return descriptionWithLocale(nil)
}
// let locale = CFLocaleCopyCurrent()
// let formatter = CFNumberFormatterCreate(nil, locale, kCFNumberFormatterNoStyle)
// CFNumberFormatterSetProperty(formatter, kCFNumberFormatterMaxFractionDigits, 15._bridgeToObject())
// return CFNumberFormatterCreateStringWithNumber(nil, formatter, self._cfObject)._swiftObject
// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably can remove the commented out code. git history is a beautiful thing ;)

}

extension CFNumber : _NSBridgable {
Expand Down
20 changes: 20 additions & 0 deletions TestFoundation/TestNSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TestNSNumber : XCTestCase {
("test_compareNumberWithFloat", test_compareNumberWithFloat ),
("test_compareNumberWithDouble", test_compareNumberWithDouble ),
("test_reflection", test_reflection ),
("test_description", test_description ),
("test_descriptionWithLocale", test_descriptionWithLocale ),
]
}

Expand Down Expand Up @@ -418,4 +420,22 @@ class TestNSNumber : XCTestCase {
default: XCTAssert(false, "NSNumber(double:) quicklook is not a Double")
}
}

func test_description() {
let nsnumber: NSNumber = 1000
let expectedDesc = "1000"
XCTAssertEqual(nsnumber.description, expectedDesc, "expected \(expectedDesc) but received \(nsnumber.description)")
}

func test_descriptionWithLocale() {
let nsnumber: NSNumber = 1000
let values : Dictionary = [
NSLocale.init(localeIdentifier: "en_GB") : "1,000",
NSLocale.init(localeIdentifier: "de_DE") : "1.000",
]
for (locale, expectedDesc) in values {
let receivedDesc = nsnumber.descriptionWithLocale(locale)
XCTAssertEqual(receivedDesc, expectedDesc, "expected \(expectedDesc) but received \(receivedDesc)")
}
}
}