From 912a2d0b15b873923698126ac8112d1aa4bdbb1a Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Tue, 27 Oct 2026 10:08:22 +0100 Subject: wasm: experimental browser spike Proves the gipfel core can run in the browser via WASM. Enabled by the earlier GSL removal: the core is now pure Rust. gipfel-core gains byte-oriented entry points (no filesystem) alongside the existing path-based ones: - hill::load_from_bytes, Panorama::load_data_from_bytes - metadata: load_image_from_bytes, read_jpeg_comment_from_bytes, write_jpeg_comment_to_bytes, save_image_to_bytes (EXIF via Cursor) New crate gipfel-wasm (experimental, not shipped with the app): - wasm-bindgen surface: version() + fit_demo(db_bytes, jpg_bytes) which runs the whole pipeline in memory: parse hill DB, read gipfel JPEG comment / EXIF, synthesize 5 observations from visible hills, perturb, refit (pure-Rust LM), report JSON with reprojection error - demo/index.html: file pickers + result pane (wasm-pack --target web) - build.sh: adds the wasm target, builds with wasm-pack, prints serve cmd - native test runs the same pipeline against gipfel.dat + test.jpg and asserts sub-pixel reprojection error (passes here) To try on a machine with rustup: cd crates/gipfel-wasm && ./build.sh --- crates/gipfel-core/src/hill.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/gipfel-core/src/hill.rs') diff --git a/crates/gipfel-core/src/hill.rs b/crates/gipfel-core/src/hill.rs index a904948..4ae0f89 100644 --- a/crates/gipfel-core/src/hill.rs +++ b/crates/gipfel-core/src/hill.rs @@ -2,9 +2,6 @@ //! //! Faithful port of gipfel's `Hill.cxx`. -use std::fs::File; -use std::io::{BufRead, BufReader}; - pub const FLAG_DUPLIC: u32 = 1 << 0; pub const FLAG_KNOWN: u32 = 1 << 1; pub const FLAG_HIDDEN: u32 = 1 << 2; @@ -77,16 +74,20 @@ fn atof(s: &str) -> f64 { 0.0 } -/// Load a hill database. Two line formats are accepted: +/// Load a hill database from a file path. +pub fn load(path: &str) -> std::io::Result> { + load_from_bytes(&std::fs::read(path)?) +} + +/// Load a hill database from raw bytes. Two line formats are accepted: /// - `SHORT,NAME,DESC,LAT,LON,HEIGHT` (6 fields) /// - `LAT,LON,HEIGHT` (3 fields, track point) -pub fn load(path: &str) -> std::io::Result> { - let file = File::open(path)?; +/// +/// Byte-oriented entry point (no filesystem): used by the WASM build and +/// by tests. The database is Latin-1; bytes decode to Unicode code points. +pub fn load_from_bytes(raw: &[u8]) -> std::io::Result> { let mut hills = Vec::new(); - - // the database is Latin-1; decode to Unicode code points - for line in BufReader::new(file).split(b'\n') { - let raw = line?; + for raw in raw.split(|b| *b == b'\n') { let line: String = raw.iter().map(|b| *b as char).collect(); // strsep() semantics: split into at most 10 fields, keeping empties let vals: Vec<&str> = line.split(',').take(10).collect(); @@ -109,7 +110,6 @@ pub fn load(path: &str) -> std::io::Result> { hills.push(h); } } - Ok(hills) } -- cgit v1.2.3 From f0cf0c3e95ca0518de754f7915458bd90e8abf32 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Wed, 28 Oct 2026 09:23:55 +0100 Subject: core: guard atof prefix slicing against char boundaries The longest-numeric-prefix fallback sliced s[..i] at every byte index, which panics when i lands inside a multi-byte character (reachable via lossy-decoded Latin-1 database fields, e.g. a trailing degree sign). Skip non-boundary indices; behavior on ASCII numeric fields is unchanged. Includes regression test atof("12.5 deg"). --- crates/gipfel-core/src/hill.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'crates/gipfel-core/src/hill.rs') diff --git a/crates/gipfel-core/src/hill.rs b/crates/gipfel-core/src/hill.rs index 4ae0f89..254a9e4 100644 --- a/crates/gipfel-core/src/hill.rs +++ b/crates/gipfel-core/src/hill.rs @@ -67,6 +67,9 @@ fn atof(s: &str) -> f64 { return v; } for i in (1..s.len()).rev() { + if !s.is_char_boundary(i) { + continue; + } if let Ok(v) = s[..i].parse::() { return v; } @@ -163,6 +166,8 @@ mod tests { assert_eq!(atof("abc"), 0.0); assert_eq!(atof("12abc"), 12.0); assert_eq!(atof(" -3.5x"), -3.5); + // prefix cut must not slice through a multi-byte character + assert_eq!(atof("12.5\u{b0}"), 12.5); } #[test] -- cgit v1.2.3