From 8e84f42b3a78ddc61ab702e19fa6908823b0a297 Mon Sep 17 00:00:00 2001 From: Subhojit Paul Date: Thu, 20 Sep 2018 08:28:25 +0530 Subject: [PATCH] Implement InputTake for Span --- source/tokens.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/source/tokens.rs b/source/tokens.rs index c6186cb6..d68a958c 100644 --- a/source/tokens.rs +++ b/source/tokens.rs @@ -50,6 +50,7 @@ use nom::{ FindSubstring, InputIter, InputLength, + InputTake, Offset, Slice }; @@ -824,6 +825,36 @@ pub struct Span<'a> { slice: Input<'a> } +impl<'a> InputTake for Span<'a> { + fn take(&self, count: usize) -> Self { + Span { + offset: self.offset, + line: self.line, + column: self.column, + slice: self.slice.take(count), + } + } + + fn take_split(&self, count: usize) -> (Self, Self) { + let (left, right) = self.slice.take_split(count); + + ( + Span { + offset: self.offset, + line: self.line, + column: self.column, + slice: left, + }, + Span { + offset: self.offset, + line: self.line, + column: self.column, + slice: right, + }, + ) + } +} + impl<'a> Span<'a> { /// Create a span for a particular input with default `offset`, /// `line`, and `column` values. @@ -1622,7 +1653,7 @@ mod tests { fn case_span_slice_with_range() { let range = 2..5; let input = b"foobar"; - let output = Span { + let output = Span { offset: 2, line : 1, column: 3, @@ -1636,7 +1667,7 @@ mod tests { fn case_span_slice_with_range_to() { let range = 2..; let input = b"foobar"; - let output = Span { + let output = Span { offset: 2, line : 1, column: 3, @@ -1650,7 +1681,7 @@ mod tests { fn case_span_slice_with_range_from() { let range = ..3; let input = b"foobar"; - let output = Span { + let output = Span { offset: 0, line : 1, column: 1, @@ -1664,7 +1695,7 @@ mod tests { fn case_span_slice_with_range_full() { let range = ..; let input = b"foobar"; - let output = Span { + let output = Span { offset: 0, line : 1, column: 1,