Struct jpar::Reader [−][src]
A String
reader that moves a cursor the reader updated.
Implementations
impl<'a> Reader<'a>
[src]
pub fn new(content: &'a str) -> Reader<'a, (), ()>
[src]
Create a new Reader
with the specified content
.
pub fn new_with_error<Err>(content: &'a str) -> Reader<'a, Err, ()>
[src]
Create a new Reader
with the specified content
and defining an error type.
pub fn new_with_context<C>(content: &'a str, context: C) -> Reader<'a, (), C>
[src]
Create a new Reader
with the specified content
and context
.
impl<'a, C, Err> Reader<'a, Err, C>
[src]
pub fn new_with_context_and_error(
content: &'a str,
context: C
) -> Reader<'a, Err, C>
[src]
content: &'a str,
context: C
) -> Reader<'a, Err, C>
Create a new Reader
with the specified content
and context
and defining an error type.
pub fn context(&self) -> &C
[src]
The associated context of the Reader
if there’s any.
pub fn content(&self) -> &'a str
[src]
The content of the Reader
.
pub fn byte_offset(&self) -> usize
[src]
The position of the Reader
in bytes.
pub fn char_offset(&self) -> usize
[src]
The position of the Cursor
in characters.
It starts at char 0.
pub fn line(&self) -> usize
[src]
The line number of the current position. It starts at line 1.
pub fn column(&self) -> usize
[src]
The column number of the current position. It starts at column 1.
pub fn remaining_content(&self) -> &'a str
[src]
The remaining content as an Slice
.
pub fn remaining_length(&self) -> usize
[src]
The length in bytes of the content that is not already read.
pub fn remaining_char_length(&self) -> usize
[src]
The length in characters of the content that is not already read.
pub fn span_at_offset(&self) -> Span<'_>
[src]
Returns an empty Span
located at the current position.
pub fn is_end(&self) -> bool
[src]
Whether the reader is placed at the end of the input or not.
pub fn read(&mut self) -> Option<char>
[src]
Consumes the next character if present moving the start index forward.
Example
let mut reader = Reader::new("test"); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('e')); assert_eq!(reader.read(), Some('s')); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), None);
pub fn read_text(&mut self, text: &str) -> bool
[src]
Consumes the next characters if match text
moving the start index forward.
Example
let mut reader = Reader::new("test"); assert!(reader.read_text("te")); assert!(reader.read_text("st")); assert!(!reader.read_text("123"));
pub fn read_quantified<Q>(&mut self, quantifier: Q) -> Option<&'a str> where
Q: Into<Quantifier>,
[src]
Q: Into<Quantifier>,
Consumes a quantified number of characters specified by quantifier
.
Example
let mut reader = Reader::new("this test"); let result = reader.read_quantified(4); assert_eq!(result, Some("this")); let result = reader.read_quantified(..=4); assert_eq!(result, Some(" tes")); let result = reader.read_quantified(3..); assert_eq!(result, None); let result = reader.read_quantified(..); assert_eq!(result, Some("t"));
pub fn read_while(
&mut self,
verifier: impl FnMut(usize, char) -> bool
) -> &'a str
[src]
&mut self,
verifier: impl FnMut(usize, char) -> bool
) -> &'a str
Checks whether the reader continues with one or more of the characters validated by verifier
.
Example
let mut reader = Reader::new("this test"); let result = reader.read_while(|i,c| ('a'..='z').contains(&c)); assert_eq!(result, "this"); assert_eq!(reader.read(), Some(' ')); let result = reader.read_while(|i,c| ('0'..='9').contains(&c)); assert_eq!(result, ""); let result = reader.read_while(|i,c| ('a'..='z').contains(&c)); assert_eq!(result, "test");
pub fn read_while_quantified(
&mut self,
quantifier: impl Into<Quantifier>,
verifier: impl FnMut(usize, char) -> bool
) -> Option<&'a str>
[src]
&mut self,
quantifier: impl Into<Quantifier>,
verifier: impl FnMut(usize, char) -> bool
) -> Option<&'a str>
Checks whether the reader continues with a quantified number of the characters validated by verifier
.
Example
let mut reader = Reader::new("this test"); let result = reader.read_while_quantified(1..=4, |i,c| c != 'i'); assert_eq!(result, Some("th")); let result = reader.read_while_quantified(4, |i,c| true); assert_eq!(result, Some("is t")); let result = reader.read_while_quantified(50, |i,c| true); assert_eq!(result, None);
pub fn peek(&self) -> Option<char>
[src]
Gets the next character if present. This method does not consume the character.
Example
let mut reader = Reader::new("test"); assert_eq!(reader.peek(), Some('t')); assert_eq!(reader.peek(), Some('t')); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.peek(), Some('e')); assert_eq!(reader.read(), Some('e')); assert_eq!(reader.read(), Some('s')); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.peek(), None);
pub fn peek_text(&self, text: &str) -> bool
[src]
Gets the next characters if match text
. This method does not consume the characters.
Example
let mut reader = Reader::new("test"); assert!(reader.peek_text("te")); assert!(reader.peek_text("test")); assert!(!reader.peek_text("123"));
pub fn peek_quantified(
&self,
quantifier: impl Into<Quantifier>
) -> Option<&'a str>
[src]
&self,
quantifier: impl Into<Quantifier>
) -> Option<&'a str>
Checks whether the reader continues with a quantified number of characters specified by quantifier
.
This method does not consume the reader.
Example
let mut reader = Reader::new("this test"); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_quantified(..); assert_eq!(result, Some("this test")); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_quantified(4_usize); assert_eq!(result, Some("this")); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_quantified(20_usize); assert_eq!(result, None); assert_eq!(reader.byte_offset(), 0);
pub fn peek_while<F>(&self, verifier: F) -> &'a str where
F: FnMut(usize, char) -> bool,
[src]
F: FnMut(usize, char) -> bool,
Checks whether the reader continues with one or more of the characters validated by verifier
.
This method does not consume the reader.
Note: this method requires interval
be sorted.
Example
let mut reader = Reader::new("this test"); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_while(|i,c| ('a'..='z').contains(&c)); assert_eq!(result, "this"); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_while(|i,c| i < 3); assert_eq!(result, "thi"); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_while(|i,c| ('0'..='9').contains(&c)); assert_eq!(result, ""); assert_eq!(reader.byte_offset(), 0);
pub fn peek_while_quantified<Q, F>(
&self,
quantifier: Q,
verifier: F
) -> Option<&'a str> where
Q: Into<Quantifier>,
F: FnMut(usize, char) -> bool,
[src]
&self,
quantifier: Q,
verifier: F
) -> Option<&'a str> where
Q: Into<Quantifier>,
F: FnMut(usize, char) -> bool,
Checks whether the reader continues with a quantified number of the characters validated by verifier
.
This method does not consume the reader.
Note: this method requires interval
be sorted.
Example
let mut reader = Reader::new("this test"); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_while_quantified(1..=4, |i,c| c != 'i'); assert_eq!(result, Some("th")); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_while_quantified(1..=4, |i,c| true); assert_eq!(result, Some("this")); assert_eq!(reader.byte_offset(), 0); let result = reader.peek_while_quantified(50, |i,c| true); assert_eq!(result, None); assert_eq!(reader.byte_offset(), 0);
pub fn substring(&self, from: &Cursor, to: &Cursor) -> Span<'a>
[src]
Gets a Span
that contains the susbstring delimited by both (from
, to
) cursors.
The order of the cursors does not matter.
Example
let mut reader = Reader::new("this test"); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('h')); let from = reader.save_cursor(); assert_eq!(reader.read(), Some('i')); assert_eq!(reader.read(), Some('s')); assert_eq!(reader.read(), Some(' ')); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('e')); assert_eq!(reader.read(), Some('s')); let to = reader.save_cursor(); assert_eq!(reader.substring(&from, &to).content(), "is tes"); assert_eq!(reader.substring(&to, &from).content(), "is tes");
pub fn substring_to_current(&self, cursor: &Cursor) -> Span<'a>
[src]
Gets a Span
that contains the susbstring delimited by cursor
and current cursors.
The order of the cursors does not matter.
Example
let mut reader = Reader::new("this test"); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('h')); let from = reader.save_cursor(); assert_eq!(reader.read(), Some('i')); assert_eq!(reader.read(), Some('s')); assert_eq!(reader.read(), Some(' ')); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('e')); assert_eq!(reader.read(), Some('s')); assert_eq!(reader.substring_to_current(&from).content(), "is tes");
pub fn save_cursor(&self) -> Cursor
[src]
Saves the current Reader
’s position as a new Cursor
.
Example
let mut reader = Reader::new("this test"); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('h')); let cursor = reader.save_cursor(); assert_eq!(cursor.byte_offset(), 2);
pub fn restore(&mut self, cursor: Cursor)
[src]
Restores the reader to the specified Cursor
state.
Safety
This method will cause undefined behaviour if the Cursor was not generated by this reader.
Example
let mut reader = Reader::new("this test"); let cursor = reader.save_cursor(); assert_eq!(reader.byte_offset(), 0); assert_eq!(cursor.byte_offset(), 0); assert_eq!(reader.read(), Some('t')); assert_eq!(reader.read(), Some('h')); let cursor2 = reader.save_cursor(); assert_eq!(reader.byte_offset(), 2); assert_eq!(cursor.byte_offset(), 0); assert_eq!(cursor2.byte_offset(), 2); reader.restore(cursor); assert_eq!(reader.byte_offset(), 0); assert_eq!(cursor2.byte_offset(), 2);
impl<'a, C: Clone> Reader<'a, C>
[src]
pub fn remaining_content_span(&self) -> Span<'a>
[src]
The remaining content as an Span
.
Trait Implementations
impl<'a, Err: Clone, C: Clone> Clone for Reader<'a, Err, C>
[src]
impl<'a, Err: Debug, C: Debug> Debug for Reader<'a, Err, C>
[src]
Auto Trait Implementations
impl<'a, Err, C> RefUnwindSafe for Reader<'a, Err, C> where
C: RefUnwindSafe,
Err: RefUnwindSafe,
C: RefUnwindSafe,
Err: RefUnwindSafe,
impl<'a, Err, C> Send for Reader<'a, Err, C> where
C: Send,
Err: Send,
C: Send,
Err: Send,
impl<'a, Err, C> Sync for Reader<'a, Err, C> where
C: Sync,
Err: Sync,
C: Sync,
Err: Sync,
impl<'a, Err, C> Unpin for Reader<'a, Err, C> where
C: Unpin,
Err: Unpin,
C: Unpin,
Err: Unpin,
impl<'a, Err, C> UnwindSafe for Reader<'a, Err, C> where
C: UnwindSafe,
Err: UnwindSafe,
C: UnwindSafe,
Err: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,