mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-24 09:46:59 -06:00
Reject a leading plus sign in page range numbers
Found while testing crates/doc/src/range.rs:12 Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
committed by
bflatastic
co-authored by
Claude Haiku 5.5
parent
3225315ce1
commit
156fe87fdd
+19
-1
@@ -9,7 +9,12 @@ pub fn parse_range(s: &str, count: usize) -> Result<Vec<usize>, String> {
|
|||||||
if t.is_empty() {
|
if t.is_empty() {
|
||||||
return Ok(open);
|
return Ok(open);
|
||||||
}
|
}
|
||||||
t.parse().ok().filter(|n| (1..=count).contains(n)).ok_or_else(|| format!("range `{s}`: `{t}` is not a number from 1 to {count}"))
|
t.bytes()
|
||||||
|
.all(|b| b.is_ascii_digit())
|
||||||
|
.then(|| t.parse().ok())
|
||||||
|
.flatten()
|
||||||
|
.filter(|n| (1..=count).contains(n))
|
||||||
|
.ok_or_else(|| format!("range `{s}`: `{t}` is not a number from 1 to {count}"))
|
||||||
};
|
};
|
||||||
let mut out = vec![];
|
let mut out = vec![];
|
||||||
for part in s.split(',').map(str::trim).filter(|p| !p.is_empty()) {
|
for part in s.split(',').map(str::trim).filter(|p| !p.is_empty()) {
|
||||||
@@ -35,3 +40,16 @@ pub fn parse_range(s: &str, count: usize) -> Result<Vec<usize>, String> {
|
|||||||
}
|
}
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::parse_range;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sign_is_not_part_of_a_page_number() {
|
||||||
|
assert!(parse_range("+2", 5).is_err());
|
||||||
|
assert!(parse_range("1-+3", 5).is_err());
|
||||||
|
assert!(parse_range("-+2", 5).is_err());
|
||||||
|
assert_eq!(parse_range("1-3, 5", 5), Ok(vec![0, 1, 2, 4]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user