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

source

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), "🌏");
source

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);
source

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.

source

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");
source

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");
source

pub fn len(&self) -> usize

Get the length of the string.

let mut input = ParserString::from("abc123");
let _ = input.take(2);
assert_eq!(input.len(), 4);
source

pub fn start(&self) -> usize

Get the current start of the string, relative to the “true” start.

let mut input = ParserString::from("abc123");
let _ = input.take(2);
assert_eq!(input.start(), 2);

Trait Implementations§

source§

impl Debug for ParserString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for ParserString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&str> for ParserString

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl From<String> for ParserString

source§

fn from(value: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.