Skip to content

Truly independent web browser

AK: Add is_ascii_digit() and GenericLexer::tell()/retreat() to Rust

Shannon Booth8766e10fd66e
diff --git a/AK/Rust/src/character_types.rs b/AK/Rust/src/character_types.rs
new file mode 100644
index 00000000000..b550ff20886
--- /dev/null
+++ b/AK/Rust/src/character_types.rs
@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2021, Max Wipfli <[email protected]>
+ * Copyright (c) 2022, the SerenityOS developers.
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+pub const fn is_ascii_digit(code_point: u32) -> bool {
+ matches!(char::from_u32(code_point), Some('0'..='9'))
+}
diff --git a/AK/Rust/src/generic_lexer.rs b/AK/Rust/src/generic_lexer.rs
index f22deabdb2a..1e3be7386a8 100644
--- a/AK/Rust/src/generic_lexer.rs
+++ b/AK/Rust/src/generic_lexer.rs
@@ -14,6 +14,10 @@ impl<'a, T: Copy + PartialEq> GenericLexer<'a, T> {
Self { input, index: 0 }
}
+ pub fn tell(&self) -> usize {
+ self.index
+ }
+
pub fn is_eof(&self) -> bool {
self.index >= self.input.len()
}
@@ -44,6 +48,11 @@ impl<'a, T: Copy + PartialEq> GenericLexer<'a, T> {
&self.input[start..self.index]
}
+ pub fn retreat(&mut self, count: usize) {
+ assert!(self.index >= count, "cannot retreat past the start of the input");
+ self.index -= count;
+ }
+
pub fn ignore(&mut self, count: usize) {
self.index = self.input.len().min(self.index + count);
}
diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rs
index c449c53c608..27c885880e3 100644
--- a/AK/Rust/src/lib.rs
+++ b/AK/Rust/src/lib.rs
@@ -6,6 +6,7 @@
//! Rust views of shared AK data structures.
+pub mod character_types;
mod column;
mod fast_hash;
mod generic_lexer;