Skip to main content

StringReadExt

Trait StringReadExt 

Source
pub trait StringReadExt: Input<Item = u8> {
    // Required methods
    fn read_utf8_payload(
        &mut self,
        len: usize,
        max_len: usize,
    ) -> Result<String>;
    fn read_utf8_string_uleb_usize(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_uleb_usize_strict(
        &mut self,
        max_len: usize,
    ) -> Result<String>;
    fn read_utf8_string_uleb_u64(&mut self, max_len: usize) -> Result<String>;
    fn read_utf8_string_uleb_u64_strict(
        &mut self,
        max_len: usize,
    ) -> Result<String>;
    fn read_string_with_u16_len(
        &mut self,
        byte_order: ByteOrder,
        max_len: usize,
    ) -> Result<String>;
    fn read_string_with_u16_len_be(&mut self, max_len: usize) -> Result<String>;
    fn read_string_with_u16_len_le(&mut self, max_len: usize) -> Result<String>;
    fn read_string_with_u32_len(
        &mut self,
        byte_order: ByteOrder,
        max_len: usize,
    ) -> Result<String>;
    fn read_string_with_u32_len_be(&mut self, max_len: usize) -> Result<String>;
    fn read_string_with_u32_len_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_usize(&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_usize_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_uleb_u64(&mut self, max_len: usize) -> Result<String>

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

Prefer this method over Self::read_utf8_string_uleb_usize for persistent files and cross-platform protocols because the length field is independent of the current Rust target’s pointer width.

§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 u64 length does not fit the local usize, when the encoded length exceeds max_len, or when the payload is not valid UTF-8.

Source

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

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

Prefer this method over Self::read_utf8_string_uleb_usize_strict for persistent files and cross-platform protocols because the length field is independent of the current Rust target’s pointer width.

§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, when the encoded u64 length does not fit the local usize, when the encoded length exceeds max_len, or when the payload is not valid UTF-8.

Source

fn read_string_with_u16_len( &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_string_with_u16_len_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_string_with_u16_len_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_string_with_u32_len( &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_string_with_u32_len_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_string_with_u32_len_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: Input<Item = u8> + ?Sized,