-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* The `.` regex should not take the ASCII fast path see #375 for an example of undefined behavior because of this fast path. TLDR: the ASCII fast path will stop matching on the first matching byte, however this would split multi-byte codepoints. Combined with `Lexer::remaining` (or even just capturing the string like in the issue), this leads to non-utf8 strings escaping into user code. This is UNSOUND. * Add tests for unicode dot in both str and bytes * chore(lib): rewrite using ClassUnicode methods As suggested by @RustyYato * Revert "chore(lib): rewrite using ClassUnicode methods" This reverts commit 80bd23f. * try: fallback to previous impl Tests are still passing * try: add repetition check * chore(lib): cleanup code * fix and move * another fix --------- Co-authored-by: Jérome Eertmans <jeertmans@icloud.com>
- Loading branch information
Showing
2 changed files
with
81 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use logos::Logos as _; | ||
use logos_derive::Logos; | ||
|
||
#[derive(Logos, Debug, PartialEq)] | ||
enum TestUnicodeDot { | ||
#[regex(".")] | ||
Dot, | ||
} | ||
|
||
#[test] | ||
fn test_unicode_dot_str_ascii() { | ||
let mut lexer = TestUnicodeDot::lexer("a"); | ||
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDot::Dot))); | ||
assert_eq!(lexer.remainder(), ""); | ||
assert_eq!(lexer.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_unicode_dot_str_unicode() { | ||
let mut lexer = TestUnicodeDot::lexer(""); | ||
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDot::Dot))); | ||
assert_eq!(lexer.remainder(), ""); | ||
assert_eq!(lexer.next(), None); | ||
} | ||
|
||
#[derive(Logos, Debug, PartialEq)] | ||
enum TestUnicodeDotBytes { | ||
#[regex(".", priority = 100)] | ||
Dot, | ||
#[regex(b".", priority = 0)] | ||
InvalidUtf8, | ||
} | ||
|
||
#[test] | ||
fn test_unicode_dot_bytes_ascii() { | ||
let mut lexer = TestUnicodeDotBytes::lexer(b"a"); | ||
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDotBytes::Dot))); | ||
assert_eq!(lexer.remainder(), b""); | ||
assert_eq!(lexer.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_unicode_dot_bytes_unicode() { | ||
let mut lexer = TestUnicodeDotBytes::lexer("".as_bytes()); | ||
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDotBytes::Dot))); | ||
assert_eq!(lexer.remainder(), b""); | ||
assert_eq!(lexer.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_unicode_dot_bytes_invalid_utf8() { | ||
let mut lexer = TestUnicodeDotBytes::lexer(b"\xff"); | ||
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDotBytes::InvalidUtf8))); | ||
assert_eq!(lexer.remainder(), b""); | ||
assert_eq!(lexer.next(), None); | ||
} |