Skip to content

Commit

Permalink
Dot followed by digits is now treated as a number
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Dec 14, 2017
1 parent 06ed90f commit 08fef32
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
46 changes: 31 additions & 15 deletions Sources/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1060,24 +1060,40 @@ private extension UnicodeScalarView {
return nil
}

guard var number = scanInteger() else {
return nil
}

let endOfInt = self
if scanCharacter(".") {
if let fraction = scanInteger() {
number += "." + fraction + (scanExponent() ?? "")
func scanNumber() -> String? {
var number: String
var endOfInt = self
if let integer = scanInteger() {
if integer == "0", scanCharacter("x") {
return "0x\(scanHex() ?? "")"
}
endOfInt = self
if scanCharacter(".") {
guard let fraction = scanInteger() else {
self = endOfInt
return integer
}
number = "\(integer).\(fraction)"
} else {
number = integer
}
} else if scanCharacter(".") {
guard let fraction = scanInteger() else {
self = endOfInt
return nil
}
number = ".\(fraction)"
} else {
self = endOfInt
return nil
}
number += scanExponent() ?? ""
} else if let exponent = scanExponent() {
number += exponent
} else if number == "0" {
if scanCharacter("x") {
number = "0x\(scanHex() ?? "")"
if let exponent = scanExponent() {
number += exponent
}
return number
}

guard let number = scanNumber() else {
return nil
}
guard let value = Double(number) else {
return .error(.unexpectedToken(number), number)
Expand Down
5 changes: 5 additions & 0 deletions Tests/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ class ExpressionTests: XCTestCase {
XCTAssertEqual(try expression.evaluate(), -0.34)
}

func testLeadingDecimalPoint() {
let expression = Expression(".5")
XCTAssertEqual(try expression.evaluate(), 0.5)
}

func testExponential() {
let expression = Expression("1234e5")
XCTAssertEqual(try expression.evaluate(), 1234e5)
Expand Down

0 comments on commit 08fef32

Please sign in to comment.