Skip to content

Commit 59de3bc

Browse files
authored
Merge pull request #1930 from spevans/pr_sr_1674_42
[4.2] SR-1674: NSString.doubleValue doesn't support a few formats supported by Darwin
2 parents 0df0449 + 5e86787 commit 59de3bc

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Foundation/Scanner.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ extension String {
337337
buf.advance()
338338
buf.skip(skipSet)
339339
}
340-
if (!isADigit(buf.currentCharacter)) {
340+
if (buf.currentCharacter != ds && !isADigit(buf.currentCharacter)) {
341341
return false
342342
}
343343

@@ -377,6 +377,34 @@ extension String {
377377
buf.advance()
378378
} while (isADigit(buf.currentCharacter))
379379
}
380+
381+
if buf.currentCharacter == unichar(unicodeScalarLiteral: "e") || buf.currentCharacter == unichar(unicodeScalarLiteral: "E") {
382+
var exponent = Double(0)
383+
var negExponent = false
384+
buf.advance()
385+
if buf.currentCharacter == unichar(unicodeScalarLiteral: "-") || buf.currentCharacter == unichar(unicodeScalarLiteral: "+") {
386+
negExponent = buf.currentCharacter == unichar(unicodeScalarLiteral: "-")
387+
buf.advance()
388+
}
389+
repeat {
390+
let numeral = numericValue(buf.currentCharacter)
391+
buf.advance()
392+
if numeral == -1 {
393+
break
394+
}
395+
exponent *= 10
396+
exponent += Double(numeral)
397+
} while (isADigit(buf.currentCharacter))
398+
399+
if exponent > 0 {
400+
let multiplier = pow(10, exponent)
401+
if negExponent {
402+
localResult /= T(multiplier)
403+
} else {
404+
localResult *= T(multiplier)
405+
}
406+
}
407+
}
380408

381409
to(neg ? T(-1) * localResult : localResult)
382410
locationToScanFrom = buf.location

TestFoundation/TestNSString.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestNSString: LoopbackServerTest {
3636
("test_BridgeConstruction", test_BridgeConstruction ),
3737
("test_integerValue", test_integerValue ),
3838
("test_intValue", test_intValue ),
39+
("test_doubleValue", test_doubleValue),
3940
("test_isEqualToStringWithSwiftString", test_isEqualToStringWithSwiftString ),
4041
("test_isEqualToObjectWithNSString", test_isEqualToObjectWithNSString ),
4142
("test_isNotEqualToObjectWithNSNumber", test_isNotEqualToObjectWithNSNumber ),
@@ -252,6 +253,28 @@ class TestNSString: LoopbackServerTest {
252253
let string10: NSString = "-999999999999999999999999999999"
253254
XCTAssertEqual(string10.intValue, Int32.min)
254255
}
256+
257+
func test_doubleValue() {
258+
XCTAssertEqual(NSString(string: ".2").doubleValue, 0.2)
259+
XCTAssertEqual(NSString(string: "+.2").doubleValue, 0.2)
260+
XCTAssertEqual(NSString(string: "-.2").doubleValue, -0.2)
261+
XCTAssertEqual(NSString(string: "1.23015e+3").doubleValue, 1230.15)
262+
XCTAssertEqual(NSString(string: "12.3015e+02").doubleValue, 1230.15)
263+
XCTAssertEqual(NSString(string: "+1.23015e+3").doubleValue, 1230.15)
264+
XCTAssertEqual(NSString(string: "+12.3015e+02").doubleValue, 1230.15)
265+
XCTAssertEqual(NSString(string: "-1.23015e+3").doubleValue, -1230.15)
266+
XCTAssertEqual(NSString(string: "-12.3015e+02").doubleValue, -1230.15)
267+
XCTAssertEqual(NSString(string: "-12.3015e02").doubleValue, -1230.15)
268+
XCTAssertEqual(NSString(string: "-31.25e-04").doubleValue, -0.003125)
269+
270+
XCTAssertEqual(NSString(string: ".e12").doubleValue, 0)
271+
XCTAssertEqual(NSString(string: "2e3.12").doubleValue, 2000)
272+
XCTAssertEqual(NSString(string: "1e2.3").doubleValue, 100)
273+
XCTAssertEqual(NSString(string: "12.e4").doubleValue, 120000)
274+
XCTAssertEqual(NSString(string: "1.2.3.4").doubleValue, 1.2)
275+
XCTAssertEqual(NSString(string: "1e2.3").doubleValue, 100)
276+
XCTAssertEqual(NSString(string: "1E3").doubleValue, 1000)
277+
}
255278

256279
func test_isEqualToStringWithSwiftString() {
257280
let string: NSString = "literal"

0 commit comments

Comments
 (0)