Commit 8ea52e8b2c

8ea52e8b2ce45ee5afae5a6c9d7e66dac1310cf6

parent: 6b184db05d

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 12:18 UTC

Read 1.0e+INF and 1.0e+NaN as string-to-number does, and refuse such radar rows

Layout: unified · split

Sources/OrgCore/Compute/TablePlot.swift +1 −3
@@ -236,9 +236,7 @@ public enum TablePlot {
236236
237237 /// `string-to-number`: the leading number, else 0.
238238 static func stringToNumber(_ s: String) -> Double {
239 let trimmed = s.drop { $0 == " " || $0 == "\t" }
240 guard let m = trimmed.firstMatch(of: /^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?/) else { return 0 }
241 return Double(m.0) ?? 0
239 lispNumber(s).value
242240 }
243241
244242 /// `org-plot/gnuplot-script`, with `org-plot/gnuplot-term-extra` and the preamble empty.
Sources/OrgCore/Compute/TablePlotRadar.swift +8 −3
@@ -26,8 +26,11 @@ extension TablePlot {
2626 /// A number as `string-to-number` reads it, and whether it's an integer.
2727 static func lispNumber(_ s: String) -> (value: Double, integer: Bool) {
2828 let trimmed = s.drop { $0 == " " || $0 == "\t" }
29 guard let m = trimmed.firstMatch(of: /^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?/) else { return (0, true) }
30 let text = String(m.0)
29 guard let m = trimmed.firstMatch(of: /^([-+]?)([0-9]+\.?[0-9]*|\.[0-9]+)([eE]([-+]?[0-9]+|\+INF|\+NaN))?/) else { return (0, true) }
30 let text = String(m.0), sign: Double = m.1 == "-" ? -1 : 1
31 // The reader's `1.0e+INF` and `1.0e+NaN`.
32 if text.hasSuffix("INF") { return (sign * .infinity, false) }
33 if text.hasSuffix("NaN") { return (sign * .nan, false) }
3134 return (Double(text) ?? 0, text.firstMatch(of: /\.[0-9]|[eE]/) == nil)
3235 }
3336
@@ -46,7 +49,9 @@ extension TablePlot {
4649 /// `org--plot/values-stats`.
4750 static func stats(_ nums: [(value: Double, integer: Bool)], hardMin: Double? = nil, hardMax: Double? = nil)
4851 -> (factor: Double, niceMin: (value: Double, integer: Bool), niceMax: (value: Double, integer: Bool)) {
49 let low = hardMin ?? nums.map(\.value).min()!, high = hardMax ?? nums.map(\.value).max()!
52 // Emacs's `min` and `max` return a NaN argument.
53 let values = nums.map(\.value), nan = values.contains { $0.isNaN }
54 let low = hardMin ?? (nan ? .nan : values.min()!), high = hardMax ?? (nan ? .nan : values.max()!)
5055 let range = high - low
5156 guard range != 0 else { return (1, nums[0], nums[0]) }
5257 let order = (1 - log10(range)).rounded(.up)
Tests/OrgCoreTests/TablePlotTests.swift +6
@@ -213,6 +213,12 @@ struct TablePlotTests {
213213 #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:100\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) }
214214 #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymax:0\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) }
215215 #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:-1\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) }
216 // string-to-number reads the reader's infinities and NaNs; Emacs then signals overflow-error.
217 #expect(TablePlot.lispNumber("-2.0e+INF").value == -.infinity && TablePlot.lispNumber("1e+NaN").value.isNaN)
218 #expect(TablePlot.lispNumber("1.0e-INF").value == 1 && TablePlot.lispNumber("1.0e+inf").value == 1 && TablePlot.stringToNumber(".5e+INFx") == .infinity)
219 for field in ["1.0e+INF", "1.0e+NaN"] {
220 #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:4\n| a | 1 | \(field) |\n| b | 2 | 5 |\n", at: 0) }
221 }
216222 #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:99999999999999999999\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) }
217223 // gnuplot draws as many rings as it's given.
218224 _ = try TablePlot.plot("#+PLOT: type:radar ticks:2000\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0)