diff options
Diffstat (limited to 'src/strcursor.rs')
-rw-r--r-- | src/strcursor.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/strcursor.rs b/src/strcursor.rs index d733ade..b7c0e5f 100644 --- a/src/strcursor.rs +++ b/src/strcursor.rs @@ -18,6 +18,9 @@ use crate::error::ReadError; /// `getRemainingLength` (use `get_remaining().len()` or /// `remaining_slice().len()`) and `getTotalLength` (use `get_ref().len()`). pub trait StringReader<'a>: Sized { + /// Returns the total length of the string. + //#[inline] + fn total_len(&self) -> usize; /// Returns the part of the string that has been read so far. /// /// # Panics @@ -147,6 +150,10 @@ pub trait StringReader<'a>: Sized { impl<'a> StringReader<'a> for Cursor<&'a str> { #[inline] + fn total_len(&self) -> usize { + self.get_ref().len() + } + #[inline] fn get_read(&self) -> &'a str { &self.get_ref()[..(self.position() as usize)] } @@ -157,7 +164,7 @@ impl<'a> StringReader<'a> for Cursor<&'a str> { #[inline] fn can_read_n(&self, len: usize) -> bool { // NOTE: NOT overflow-aware! - self.position() as usize + len <= self.get_ref().len() + self.position() as usize + len <= self.total_len() } #[inline] fn peek_n(&self, offset: usize) -> char { @@ -183,7 +190,7 @@ impl<'a> StringReader<'a> for Cursor<&'a str> { where T: FromStr<Err=::std::num::ParseIntError> { // see read_unquoted_str for rationale let start = self.position() as usize; - let total = self.get_ref().len(); + let total = self.total_len(); let end = total - { self.get_remaining().trim_start_matches(number_chars).len() }; @@ -204,7 +211,7 @@ impl<'a> StringReader<'a> for Cursor<&'a str> { where T: FromStr<Err=::std::num::ParseFloatError> { // see read_unquoted_str for rationale let start = self.position() as usize; - let total = self.get_ref().len(); + let total = self.total_len(); let end = total - { self.get_remaining().trim_start_matches(number_chars).len() }; @@ -240,7 +247,7 @@ impl<'a> StringReader<'a> for Cursor<&'a str> { // there's no easy way to grab start matches, so we have to do something // a bit more involved. let start = self.position() as usize; - let total = self.get_ref().len(); + let total = self.total_len(); let end = total - { self.get_remaining().trim_start_matches(unquoted_chars).len() }; |