1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-31 12:18:31 +00:00

add str traits for split, between, unquote; consolidate tests

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-31 02:13:23 +00:00 committed by strawberry
parent 2db017af37
commit 99ad404ea9
8 changed files with 212 additions and 51 deletions

View file

@ -0,0 +1,33 @@
const QUOTE: char = '"';
/// Slice a string between quotes
pub trait Unquote<'a> {
/// Whether the input is quoted. If this is false the fallible methods of
/// this interface will fail.
fn is_quoted(&self) -> bool;
/// Unquotes a string. If the input is not quoted it is simply returned
/// as-is. If the input is partially quoted on either end that quote is not
/// removed.
fn unquote(&self) -> Option<&'a str>;
/// Unquotes a string. The input must be quoted on each side for Some to be
/// returned
fn unquote_infallible(&self) -> &'a str;
}
impl<'a> Unquote<'a> for &'a str {
#[inline]
fn unquote_infallible(&self) -> &'a str {
self.strip_prefix(QUOTE)
.unwrap_or(self)
.strip_suffix(QUOTE)
.unwrap_or(self)
}
#[inline]
fn unquote(&self) -> Option<&'a str> { self.strip_prefix(QUOTE).and_then(|s| s.strip_suffix(QUOTE)) }
#[inline]
fn is_quoted(&self) -> bool { self.starts_with(QUOTE) && self.ends_with(QUOTE) }
}