fix(config): match Zig's numeric grammar, NUL handling and diagnostics
Resolves the 6 differential gap groups in docs/rewrite/notes/review- rv9-codex.md (details in final-Z11.md): - ghostty_os::zig_fmt ports std.fmt.parseInt/parseUnsigned/parseFloat (underscores, 0x/0o/0b prefixes, hex floats, inf/nan) with the Zig std tests and replaces every int/float parse in config, keybind parameters, ssh-cache and desktop detection. - Interior NUL no longer panics in font names and ends C strings like Zig's backing bytes (fontconfig, C config getters, title actions, clipboard callback, relative paths, config-file, shaders, background image, GTK CSS). - GTK new-window arguments are trimmed of ASCII whitespace only. - Command tokenization uses a port of std.process.ArgIteratorGeneral (incl. CR/LF separators) with its std tests. - UTF-8 validation happens where Zig's field parsers do it, so error kinds match (metric modifiers, quick-terminal size, link, ...). A scripted differential run of 202 fields x ~220 hostile/valid values against /usr/bin/ghostty (+validate-config and +show-config: exit code, stdout, stderr) now has zero mismatches besides build-specific defaults (theme resources path, auto-update-channel, gtk-opengl-debug); it also surfaced and fixed background-blur failure state, NaN formatting and NaN/-0 finalize clamps. Regression tests: ghostty-config/tests/ zig_parity.rs. Test Plan: - cargo test --workspace --no-fail-fast (3456 passed, exit 0) - just check-macos (both), just check-windows, just check-wasm
This commit is contained in:
1 parent
7480f99d91
commit
79a7fd4f76
26 files changed
+1835
-228
No files matched your search
@@ -98,18 +98,23 @@ impl CConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// A C string without interior NULs.
|
||||
/// A C string over `bytes` that ends at the first NUL, like a C consumer
|
||||
/// reading Zig's backing bytes (the rest is never visible through the pointer).
|
||||
fn cstring_until_nul(bytes: &[u8]) -> CString {
|
||||
let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
|
||||
CString::new(&bytes[..end]).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// A C string for a UTF-8 string, ending at the first NUL.
|
||||
fn cstring(s: &str) -> CString {
|
||||
CString::new(s.replace('\0', "")).unwrap_or_default()
|
||||
cstring_until_nul(s.as_bytes())
|
||||
}
|
||||
|
||||
/// A C string for a config *value*: config strings carry non-UTF-8 bytes in
|
||||
/// the reversible [`ghostty_os::osbytes`] encoding, so the embedder gets the
|
||||
/// decoded bytes (Zig hands out the original byte slice).
|
||||
fn cstring_value(s: &str) -> CString {
|
||||
let mut bytes = ghostty_os::osbytes::config_str_to_bytes(s);
|
||||
bytes.retain(|b| *b != 0);
|
||||
CString::new(bytes).unwrap_or_default()
|
||||
cstring_until_nul(&ghostty_os::osbytes::config_str_to_bytes(s))
|
||||
}
|
||||
|
||||
/// Intern a string for the lifetime of the process (enum tag names).
|
||||
@@ -614,6 +619,23 @@ mod tests {
|
||||
assert_eq!(b.strings[0].to_str().unwrap(), "h\u{e9}llo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_string_values_end_at_the_first_interior_nul() {
|
||||
use ghostty_os::osbytes::EncodedOs;
|
||||
let enc = EncodedOs::from_bytes(b"a\0b");
|
||||
let b = build_backing(&CValue::String(enc.into_encoded()));
|
||||
assert_eq!(b.strings[0].as_bytes(), b"a");
|
||||
let b = build_backing(&CValue::String("x\0y".to_owned()));
|
||||
assert_eq!(b.strings[0].as_bytes(), b"x");
|
||||
let enc = EncodedOs::from_bytes(b"\0lead");
|
||||
let b = build_backing(&CValue::Path {
|
||||
path: enc.into_encoded(),
|
||||
optional: false,
|
||||
});
|
||||
assert_eq!(b.strings[0].as_bytes(), b"");
|
||||
assert_eq!(intern("ab\0c"), intern("ab"));
|
||||
}
|
||||
|
||||
fn get<T: Copy>(cfg: ghostty_config_t, key: &str, init: T) -> (bool, T) {
|
||||
let mut out = init;
|
||||
// SAFETY: `out` is a valid destination of the key's C type.
|
||||
|
||||
Reference in new issue
Block a user