Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Fix Double fraction parsing #59

Merged
merged 2 commits into from
Dec 4, 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
15 changes: 7 additions & 8 deletions Sources/Jay/NumberParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct NumberParser: JsonParser {
// - continue to the frac part right away
// - if it's 1...9, continue to parsing integer part first
let integer: Int
let frac: Int
let frac: String
if reader.curr() == Const.Zero {
try reader.nextAndCheckNotDone()

Expand All @@ -57,7 +57,7 @@ struct NumberParser: JsonParser {
if reader.curr() == Const.DecimalPoint {
frac = try self.parseFrac(reader)
} else {
frac = 0
frac = ""
}
}

Expand All @@ -70,15 +70,15 @@ struct NumberParser: JsonParser {
return value
}

private func generateNumber(negative: Bool, integer: Int, frac: Int, exp: Int?) -> JSON.Number {
private func generateNumber(negative: Bool, integer: Int, frac: String, exp: Int?) -> JSON.Number {

//form the int section
var int = integer
let sign = negative ? -1 : 1
int *= sign

//if that's it, let's call it an integer
if frac == 0 && exp == nil {
if frac.isEmpty && exp == nil {
if negative {
//if it's negative, make it a signed integer
return .integer(int)
Expand All @@ -92,7 +92,7 @@ struct NumberParser: JsonParser {
var dbl = Double(int)

//if it's a frac, append the fraction section
if frac > 0 {
if !frac.isEmpty {
//double
let fracAdd = Double("0.\(frac)")! * Double(sign)
dbl += fracAdd
Expand Down Expand Up @@ -162,7 +162,7 @@ struct NumberParser: JsonParser {
}
}

private func parseFrac<R: Reader>(_ reader: R) throws -> Int {
private func parseFrac<R: Reader>(_ reader: R) throws -> String {

//frac part MUST start with decimal point!
guard reader.curr() == Const.DecimalPoint else {
Expand Down Expand Up @@ -200,8 +200,7 @@ struct NumberParser: JsonParser {
if fracTerm.contains(reader.curr()) {
//gracefully end this section
let fracString = try digs.string()
let frac = Int(fracString)!
return frac
return fracString
}

//okay, we encountered an illegal character, error out
Expand Down
7 changes: 7 additions & 0 deletions Tests/JayTests/ParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extension ParsingTests {
("testNumber_Int_Negative", testNumber_Int_Negative),
("testNumber_Dbl_Basic", testNumber_Dbl_Basic),
("testNumber_Dbl_ZeroSomething", testNumber_Dbl_ZeroSomething),
("testNumber_Dbl_ZeroZeroSomething", testNumber_Dbl_ZeroZeroSomething),
("testNumber_Dbl_MinusZeroSomething", testNumber_Dbl_MinusZeroSomething),
("testNumber_Dbl_Incomplete", testNumber_Dbl_Incomplete),
("testNumber_Dbl_Negative", testNumber_Dbl_Negative),
Expand Down Expand Up @@ -329,6 +330,12 @@ class ParsingTests:XCTestCase {
XCTAssertNil(ret)
}

func testNumber_Dbl_ZeroZeroSomething() {
let reader = ByteReader(content: "0.00034]")
let ret = try! ValueParser().parse(with: reader)
ensureNumber(ret, exp: JSON.Number.double(0.00034))
}

func testNumber_Double_Exp_Normal() {
let reader = ByteReader(content: "-24.3245e2, ")
let ret = try! ValueParser().parse(with: reader)
Expand Down