Fixed StringInput bug.
@@ -106,6 +106,9 @@ impl Input for StringInput {
panic!("position out of bounds");
}
+ let start = if pos == 0 {
+ 0
+ } else {
if self.string.slice_unchecked(pos, pos + 1) == "\n" {
pos -= 1;
}
@@ -114,11 +117,24 @@ impl Input for StringInput {
.rev()
.skip_while(|&(i, _)| i > pos)
.find(|&(_, c)| c == '\n');
- let start = match start {
+ match start {
Some((i, _)) => i + 1,
None => 0
+ }
};
+ let end = if pos == self.string.len() - 1 {
+ let mut end = self.string.len();
+
+ if end > 0 && self.string.slice_unchecked(end - 1, end) == "\n" {
+ end -= 1;
+ }
+ if end > 0 && self.string.slice_unchecked(end - 1, end) == "\r" {
+ end -= 1;
+ }
+
+ end
+ } else {
let end = self.string.char_indices()
.skip_while(|&(i, _)| i < pos)
.find(|&(_, c)| c == '\n');
@@ -126,10 +142,14 @@ impl Input for StringInput {
Some((i, _)) => i,
None => self.string.len()
};
+
if end > 0 && self.string.slice_unchecked(end - 1, end) == "\r" {
end -= 1;
}
+ end
+ };
+
self.string.slice_unchecked(start, end)
}
@@ -265,6 +285,15 @@ mod tests {
}
}
+ #[test]
+ fn line_of_new_line() {
+ let input = StringInput::new("\n".to_owned());
+
+ unsafe {
+ assert_eq!(input.line_of(0), "");
+ }
+ }
+
#[test]
fn skip_empty() {
let input = StringInput::new("".to_owned());
