Struct parsa::ParserString
source · pub struct ParserString { /* private fields */ }
Expand description
A shrinking-window read-only string.
String slices can be taken from the front, and reset, with zero allocations or copies.
Implementations§
source§impl ParserString
impl ParserString
sourcepub fn take(&mut self, n: usize) -> &str
pub fn take(&mut self, n: usize) -> &str
Splits the string at n
, shrinking it. Panics if n
is larger than the remaining slice.
let mut input = ParserString::from("abc123");
assert_eq!(input.take(3), "abc");
assert_eq!(input.take(3), "123");
let mut input = ParserString::from("🗻∈🌏");
assert_eq!(input.take(2), "🗻∈");
assert_eq!(input.take(1), "🌏");
sourcepub fn try_take(&mut self, n: usize) -> Option<&str>
pub fn try_take(&mut self, n: usize) -> Option<&str>
Splits the string at n
, shrinking it. Returns None
if n
is larger than the remaining slice.
let mut input = ParserString::from("abc123");
assert_eq!(input.try_take(5), Some("abc12"));
assert_eq!(input.try_take(5), None);
sourcepub unsafe fn give(&mut self, n: usize)
pub unsafe fn give(&mut self, n: usize)
Rewinds the string slice n
spaces. Panics if n
is larger than the taken space.
let mut input = ParserString::from("abc123");
assert_eq!(input.take(3), "abc");
unsafe { input.give(3); }
assert_eq!(input.take(3), "abc");
assert_eq!(input.take(3), "123");
§Safety
Caller must assure that the resulting pointer lands on a UTF-8 code point. This library assumes that a function will never add back more than its taken, and thus is considered undefined behavior. This will never cause memory-unsafety, but can cause unpredictable things to happen.
sourcepub unsafe fn set_ptr(&mut self, ptr: usize)
pub unsafe fn set_ptr(&mut self, ptr: usize)
Set the current start position manually.
§Safety
Caller must assure that the resulting pointer lands on a UTF-8 code point.
let mut input = ParserString::from("abc123");
unsafe { input.set_ptr(3); }
assert_eq!(input.get(), "123");
sourcepub fn get(&self) -> &str
pub fn get(&self) -> &str
Get a reference to the string slice.
let mut input = ParserString::from("abc123");
let _ = input.take(2);
assert_eq!(input.get(), "c123");