Mojo struct
InlineString
A string that performs small-string optimization to avoid heap allocations for short strings.
Aliasesβ
SMALL_CAP = 24
: The number of bytes of string data that can be stored inline in this string before a heap allocation is required.Layout = Variant[String, _FixedString[24]]
:
Implemented traitsβ
AnyType
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
Sized
,
Stringable
Methodsβ
__init__
β
__init__(inout self: Self)
Constructs a new empty string.
__init__(inout self: Self, literal: StringLiteral)
Constructs a InlineString value given a string literal.
Args:
- βliteral (
StringLiteral
): The input constant string.
__init__(inout self: Self, owned heap_string: String)
Construct a new small string by taking ownership of an existing heap-allocated String.
Args:
- βheap_string (
String
): The heap string to take ownership of.
__init__(inout self: Self, *, other: Self)
Copy the object.
Args:
- βother (
Self
): The value to copy.
__add__
β
__add__(self: Self, other: StringLiteral) -> Self
Construct a string by appending another string at the end of this string.
Args:
- βother (
StringLiteral
): The string to append.
Returns:
A new string containing the concatenation of self
and other
.
__add__(self: Self, other: String) -> Self
Construct a string by appending another string at the end of this string.
Args:
- βother (
String
): The string to append.
Returns:
A new string containing the concatenation of self
and other
.
__add__(self: Self, other: Self) -> Self
Construct a string by appending another string at the end of this string.
Args:
- βother (
Self
): The string to append.
Returns:
A new string containing the concatenation of self
and other
.
__iadd__
β
__iadd__(inout self: Self, literal: StringLiteral)
Appends another string to this string.
Args:
- βliteral (
StringLiteral
): The string to append.
__iadd__(inout self: Self, string: String)
Appends another string to this string.
Args:
- βstring (
String
): The string to append.
__iadd__(inout self: Self, str_slice: StringSlice[is_mutable, lifetime])
Appends another string to this string.
Args:
- βstr_slice (
StringSlice[is_mutable, lifetime]
): The string to append.
__len__
β
__len__(self: Self) -> Int
Gets the string length, in bytes.
Returns:
The string length, in bytes.
__str__
β
__str__(self: Self) -> String
Gets this string as a standard String
.
Returns:
The string representation of the type.
unsafe_ptr
β
unsafe_ptr(self: Self) -> UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Returns a pointer to the bytes of string data.
Returns:
The pointer to the underlying memory.
as_string_slice
β
as_string_slice(ref [self_is_lifetime] self: Self) -> StringSlice[$0, $1]
Returns a string slice of the data owned by this inline string.
Returns:
A string slice pointing to the data owned by this inline string.
as_bytes_slice
β
as_bytes_slice(ref [self_is_lifetime] self: Self) -> Span[$0, SIMD[uint8, 1], $1]
Returns a contiguous slice of the bytes owned by this string.
This does not include the trailing null terminator.
Returns:
A contiguous slice pointing to the bytes owned by this string.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?