From ea604e09b8338b9fdb66c3921ea3dc955168e79b Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sun, 5 Dec 2021 12:21:19 -0300 Subject: Add StringArgumentType --- src/strcursor.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/strcursor.rs') 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 @@ -146,6 +149,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 { // 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 { // 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() }; -- cgit 1.4.1