-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::Needed; | ||
|
||
/// Abstracts split_first | ||
pub trait InputSplit: Sized { | ||
/// The current input type is a sequence of that `Item` type. | ||
/// | ||
/// Example: `u8` for `&[u8]` or `char` for `&str` | ||
type Item; | ||
|
||
/// Divides one input into two at an index. | ||
/// | ||
/// Return the head first then the tail `Ok((head, tail))` | ||
fn split_at(&self, mid: usize) -> Result<(Self, Self), Needed>; | ||
|
||
/// Returns the first and all the rest of the elements of the slice, or None if it is empty. | ||
fn split_first(&self) -> Option<(Self::Item, Self)>; | ||
|
||
/// Returns the last and all the rest of the elements of the slice, or None if it is empty. | ||
fn split_last(&self) -> Option<(Self::Item, Self)>; | ||
} | ||
|
||
impl<'a> InputSplit for &'a str { | ||
type Item = char; | ||
|
||
fn split_at(&self, mid: usize) -> Result<(Self, Self), Needed> { | ||
if mid <= self.len() { | ||
Ok(str::split_at(self, mid)) | ||
} else { | ||
Err(Needed::new(mid - self.len())) | ||
} | ||
} | ||
|
||
fn split_first(&self) -> Option<(Self::Item, Self)> { | ||
let mut chars = self.chars(); | ||
chars.next().map(|c| (c, chars.as_str())) | ||
} | ||
|
||
fn split_last(&self) -> Option<(Self::Item, Self)> { | ||
let mut chars = self.chars(); | ||
chars.next_back().map(|c| (c, chars.as_str())) | ||
} | ||
} | ||
|
||
impl<'a> InputSplit for &'a [u8] { | ||
type Item = u8; | ||
|
||
fn split_at(&self, mid: usize) -> Result<(Self, Self), Needed> { | ||
if mid <= self.len() { | ||
Ok(<[u8]>::split_at(self, mid)) | ||
} else { | ||
Err(Needed::new(mid - self.len())) | ||
} | ||
} | ||
|
||
fn split_first(&self) -> Option<(Self::Item, Self)> { | ||
if let [first, tail @ ..] = *self { | ||
Some((*first, tail)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn split_last(&self) -> Option<(Self::Item, Self)> { | ||
if let [last, tail @ ..] = *self { | ||
Some((*last, tail)) | ||
} else { | ||
None | ||
} | ||
} | ||
} |