From 5d4cfab739a1567f49a71c421647370271da220f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 19 Jan 2026 07:45:01 -0600 Subject: [PATCH 1/4] test(cli): Show hex literal issue --- .../tests/cmd/false-positives.in/sample.cpp | 2 +- crates/typos-cli/tests/cmd/false-positives.toml | 9 ++++++++- crates/typos/src/tokens.rs | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/typos-cli/tests/cmd/false-positives.in/sample.cpp b/crates/typos-cli/tests/cmd/false-positives.in/sample.cpp index 40455b15..7cd246b9 100644 --- a/crates/typos-cli/tests/cmd/false-positives.in/sample.cpp +++ b/crates/typos-cli/tests/cmd/false-positives.in/sample.cpp @@ -1,5 +1,5 @@ int main() { - uint8 i = 0; + uint8 i = 0x1afe23456UL; std::countr_one(i); return 0; } diff --git a/crates/typos-cli/tests/cmd/false-positives.toml b/crates/typos-cli/tests/cmd/false-positives.toml index 324f41d7..276faf9a 100644 --- a/crates/typos-cli/tests/cmd/false-positives.toml +++ b/crates/typos-cli/tests/cmd/false-positives.toml @@ -1,4 +1,11 @@ bin.name = "typos" stdin = "" -stdout = "" +stdout = """ +error: `afe` should be `safe` + ╭▸ ./sample.cpp:2:18 + │ +2 │ uint8 i = 0x1afe23456UL; + ╰╴ ━━━ +""" stderr = "" +status.code = 2 diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index 43213112..dc368f57 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -1260,7 +1260,7 @@ mod test { fn tokenize_ignore_hex() { let parser = TokenizerBuilder::new().build(); - let input = "Hello 0xDEADBEEF World"; + let input = "Hello 0xDEADBEEF 0x1afe23456UL World"; let actual: Vec<_> = parser.parse_bytes(input.as_bytes()).collect(); assert_data_eq!( actual.to_debug(), @@ -1272,10 +1272,15 @@ mod test { offset: 0, }, Identifier { - token: "World", + token: "0x1afe23456UL", case: None, offset: 17, }, + Identifier { + token: "World", + case: None, + offset: 31, + }, ] "#]] @@ -1291,10 +1296,15 @@ mod test { offset: 0, }, Identifier { - token: "World", + token: "0x1afe23456UL", case: None, offset: 17, }, + Identifier { + token: "World", + case: None, + offset: 31, + }, ] "#]] From 01955c0f2a615c6f18fecd48a83fce416f520c38 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 19 Jan 2026 07:50:11 -0600 Subject: [PATCH 2/4] perf(token): Prefer slices over characters --- crates/typos/src/tokens.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index dc368f57..0ce6f187 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -146,8 +146,9 @@ mod parser { /// later may cause it to fail. const NON_TERMINATING_CAP: usize = 1024; - pub(crate) fn next_identifier(input: &mut T) -> Result<::Slice, ()> + pub(crate) fn next_identifier<'i, T>(input: &mut T) -> Result<::Slice, ()> where + T: Compare<&'i str>, T: Compare, T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -174,8 +175,9 @@ mod parser { .parse_next(input) } - fn ignore(input: &mut T) -> Result<::Slice, ()> + fn ignore<'i, T>(input: &mut T) -> Result<::Slice, ()> where + T: Compare<&'i str>, T: Compare, T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -236,9 +238,10 @@ mod parser { .parse_next(input) } - fn ordinal_literal(input: &mut T) -> Result<::Slice, ()> + fn ordinal_literal<'i, T>(input: &mut T) -> Result<::Slice, ()> where T: Compare, + T: Compare<&'i str>, T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, ::Token: AsChar + Copy, @@ -254,7 +257,7 @@ mod parser { ( take_while(0.., is_sep), take_while(1.., is_dec_digit), - alt((('s', 't'), ('n', 'd'), ('r', 'd'), ('t', 'h'))), + alt((("st"), ("nd"), ("rd"), ("th"))), take_while(0.., is_sep), ) .take(), @@ -304,9 +307,10 @@ mod parser { .parse_next(input) } - fn jwt(input: &mut T) -> Result<::Slice, ()> + fn jwt<'i, T>(input: &mut T) -> Result<::Slice, ()> where T: Compare, + T: Compare<&'i str>, T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, ::Token: AsChar + Copy, @@ -314,12 +318,9 @@ mod parser { trace( "jwt", ( - 'e', - 'y', + "ey", take_while(20.., is_jwt_token), - '.', - 'e', - 'y', + ".ey", take_while(20.., is_jwt_token), '.', take_while(20.., is_jwt_token), @@ -459,9 +460,10 @@ mod parser { .parse_next(input) } - fn url_literal(input: &mut T) -> Result<::Slice, ()> + fn url_literal<'i, T>(input: &mut T) -> Result<::Slice, ()> where T: Compare, + T: Compare<&'i str>, T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, ::Token: AsChar + Copy, @@ -473,7 +475,7 @@ mod parser { take_while(1..NON_TERMINATING_CAP, is_scheme_char), // HACK: Technically you can skip `//` if you don't have a domain but that would // get messy to support. - (':', '/', '/'), + ("://"), )), ( opt((url_userinfo, '@')), From 7300bb0964cb8a525429adbde59c6a4eeb27099e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 19 Jan 2026 07:54:10 -0600 Subject: [PATCH 3/4] perf(token): Avoid switching to chars --- crates/typos/src/tokens.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index 0ce6f187..a87a4834 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -423,10 +423,7 @@ mod parser { if captured.slice_len() < 90 && padding_len == 0 - && captured - .as_bstr() - .iter() - .all(|c| !['/', '+'].contains(&c.as_char())) + && captured.as_bstr().iter().all(|c| ![b'/', b'+'].contains(c)) { #[allow(clippy::unit_arg)] return Err(ParserError::from_input(input)); From 207157952c1c5bb2f91d933d56924dcb674cd919 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 19 Jan 2026 08:07:22 -0600 Subject: [PATCH 4/4] fix(tokens): Ignore hex literals with suffixes Fixes #1469 --- .../typos-cli/tests/cmd/false-positives.toml | 9 +-------- crates/typos/src/tokens.rs | 18 +++++++----------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/crates/typos-cli/tests/cmd/false-positives.toml b/crates/typos-cli/tests/cmd/false-positives.toml index 276faf9a..324f41d7 100644 --- a/crates/typos-cli/tests/cmd/false-positives.toml +++ b/crates/typos-cli/tests/cmd/false-positives.toml @@ -1,11 +1,4 @@ bin.name = "typos" stdin = "" -stdout = """ -error: `afe` should be `safe` - ╭▸ ./sample.cpp:2:18 - │ -2 │ uint8 i = 0x1afe23456UL; - ╰╴ ━━━ -""" +stdout = "" stderr = "" -status.code = 2 diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index a87a4834..19f5326a 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -281,7 +281,13 @@ mod parser { ::Slice: AsBStr + SliceLen + Default, ::Token: AsChar + Copy, { - ('0', alt(('x', 'X')), take_while(1.., is_hex_digit_with_sep)) + ( + '0', + alt(('x', 'X')), + take_while(1.., is_hex_digit_with_sep), + take_while(0.., is_xid_continue) + .verify(|s: &::Slice| std::str::from_utf8(s.as_bstr()).is_ok()), + ) .take() .parse_next(input) } @@ -1270,11 +1276,6 @@ mod test { case: None, offset: 0, }, - Identifier { - token: "0x1afe23456UL", - case: None, - offset: 17, - }, Identifier { token: "World", case: None, @@ -1294,11 +1295,6 @@ mod test { case: None, offset: 0, }, - Identifier { - token: "0x1afe23456UL", - case: None, - offset: 17, - }, Identifier { token: "World", case: None,