Skip to main content

Mojo package

string

Provides comprehensive Unicode string functionality.

Core features:

  • Unicode support with UTF-8 encoding
  • Efficient string slicing and views
  • String formatting and interpolation
  • Memory-safe string operations
  • Unicode case conversion
  • Unicode property lookups and validation

Key components:

  • String: Mutable and owning string

    • Uses a smart three-mode allocation strategy: static memory references for string literals, small string optimization (SSO) for strings ≤23 bytes (stored directly within the String object with zero allocation cost), and reference-counted heap allocation for larger strings. This design makes the vast majority of real-world strings extremely fast.

    • Owns its data and manages memory automatically when heap-allocated.

    • Mutable and grows dynamically as needed.

  • StringSlice: Non-owning string view

    • Performs zero heap allocations: stores only a pointer and length that reference existing string data owned by another object.

    • Does not own the data pointed to, so it can't outlive the data it references.

  • StaticString: Compile-time constant (immutable) string reference

    • Performs zero heap allocations: stores a pointer and length to a compile-time constant or static program memory.

    • References data with a static lifetime that exists for the entire program duration, unlike StringSlice which can reference temporary data.

  • Codepoint: Unicode codepoint representation and operations

    • Represents a single Unicode codepoint as a 32-bit value.

    • Enables iteration over string contents at the Unicode codepoint level rather than byte level for proper Unicode text processing.

  • format: Built-in string formatting and interpolation utilities.

Modules

  • codepoint: Unicode codepoint handling.
  • format: String formatting utilities for Mojo.
  • string: Implements the core String type and related utilities.
  • string_slice: Implements the StringSlice type and related utilities for efficient string operations.

Was this page helpful?