Skip to content

[LengthFormatter] Implementation of LengthFormatter and tests #745

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 2 commits into from
Dec 20, 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
4 changes: 4 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
7900433B1CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790043391CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift */; };
7900433C1CACD33E00ECCBF1 /* TestNSPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7900433A1CACD33E00ECCBF1 /* TestNSPredicate.swift */; };
AE35A1861CBAC85E0042DB84 /* SwiftFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = AE35A1851CBAC85E0042DB84 /* SwiftFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; };
BD8042161E09857800487EB8 /* TestNSLengthFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD8042151E09857800487EB8 /* TestNSLengthFormatter.swift */; };
BDFDF0A71DFF5B3E00C04CC5 /* TestNSPersonNameComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDFDF0A61DFF5B3E00C04CC5 /* TestNSPersonNameComponents.swift */; };
BF8E65311DC3B3CB005AB5C3 /* TestNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF8E65301DC3B3CB005AB5C3 /* TestNotification.swift */; };
CC5249C01D341D23007CB54D /* TestUnitConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC5249BF1D341D23007CB54D /* TestUnitConverter.swift */; };
Expand Down Expand Up @@ -752,6 +753,7 @@
88D28DE61C13AE9000494606 /* TestNSGeometry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSGeometry.swift; sourceTree = "<group>"; };
A5A34B551C18C85D00FD972B /* TestNSByteCountFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSByteCountFormatter.swift; sourceTree = "<group>"; };
AE35A1851CBAC85E0042DB84 /* SwiftFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftFoundation.h; sourceTree = "<group>"; };
BD8042151E09857800487EB8 /* TestNSLengthFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSLengthFormatter.swift; sourceTree = "<group>"; };
BDFDF0A61DFF5B3E00C04CC5 /* TestNSPersonNameComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSPersonNameComponents.swift; sourceTree = "<group>"; };
BF8E65301DC3B3CB005AB5C3 /* TestNotification.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNotification.swift; sourceTree = "<group>"; };
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSUUID.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1351,6 +1353,7 @@
5EB6A15C1C188FC40037DCB8 /* TestNSJSONSerialization.swift */,
D3A597F11C33C68E00295652 /* TestNSKeyedArchiver.swift */,
D3A597EF1C33A9E500295652 /* TestNSKeyedUnarchiver.swift */,
BD8042151E09857800487EB8 /* TestNSLengthFormatter.swift */,
61A395F91C2484490029B337 /* TestNSLocale.swift */,
61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */,
5EF673AB1C28B527006212A3 /* TestNSNotificationQueue.swift */,
Expand Down Expand Up @@ -2248,6 +2251,7 @@
5B13B3301C582D4C00651CE2 /* TestNSHTTPCookie.swift in Sources */,
5B13B3361C582D4C00651CE2 /* TestNSLocale.swift in Sources */,
5B13B3391C582D4C00651CE2 /* TestNSNull.swift in Sources */,
BD8042161E09857800487EB8 /* TestNSLengthFormatter.swift in Sources */,
5B13B3421C582D4C00651CE2 /* TestNSRunLoop.swift in Sources */,
5B13B34E1C582D4C00651CE2 /* TestNSXMLDocument.swift in Sources */,
5B13B32B1C582D4C00651CE2 /* TestNSData.swift in Sources */,
Expand Down
209 changes: 191 additions & 18 deletions Foundation/NSLengthFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//


extension LengthFormatter {
public enum Unit : Int {

case millimeter
case centimeter
case meter
case kilometer
case inch
case foot
case yard
case mile
public enum Unit: Int {
case millimeter = 8
case centimeter = 9
case meter = 11
case kilometer = 14
case inch = 1281
case foot = 1282
case yard = 1283
case mile = 1284
}
}

open class LengthFormatter : Formatter {

public override init() {
numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
unitStyle = .medium
isForPersonHeightUse = false
super.init()
}

public required init?(coder: NSCoder) {
NSUnimplemented()
numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
unitStyle = .medium
isForPersonHeightUse = false
super.init(coder:coder)
}

/*@NSCopying*/ open var numberFormatter: NumberFormatter! // default is NSNumberFormatter with NSNumberFormatterDecimalStyle
Expand All @@ -34,20 +44,183 @@ open class LengthFormatter : Formatter {
open var isForPersonHeightUse: Bool // default is NO; if it is set to YES, the number argument for -stringFromMeters: and -unitStringFromMeters: is considered as a person's height

// Format a combination of a number and an unit to a localized string.
open func string(fromValue value: Double, unit: LengthFormatter.Unit) -> String { NSUnimplemented() }
open func string(fromValue value: Double, unit: LengthFormatter.Unit) -> String {
guard let formattedValue = numberFormatter.string(from:NSNumber(value: value)) else {
fatalError("Cannot format \(value) as string")
}
let separator = unitStyle == LengthFormatter.UnitStyle.short ? "" : " "
return "\(formattedValue)\(separator)\(unitString(fromValue: value, unit: unit))"
}

// Format a number in meters to a localized string with the locale-appropriate unit and an appropriate scale (e.g. 4.3m = 14.1ft in the US locale).
open func string(fromMeters numberInMeters: Double) -> String { NSUnimplemented() }
open func string(fromMeters numberInMeters: Double) -> String {
//Convert to the locale-appropriate unit
let unitFromMeters = unit(fromMeters: numberInMeters)

//Map the unit to UnitLength type for conversion later
let unitLengthFromMeters = LengthFormatter.unitLength[unitFromMeters]!

//Create a measurement object based on the value in meters
let meterMeasurement = Measurement<UnitLength>(value:numberInMeters, unit: .meters)

//Convert the object to the locale-appropriate unit determined above
let unitMeasurement = meterMeasurement.converted(to: unitLengthFromMeters)

//Extract the number from the measurement
let numberInUnit = unitMeasurement.value

if isForPersonHeightUse && !LengthFormatter.isMetricSystemLocale(numberFormatter.locale) {
let feet = numberInUnit.rounded(.towardZero)
let feetString = string(fromValue: feet, unit: .foot)

let inches = abs(numberInUnit.truncatingRemainder(dividingBy: 1.0)) * 12
let inchesString = string(fromValue: inches, unit: .inch)

return ("\(feetString), \(inchesString)")
}
return string(fromValue: numberInUnit, unit: unitFromMeters)
}

// Return a localized string of the given unit, and if the unit is singular or plural is based on the given number.
open func unitString(fromValue value: Double, unit: Unit) -> String { NSUnimplemented() }
open func unitString(fromValue value: Double, unit: Unit) -> String {
if unitStyle == .short {
return LengthFormatter.shortSymbol[unit]!
} else if unitStyle == .medium {
return LengthFormatter.mediumSymbol[unit]!
} else if value == 1.0 {
return LengthFormatter.largeSingularSymbol[unit]!
} else {
return LengthFormatter.largePluralSymbol[unit]!
}
}

// Return the locale-appropriate unit, the same unit used by -stringFromMeters:.
open func unitString(fromMeters numberInMeters: Double, usedUnit unitp: UnsafeMutablePointer<Unit>?) -> String { NSUnimplemented() }
open func unitString(fromMeters numberInMeters: Double, usedUnit unitp: UnsafeMutablePointer<Unit>?) -> String {

//Convert to the locale-appropriate unit
let unitFromMeters = unit(fromMeters: numberInMeters)
unitp?.pointee = unitFromMeters

//Map the unit to UnitLength type for conversion later
let unitLengthFromMeters = LengthFormatter.unitLength[unitFromMeters]!

//Create a measurement object based on the value in meters
let meterMeasurement = Measurement<UnitLength>(value:numberInMeters, unit: .meters)

//Convert the object to the locale-appropriate unit determined above
let unitMeasurement = meterMeasurement.converted(to: unitLengthFromMeters)

//Extract the number from the measurement
let numberInUnit = unitMeasurement.value

//Return the appropriate representation of the unit based on the selected unit style
return unitString(fromValue: numberInUnit, unit: unitFromMeters)
}

/// This method selects the appropriate unit based on the formatter’s locale,
/// the magnitude of the value, and isForPersonHeightUse property.
///
/// - Parameter numberInMeters: the magnitude in terms of meters
/// - Returns: Returns the appropriate unit
private func unit(fromMeters numberInMeters: Double) -> Unit {
if LengthFormatter.isMetricSystemLocale(numberFormatter.locale) {
//Person height is always returned in cm for metric system
if isForPersonHeightUse { return .centimeter }

if numberInMeters > 1000 || numberInMeters < 0.0 {
return .kilometer
} else if numberInMeters > 1.0 {
return .meter
} else if numberInMeters > 0.01 {
return .centimeter
} else { return .millimeter }
} else {
//Person height is always returned in ft for U.S. system
if isForPersonHeightUse { return .foot }

let metricMeasurement = Measurement<UnitLength>(value:numberInMeters, unit: .meters)
let usMeasurement = metricMeasurement.converted(to: .feet)
let numberInFeet = usMeasurement.value

if numberInFeet < 0.0 || numberInFeet > 5280 {
return .mile
} else if numberInFeet > 3 || numberInFeet == 0.0 {
return .yard
} else if numberInFeet >= 1.0 {
return .foot
} else { return .inch }
}
}

/// TODO: Replace calls to the below function to use Locale.usesMetricSystem
/// Temporary workaround due to unpopulated Locale attributes
/// See https://bugs.swift.org/browse/SR-3202
private static func isMetricSystemLocale(_ locale: Locale) -> Bool {
switch locale.identifier {
case "en_US": return false
case "en_US_POSIX": return false
case "haw_US": return false
case "es_US": return false
case "chr_US": return false
case "my_MM": return false
case "en_LR": return false
case "vai_LR": return false
default: return true
}
}

/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
open override func objectValue(_ string: String) throws -> Any? { return nil }
}



/// Maps NSLengthFormatter.Unit enum to UnitLength class. Used for measurement conversion.
private static let unitLength: [Unit:UnitLength] = [.millimeter:.millimeters,
.centimeter:.centimeters,
.meter:.meters,
.kilometer:.kilometers,
.inch:.inches,
.foot:.feet,
.yard:.yards,
.mile:.miles]

/// Maps a unit to its short symbol. Reuses strings from UnitLength wherever possible.
private static let shortSymbol: [Unit: String] = [.millimeter:UnitLength.millimeters.symbol,
.centimeter:UnitLength.centimeters.symbol,
.meter:UnitLength.meters.symbol,
.kilometer:UnitLength.kilometers.symbol,
.inch:"\"",
.foot:"′",
.yard:UnitLength.yards.symbol,
.mile:UnitLength.miles.symbol]

/// Maps a unit to its medium symbol. Reuses strings from UnitLength.
private static let mediumSymbol: [Unit: String] = [.millimeter:UnitLength.millimeters.symbol,
.centimeter:UnitLength.centimeters.symbol,
.meter:UnitLength.meters.symbol,
.kilometer:UnitLength.kilometers.symbol,
.inch:UnitLength.inches.symbol,
.foot:UnitLength.feet.symbol,
.yard:UnitLength.yards.symbol,
.mile:UnitLength.miles.symbol]

/// Maps a unit to its large, singular symbol.
private static let largeSingularSymbol: [Unit: String] = [.millimeter:"millimeter",
.centimeter:"centimeter",
.meter:"meter",
.kilometer:"kilometer",
.inch:"inch",
.foot:"foot",
.yard:"yard",
.mile:"mile"]

/// Maps a unit to its large, plural symbol.
private static let largePluralSymbol: [Unit: String] = [.millimeter:"millimeters",
.centimeter:"centimeters",
.meter:"meters",
.kilometer:"kilometers",
.inch:"inches",
.foot:"feet",
.yard:"yards",
.mile:"miles"]
}
Loading