pak.types.string

Types for strings.

class PrefixedString(prefix, *, encoding=None, errors=None)

Bases: Type

A string prefixed by the length of its encoded data.

Parameters:
  • prefix (typelike) –

    The Type which prefixes the string data and represents the length of the string.

    The prefixed length represents the amount of bytes the string data takes up.

    Note

    This does not include terminator characters such as a null-terminator.

  • encoding (str or None) –

    The encoding to encode/decode the data.

    If None, then the value of the encoding attribute is used.

  • errors (str) –

    The error handling scheme for encoding/decoding errors.

    If None, then the value of the errors attribute is used.

encoding = 'utf-8'
errors = 'replace'
prefix = None
class TerminatedString(*, encoding=None, terminator=None, errors=None)

Bases: Type

A string that is read until a terminator character is reached.

By default the terminator character is a null-terminator.

A util.BufferOutOfDataError is raised when unpacking if no terminator can be found.

Parameters:
  • encoding (str) –

    The encoding to encode/decode the data.

    If None, then the value of the encoding attribute is used.

  • terminator (str) –

    A single character indicating when string data has concluded.

    If None the value of the terminator attribute is used.

  • errors (str) –

    The error handling scheme for encoding/decoding errors.

    If None, then the value of the errors attribute is used.

encoding = 'utf-8'
errors = 'replace'
terminator = '\x00'
class StaticTerminatedString(size, *, encoding=None, terminator=None, errors=None, alignment=None)

Bases: Type

A string with a static size, terminated by a certain character.

By default the terminator character is a null-terminator.

A ValueError is raised when unpacking if no terminator is found in the data. Additionally a util.BufferOutOfDataError is raised if the buffer doesn’t contain the static size.

Additionally, a ValueError is raised when packing if the to-be-packed value is too long for the static size.

Parameters:
  • size (int) –

    The static size of the raw data containing the string.

    This size is in bytes.

    Warning

    The size should contain the terminator.

  • encoding (str or None) –

    The encoding to use to encode/decode the data.

    If None, then the value of the encoding attribute is used.

  • terminator (str) –

    A single character indicating when string data has concluded.

    If None the value of the terminator attribute is used.

  • errors (str) –

    The error handling scheme for encoding/decoding errors.

    If None, then the value of the errors attribute is used.

  • alignment (int or None) –

    The alignment of the string data.

    If None and encoding is None or the original encoding, alignment will be assumed to be the same as the original alignment.

encoding = 'utf-8'
errors = 'replace'
terminator = '\x00'