Skip to main content

StringTextReader

Struct StringTextReader 

Source
pub struct StringTextReader { /* private fields */ }
Expand description

Text reader over an owned string.

The reader tracks its physical UTF-8 byte offset, including any character read ahead while recognizing line endings. It recognizes LF, CRLF, and CR line endings by default.

§Examples

use qubit_io_text::StringTextReader;
use qubit_io_text::TextReader;

let mut reader = StringTextReader::new(String::from("hello"));
let mut output = String::new();
let count = reader.read_to_string(&mut output)?;
assert_eq!(count, 5);
assert_eq!(output, "hello");

Implementations§

Source§

impl StringTextReader

Source

pub fn new(text: String) -> Self

Creates a reader over owned text.

§Parameters
  • text: Text to own and read.
§Returns

A reader positioned at the start of the text.

Source

pub const fn position(&self) -> usize

Returns the physical UTF-8 byte position in the underlying string.

Line-ending detection may read one character ahead and retain it for the next read. This position includes that character and can equal the input length before all characters have been returned. Reading a retained character does not advance this position. Do not use this value as a checkpoint of text already delivered to the caller.

§Returns

The underlying byte offset, including any line-ending lookahead.

Source

pub const fn line_endings(&self) -> LineEndingSet

Returns the line endings recognized by this reader.

§Returns

The line endings recognized by this reader.

Source

pub const fn with_line_endings(self, line_endings: LineEndingSet) -> Self

Sets the line endings recognized by TextLineReader::read_line.

§Parameters
  • line_endings: Accepted line-ending sequences.
§Returns

This reader with the requested line-ending configuration.

Source

pub fn into_inner(self) -> String

Returns the owned string.

§Returns

The original string owned by this reader.

Trait Implementations§

Source§

impl Debug for StringTextReader

Source§

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

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

impl TextLineReader for StringTextReader

Source§

fn read_line(&mut self, output: &mut String) -> Result<bool, Self::Error>

Appends the next line to output, consuming its recognized line ending when present. Returns false only when no line content or terminator remains; a final unterminated line is returned with true.

§Parameters
  • output: Destination that receives the next line, appended after its existing contents.
§Returns

Ok(true) when a line or final unterminated line is read, or Ok(false) when no content or terminator remains.

§Errors

This in-memory reader never returns an error because its error type is Infallible.

Source§

impl TextReader for StringTextReader

Source§

type Error = !

This in-memory reader cannot fail while decoding its valid UTF-8 input.

Source§

fn read_char(&mut self) -> Result<Option<char>, Self::Error>

Returns the next Unicode scalar value, including any character retained during line-ending lookahead, or None when the input is exhausted.

§Returns

Ok(Some(ch)) when a character is available, or Ok(None) at EOF.

§Errors

This in-memory reader never returns an error because its error type is Infallible.

Source§

fn read_chars( &mut self, output: &mut Vec<char>, max: usize, ) -> Result<usize, Self::Error>

Appends up to max Unicode scalar values to output and returns the number appended. A character retained during line-ending lookahead is returned first; a zero limit leaves it pending.

§Parameters
  • output: Destination that receives the characters, appended after its existing contents.
  • max: Maximum number of Unicode scalar values to append.
§Returns

Ok(count) with the number of characters appended.

§Errors

This in-memory reader never returns an error because its error type is Infallible.

Source§

fn read_to_string(&mut self, output: &mut String) -> Result<usize, Self::Error>

Appends all remaining text, including any retained lookahead character, to output and returns the number of Unicode scalar values appended.

§Parameters
  • output: Destination that receives the remaining text, appended after its existing contents.
§Returns

Ok(count) with the number of Unicode scalar values appended.

§Errors

This in-memory reader never returns an error because its error type is Infallible.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.