Skip to main content

StringReadExt

Trait StringReadExt 

Source
pub trait StringReadExt: Read {
    // Required methods
    fn read_utf8_payload(
        &mut self,
        len: usize,
        max_len: usize,
    ) -> Result<String>;
    fn read_utf8_string_uleb(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_uleb_strict(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_u16(
        &mut self,
        byte_order: ByteOrder,
        max_len: usize,
    ) -> Result<String>;
    fn read_utf8_string_u16_be(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_u16_le(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_u32(
        &mut self,
        byte_order: ByteOrder,
        max_len: usize,
    ) -> Result<String>;
    fn read_utf8_string_u32_be(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_u32_le(&mut self, max_len: usize) -> Result<String>;
}
Expand description

Extension methods for reading length-prefixed UTF-8 strings.

Required Methods§

Source

fn read_utf8_payload(&mut self, len: usize, max_len: usize) -> Result<String>

Reads a UTF-8 payload with an already decoded byte length.

§Parameters
  • len: UTF-8 payload length in bytes.
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for payload reads, std::io::ErrorKind::InvalidData when len exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_uleb(&mut self, max_len: usize) -> Result<String>

Reads a UTF-8 string with an unsigned LEB128 byte-length prefix.

The length prefix is decoded as usize, so this format is target-width dependent. Prefer u16 or u32 length-prefix methods for persistent files and cross-platform protocols.

§Parameters
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_uleb_strict(&mut self, max_len: usize) -> Result<String>

Reads a UTF-8 string with a canonical unsigned LEB128 byte-length prefix.

The length prefix is decoded as usize, so this format is target-width dependent. Prefer u16 or u32 length-prefix methods for persistent files and cross-platform protocols.

§Parameters
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the length prefix is malformed or non-canonical, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_u16( &mut self, byte_order: ByteOrder, max_len: usize, ) -> Result<String>

Reads a UTF-8 string with a runtime-order u16 byte-length prefix.

§Parameters
  • byte_order: Byte order used by the length prefix.
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_u16_be(&mut self, max_len: usize) -> Result<String>

Reads a UTF-8 string with a big-endian u16 byte-length prefix.

§Parameters
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_u16_le(&mut self, max_len: usize) -> Result<String>

Reads a UTF-8 string with a little-endian u16 byte-length prefix.

§Parameters
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_u32( &mut self, byte_order: ByteOrder, max_len: usize, ) -> Result<String>

Reads a UTF-8 string with a runtime-order u32 byte-length prefix.

§Parameters
  • byte_order: Byte order used by the length prefix.
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_u32_be(&mut self, max_len: usize) -> Result<String>

Reads a UTF-8 string with a big-endian u32 byte-length prefix.

§Parameters
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Source

fn read_utf8_string_u32_le(&mut self, max_len: usize) -> Result<String>

Reads a UTF-8 string with a little-endian u32 byte-length prefix.

§Parameters
  • max_len: Maximum accepted UTF-8 payload length in bytes.
§Returns

The decoded string.

§Errors

Returns an I/O error for length or payload reads, std::io::ErrorKind::InvalidData when the encoded length exceeds max_len, or std::io::ErrorKind::InvalidData when the payload is not valid UTF-8.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> StringReadExt for T
where T: Read + ?Sized,