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 { |
| 236 | 236 | |
| 237 | 237 | /// `string-to-number`: the leading number, else 0. |
| 238 | 238 | 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 |
| 242 | 240 | } |
| 243 | 241 | |
| 244 | 242 | /// `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 { |
| 26 | 26 | /// A number as `string-to-number` reads it, and whether it's an integer. |
| 27 | 27 | static func lispNumber(_ s: String) -> (value: Double, integer: Bool) { |
| 28 | 28 | 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) } |
| 31 | 34 | return (Double(text) ?? 0, text.firstMatch(of: /\.[0-9]|[eE]/) == nil) |
| 32 | 35 | } |
| 33 | 36 | |
| @@ -46,7 +49,9 @@ extension TablePlot { |
| 46 | 49 | /// `org--plot/values-stats`. |
| 47 | 50 | static func stats(_ nums: [(value: Double, integer: Bool)], hardMin: Double? = nil, hardMax: Double? = nil) |
| 48 | 51 | -> (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()!) |
| 50 | 55 | let range = high - low |
| 51 | 56 | guard range != 0 else { return (1, nums[0], nums[0]) } |
| 52 | 57 | let order = (1 - log10(range)).rounded(.up) |
Tests/OrgCoreTests/TablePlotTests.swift
+6
| @@ -213,6 +213,12 @@ struct TablePlotTests { |
| 213 | 213 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:100\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 214 | 214 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymax:0\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 215 | 215 | #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 | } |
| 216 | 222 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:99999999999999999999\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 217 | 223 | // gnuplot draws as many rings as it's given. |
| 218 | 224 | _ = try TablePlot.plot("#+PLOT: type:radar ticks:2000\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) |