Skip to content

Commit 313a3ea

Browse files
committed
Fix range parsing
1 parent 5680f1d commit 313a3ea

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/types/mod.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,32 +223,32 @@ impl Type for Vec<Range<u32>> {
223223
})
224224
};
225225

226-
let component = |s: &mut Scanner, offset: usize| -> Result<u32, TypeError> {
227-
loop {
228-
let num = number(s, offset)?;
229-
s.eat_whitespace();
230-
if !s.eat_if(':') {
231-
return Ok(num);
232-
}
233-
}
234-
};
235-
236226
for (range_candidate, span) in
237227
range_vecs.iter().map(|f| (f.format_verbatim(), f.span()))
238228
{
239229
let mut s = Scanner::new(&range_candidate);
240-
let start = component(&mut s, span.start)?;
230+
let start = number(&mut s, span.start)?;
241231
s.eat_whitespace();
242232

243233
// The double and triple hyphen is converted into en dashes and em
244234
// dashes earlier.
245235
if !s.eat_if(['-', '–', '—']) {
246236
res.push(start..start);
237+
if !s.done() {
238+
return Err(TypeError::new(span, TypeErrorKind::InvalidNumber));
239+
}
247240
continue;
248241
}
249242
s.eat_while('-');
250243
s.eat_whitespace();
251-
let end = component(&mut s, span.start)?;
244+
let offset = s.cursor();
245+
let end = number(&mut s, span.start)?;
246+
if !s.done() {
247+
return Err(TypeError::new(
248+
offset..span.end,
249+
TypeErrorKind::InvalidNumber,
250+
));
251+
}
252252
res.push(start..end);
253253
}
254254

@@ -473,7 +473,7 @@ impl Type for Gender {
473473
#[cfg(test)]
474474
mod tests {
475475
use super::*;
476-
use crate::chunk::tests::*;
476+
use crate::{chunk::tests::*, Bibliography};
477477

478478
#[test]
479479
fn test_ranges() {
@@ -491,4 +491,22 @@ mod tests {
491491
assert_eq!(res[0], 34..34);
492492
assert_eq!(res[1], 37..39);
493493
}
494+
495+
#[test]
496+
fn test_hayagriva_issue_340() {
497+
let bib = Bibliography::parse(
498+
r#"@inproceedings{test,
499+
author = {John Doe},
500+
title = {Interesting Findings},
501+
journal = {Example Journal},
502+
year = {2024},
503+
pages = {1A1}
504+
}"#,
505+
)
506+
.unwrap();
507+
let t = bib.get("test").unwrap();
508+
let pages = t.get("pages").unwrap();
509+
let parsed: PermissiveType<std::ops::Range<u32>> = pages.parse().unwrap();`
510+
assert!(matches!(parsed, PermissiveType::Chunks(_)))
511+
}
494512
}

0 commit comments

Comments
 (0)