fix: use Zig's numeric parsing in protocol and escape-sequence parsers
Rust's str::parse accepts and rejects different inputs than Zig's std.fmt.parseInt/parseFloat (underscores, base prefixes, signs). The config layer already used the Zig ports; now the protocol parsers do too (call site -> Zig reference -> status in final-Z12.md): - zig_fmt moves into the leaf crate ghostty-zigfmt (ghostty-vt cannot depend on ghostty-os); ghostty-os re-exports it unchanged. - ghostty-vt: kitty graphics keys, kitty text sizing, OSC 4/5/10-19 color indices, hex color components and intensities, OSC 9 sleep and progress, OSC 133 exit codes, OSC 3008 fields, kitty color palette. - ghostty-terminal: tmux layout, format variables and pane tabs. - ghostty-os: URI ports and %XX decoding; ghostty-bench CLI arguments. Expected values were cross-checked against the real Zig toolchain. Test Plan: - cargo test --workspace --no-fail-fast - just check-macos (both), just check-windows, just check-wasm
This commit is contained in:
1 parent
ecd67c8f3e
commit
16eb1fba61
22 files changed
+290
-186
No files matched your search
@@ -141,30 +141,10 @@ fn tokenize(buf: &[u8]) -> impl Iterator<Item = &[u8]> {
|
||||
buf.split(|&c| c == b';').filter(|t| !t.is_empty())
|
||||
}
|
||||
|
||||
/// Zig `std.fmt.parseInt(u9, s, 10)`: optional sign, digits only.
|
||||
/// Zig `std.fmt.parseInt(u9, s, 10)`.
|
||||
fn parse_u9(s: &[u8]) -> Option<u16> {
|
||||
let (neg, digits) = match s.first()? {
|
||||
b'+' => (false, &s[1..]),
|
||||
b'-' => (true, &s[1..]),
|
||||
_ => (false, s),
|
||||
};
|
||||
if digits.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut acc: u32 = 0;
|
||||
for &c in digits {
|
||||
if !c.is_ascii_digit() {
|
||||
return None;
|
||||
}
|
||||
acc = acc * 10 + (c - b'0') as u32;
|
||||
if acc > 511 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if neg && acc != 0 {
|
||||
return None;
|
||||
}
|
||||
Some(acc as u16)
|
||||
let v = ghostty_zigfmt::parse_int::<i32>(s, 10).ok()?;
|
||||
u16::try_from(v).ok().filter(|v| *v <= 511)
|
||||
}
|
||||
|
||||
/// Parse any color operation string. This should NOT include the operation
|
||||
@@ -542,6 +522,21 @@ mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zig_parse_u9_differences() {
|
||||
assert_eq!(Some(10), parse_u9(b"1_0"));
|
||||
assert_eq!(Some(500), parse_u9(b"5_0_0"));
|
||||
assert_eq!(Some(5), parse_u9(b"+5"));
|
||||
assert_eq!(Some(0), parse_u9(b"-0"));
|
||||
assert_eq!(None, parse_u9(b"-1"));
|
||||
assert_eq!(None, parse_u9(b"0x10"));
|
||||
assert_eq!(None, parse_u9(b"1_"));
|
||||
assert_eq!(None, parse_u9(b"_1"));
|
||||
assert_eq!(Some(511), parse_u9(b"511"));
|
||||
assert_eq!(None, parse_u9(b"512"));
|
||||
assert_eq!(None, parse_u9(b"99999999999"));
|
||||
}
|
||||
|
||||
const RED: Rgb = Rgb { r: 255, g: 0, b: 0 };
|
||||
const BLUE: Rgb = Rgb { r: 0, g: 0, b: 255 };
|
||||
|
||||
|
||||
@@ -216,9 +216,8 @@ impl Field {
|
||||
if value.iter().any(|c| !c.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
std::str::from_utf8(value)
|
||||
ghostty_zigfmt::parse_int::<u64>(value, 10)
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<u64>().ok())
|
||||
.map(FieldValue::U64)
|
||||
}
|
||||
// String fields
|
||||
|
||||
@@ -67,28 +67,8 @@ enum UpdateError {
|
||||
|
||||
/// Equivalent of Zig `std.fmt.parseInt(u4, value, 10)`.
|
||||
fn parse_u4(value: &[u8]) -> Option<u8> {
|
||||
let (neg, digits) = match value.first() {
|
||||
Some(b'+') => (false, &value[1..]),
|
||||
Some(b'-') => (true, &value[1..]),
|
||||
_ => (false, value),
|
||||
};
|
||||
if digits.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut acc: u32 = 0;
|
||||
for &c in digits {
|
||||
if !c.is_ascii_digit() {
|
||||
return None;
|
||||
}
|
||||
acc = acc * 10 + (c - b'0') as u32;
|
||||
if acc > 15 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if neg && acc != 0 {
|
||||
return None;
|
||||
}
|
||||
Some(acc as u8)
|
||||
let v = ghostty_zigfmt::parse_int::<i32>(value, 10).ok()?;
|
||||
u8::try_from(v).ok().filter(|v| *v <= 15)
|
||||
}
|
||||
|
||||
impl OSC<'_> {
|
||||
@@ -213,6 +193,20 @@ pub fn parse<S: ByteStorage>(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zig_parse_u4_differences() {
|
||||
assert_eq!(Some(10), parse_u4(b"1_0"));
|
||||
assert_eq!(Some(5), parse_u4(b"+5"));
|
||||
assert_eq!(Some(0), parse_u4(b"-0"));
|
||||
assert_eq!(Some(15), parse_u4(b"15"));
|
||||
assert_eq!(None, parse_u4(b"16"));
|
||||
assert_eq!(None, parse_u4(b"5_0_0"));
|
||||
assert_eq!(None, parse_u4(b"-1"));
|
||||
assert_eq!(None, parse_u4(b"0x1"));
|
||||
assert_eq!(None, parse_u4(b"1_"));
|
||||
assert_eq!(None, parse_u4(b"99999999999"));
|
||||
}
|
||||
|
||||
fn run(input: &[u8]) -> Option<Command<'static>> {
|
||||
let mut p = Parser::init(false);
|
||||
for &ch in input {
|
||||
|
||||
@@ -12,8 +12,8 @@ use crate::osc::{
|
||||
};
|
||||
|
||||
/// Parse a base-10 unsigned integer from raw bytes (Zig `parseUnsigned`).
|
||||
fn parse_unsigned<T: std::str::FromStr>(bytes: &[u8]) -> Option<T> {
|
||||
std::str::from_utf8(bytes).ok()?.parse().ok()
|
||||
fn parse_unsigned<T: ghostty_zigfmt::ZigInt>(bytes: &[u8]) -> Option<T> {
|
||||
ghostty_zigfmt::parse_unsigned::<T>(bytes, 10).ok()
|
||||
}
|
||||
|
||||
/// Parse OSC 9, which could be an iTerm2 notification or a ConEmu extension.
|
||||
@@ -221,6 +221,18 @@ fn conemu_inner(data: &[u8]) -> Option<Command<'_>> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zig_parse_unsigned_differences() {
|
||||
assert_eq!(run("9;1;4_2"), Command::ConemuSleep { duration_ms: 42 });
|
||||
assert_eq!(run("9;1;+42"), Command::ConemuSleep { duration_ms: 100 });
|
||||
assert_eq!(run("9;1;-0"), Command::ConemuSleep { duration_ms: 100 });
|
||||
assert_eq!(run("9;1;0x10"), Command::ConemuSleep { duration_ms: 100 });
|
||||
assert_eq!(run("9;1;70000"), Command::ConemuSleep { duration_ms: 100 });
|
||||
assert_eq!(parse_unsigned::<usize>(b"5_0"), Some(50));
|
||||
assert_eq!(parse_unsigned::<usize>(b"+5"), None);
|
||||
assert_eq!(parse_unsigned::<usize>(b"5_"), None);
|
||||
}
|
||||
|
||||
fn run(input: &str) -> Command<'static> {
|
||||
let mut p = Parser::init(false);
|
||||
for ch in input.bytes() {
|
||||
|
||||
@@ -223,9 +223,8 @@ impl OptionKey {
|
||||
// If we're looking for exit_code we special case it.
|
||||
// as the first value.
|
||||
if self == OptionKey::ExitCode {
|
||||
return std::str::from_utf8(full)
|
||||
return ghostty_zigfmt::parse_int::<i32>(full, 10)
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<i32>().ok())
|
||||
.map(OptionValue::Int);
|
||||
}
|
||||
|
||||
@@ -428,6 +427,16 @@ pub fn parse<S: ByteStorage>(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zig_parse_int_exit_code() {
|
||||
assert_eq!(run("133;D;1_0").unwrap().exit_code(), Some(10));
|
||||
assert_eq!(run("133;D;+5").unwrap().exit_code(), Some(5));
|
||||
assert_eq!(run("133;D;-5").unwrap().exit_code(), Some(-5));
|
||||
assert_eq!(run("133;D;0x10").unwrap().exit_code(), None);
|
||||
assert_eq!(run("133;D;1_").unwrap().exit_code(), None);
|
||||
assert_eq!(run("133;D;99999999999").unwrap().exit_code(), None);
|
||||
}
|
||||
|
||||
fn run(input: &str) -> Option<Command<'static>> {
|
||||
let mut p = Parser::init(false);
|
||||
for ch in input.bytes() {
|
||||
|
||||
Reference in new issue
Block a user