Skip to content
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

Numberformatter uses locale, decimal separator , is used which causes fatal error #10

Open
doozMen opened this issue Sep 23, 2022 · 0 comments

Comments

@doozMen
Copy link

doozMen commented Sep 23, 2022

The code

private let numberFormatter = NumberFormatter()
private let doublePrefixes: Set<String> = Set((0...9).map { "\($0)." } + (0...9).map { "\(-$0)." })

public struct DoubleLiteral: Parser {
    public static let kind: Kind = .doubleLiteral

    public init() { }

    public var body: AnyParser<Double> {
        Leaf {
            RegularExpression("-?\\d+(,\\d+)*\\.\\d+(e((-|\\+)?)\\d+)?\\b").map { match -> Double in
                guard let number = numberFormatter.number(from: String(match.text)) else { fatalError() }
                return number.doubleValue
            }
        }
        .optimize(using: doublePrefixes)
    }
}

can work with small adjustment

private let numberFormatter: NumberFormatter = {
  let formatter = NumberFormatter()
  formatter.decimalSeparator = "."
  return formatter
}()

But instead of using NSNumberFormatter is it not more performant to just use Double(String(match.text))? Added bonus this always works with decimal separator .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant