This repository was archived by the owner on Dec 29, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +6
-36
lines changed
Tests/CommandLineKitTests Expand file tree Collapse file tree 3 files changed +6
-36
lines changed Original file line number Diff line number Diff line change @@ -43,40 +43,10 @@ internal extension String {
43
43
* - returns: A Double if the string can be parsed, nil otherwise.
44
44
*/
45
45
func toDouble( ) -> Double ? {
46
- var characteristic : String = " 0 "
47
- var mantissa : String = " 0 "
48
- var inMantissa : Bool = false
49
- var isNegative : Bool = false
50
- let decimalPoint = self . _localDecimalPoint ( )
51
-
52
- let charactersEnumerator = self . characters. enumerated ( )
53
- for (i, c) in charactersEnumerator {
54
- if i == 0 && c == " - " {
55
- isNegative = true
56
- continue
57
- }
58
-
59
- if c == decimalPoint {
60
- inMantissa = true
61
- continue
62
- }
63
-
64
- if Int ( String ( c) ) != nil {
65
- if !inMantissa {
66
- characteristic. append ( c)
67
- } else {
68
- mantissa. append ( c)
69
- }
70
- } else {
71
- /* Non-numeric character found, bail */
72
- return nil
73
- }
74
- }
75
-
76
- let doubleCharacteristic = Double ( Int ( characteristic) !)
77
- return ( doubleCharacteristic +
78
- Double( Int ( mantissa) !) / pow( Double ( 10 ) , Double ( mantissa. characters. count - 1 ) ) ) *
79
- ( isNegative ? - 1 : 1 )
46
+ let decimalPoint = String ( self . _localDecimalPoint ( ) )
47
+ guard decimalPoint == " . " || self . range ( of: " . " ) == nil else { return nil }
48
+ let localeSelf = self . replacingOccurrences ( of: decimalPoint, with: " . " )
49
+ return Double ( localeSelf)
80
50
}
81
51
82
52
/**
Original file line number Diff line number Diff line change @@ -71,8 +71,8 @@ class StringExtensionTests: XCTestCase {
71
71
72
72
73
73
/* Various extraneous chars */
74
- let k = " +42.3 " . toDouble ( )
75
- XCTAssertNil ( k, " Parsed double with extraneous +" )
74
+ let k = " +42.3 " . toDouble ( ) // 4 Jan 2017: leading + is valid language syntax
75
+ XCTAssertEqual ( k, 42.3 , " Failed to parse double with leading +" )
76
76
77
77
let l = " 827.2 " . toDouble ( )
78
78
XCTAssertNil ( l, " Parsed double with extraneous space " )
You can’t perform that action at this time.
0 commit comments