Review: reuse the importer's find and number helpers (#1208)

This commit is contained in:
bflatastic
2026-10-11 22:17:39 +07:00
parent 996111b179
commit 66fd1b78a2
3 changed files with 5 additions and 10 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+3 -8
View File
@@ -12,6 +12,9 @@ use std::collections::HashMap;
use kurbo::{BezPath, Point};
use super::find;
use super::lex::number;
/// The most text objects read.
const MAX_OBJECTS: usize = 100_000;
/// How far past `/AI11Text :` an object's `/Art` path may end (bytes).
@@ -48,19 +51,11 @@ pub(crate) fn text_frames(data: &[u8]) -> HashMap<(u32, usize), BezPath> {
out
}
fn find(hay: &[u8], needle: &[u8]) -> Option<usize> {
hay.windows(needle.len()).position(|w| w == needle)
}
/// The words of `text`, without the `%_` that leads lines inside dictionaries.
fn words(text: &[u8]) -> impl Iterator<Item = &[u8]> {
text.split(|b| b.is_ascii_whitespace()).map(|w| w.strip_prefix(b"%_").unwrap_or(w)).filter(|w| !w.is_empty())
}
fn number(w: &[u8]) -> Option<f64> {
std::str::from_utf8(w).ok()?.parse::<f64>().ok().filter(|v| v.is_finite())
}
/// The number written just before `key` (`49 /StoryIndex`) in `head`.
fn number_before(head: &[u8], key: &[u8]) -> Option<f64> {
let k = find(head, key)?;
+1 -1
View File
@@ -258,7 +258,7 @@ impl<'a> Lexer<'a> {
}
/// A finite number token (`12`, `-3.5`, `.5`, `1e-3`), or `None`.
fn number(w: &[u8]) -> Option<f64> {
pub(super) fn number(w: &[u8]) -> Option<f64> {
let first = *w.first()?;
if !(first.is_ascii_digit() || matches!(first, b'+' | b'-' | b'.')) {
return None;