Mojo struct
StringSlice
A non-owning view to encoded string data.
TODO: The underlying string data is guaranteed to be encoded using UTF-8.
Parametersβ
- βis_mutable (
Bool
): Whether the slice is mutable. - βlifetime (
AnyLifetime[is_mutable.value]
): The lifetime of the underlying string data.
Implemented traitsβ
AnyType
,
Formattable
,
Sized
,
Stringable
Methodsβ
__init__
β
__init__(inout self: StringSlice[0, MutableStaticLifetime], lit: StringLiteral)
Construct a new string slice from a string literal.
Args:
- βlit (
StringLiteral
): The literal to construct this string slice from.
__init__(inout self: Self, *, owned unsafe_from_utf8: Span[is_mutable, SIMD[uint8, 1], lifetime])
Construct a new StringSlice from a sequence of UTF-8 encoded bytes.
Safety:
unsafe_from_utf8
MUST be valid UTF-8 encoded data.
Args:
- βunsafe_from_utf8 (
Span[is_mutable, SIMD[uint8, 1], lifetime]
): A slice of bytes encoded in UTF-8.
__init__(inout self: Self, *, unsafe_from_utf8_strref: StringRef)
Construct a new StringSlice from a StringRef pointing to UTF-8 encoded bytes.
Safety:
- unsafe_from_utf8_strref
MUST point to data that is valid for
lifetime
.
- unsafe_from_utf8_strref
MUST be valid UTF-8 encoded data.
Args:
- βunsafe_from_utf8_strref (
StringRef
): A StringRef of bytes encoded in UTF-8.
__init__(inout self: Self, *, unsafe_from_utf8_ptr: UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1], len: Int)
Construct a StringSlice from a pointer to a sequence of UTF-8 encoded bytes and a length.
Safety:
- unsafe_from_utf8_ptr
MUST point to at least len
bytes of valid
UTF-8 encoded data.
- unsafe_from_utf8_ptr
must point to data that is live for the
duration of lifetime
.
Args:
- βunsafe_from_utf8_ptr (
UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): A pointer to a sequence of bytes encoded in UTF-8. - βlen (
Int
): The number of bytes of encoded data.
__bool__
β
__bool__(self: Self) -> Bool
Check if a string slice is non-empty.
Returns:
True if a string slice is non-empty, False otherwise.
__eq__
β
__eq__(self: Self, rhs: StringSlice[is_mutable, lifetime]) -> Bool
Verify if a string slice is equal to another string slice.
Args:
- βrhs (
StringSlice[is_mutable, lifetime]
): The string slice to compare against.
Returns:
True if the string slices are equal in length and contain the same elements, False otherwise.
__eq__(self: Self, rhs: String) -> Bool
Verify if a string slice is equal to a string.
Args:
- βrhs (
String
): The string to compare against.
Returns:
True if the string slice is equal to the input string in length and contain the same bytes, False otherwise.
__eq__(self: Self, rhs: StringLiteral) -> Bool
Verify if a string slice is equal to a literal.
Args:
- βrhs (
StringLiteral
): The literal to compare against.
Returns:
True if the string slice is equal to the input literal in length and contain the same bytes, False otherwise.
__ne__
β
__ne__(self: Self, rhs: StringSlice[is_mutable, lifetime]) -> Bool
Verify if span is not equal to another string slice.
Args:
- βrhs (
StringSlice[is_mutable, lifetime]
): The string slice to compare against.
Returns:
True if the string slices are not equal in length or contents, False otherwise.
__ne__(self: Self, rhs: String) -> Bool
Verify if span is not equal to another string slice.
Args:
- βrhs (
String
): The string slice to compare against.
Returns:
True if the string and slice are not equal in length or contents, False otherwise.
__ne__(self: Self, rhs: StringLiteral) -> Bool
Verify if span is not equal to a literal.
Args:
- βrhs (
StringLiteral
): The string literal to compare against.
Returns:
True if the slice is not equal to the literal in length or contents, False otherwise.
__str__
β
__str__(self: Self) -> String
Gets this slice as a standard String
.
Returns:
The string representation of the slice.
__len__
β
__len__(self: Self) -> Int
Nominally returns the length in Unicode codepoints (not bytes!).
Returns:
The length in Unicode codepoints.
format_to
β
format_to(self: Self, inout writer: Formatter)
Formats this string slice to the provided formatter.
Args:
- βwriter (
Formatter
): The formatter to write to.
__iter__
β
__iter__(ref [self_is_lifetime] self: Self) -> _StringSliceIter[$0, $1, 1]
Iterate over elements of the string slice, returning immutable references.
Returns:
An iterator of references to the string elements.
__reversed__
β
__reversed__(ref [self_is_lifetime] self: Self) -> _StringSliceIter[$0, $1, 0]
Iterate backwards over the string, returning immutable references.
Returns:
A reversed iterator of references to the string elements.
as_bytes_slice
β
as_bytes_slice(self: Self) -> Span[is_mutable, SIMD[uint8, 1], lifetime]
Get the sequence of encoded bytes as a slice of the underlying string.
Returns:
A slice containing the underlying sequence of encoded bytes.
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]
Gets a pointer to the first element of this string slice.
Returns:
A pointer pointing at the first element of this string slice.
byte_length
β
byte_length(self: Self) -> Int
Get the length of this string slice in bytes.
Returns:
The length of this string slice in bytes.
find
β
find(self: Self, substr: StringSlice[is_mutable, lifetime], start: Int = 0) -> Int
Finds the offset of the first occurrence of substr
starting at start
. If not found, returns -1.
Args:
- βsubstr (
StringSlice[is_mutable, lifetime]
): The substring to find. - βstart (
Int
): The offset from which to find.
Returns:
The offset of substr
relative to the beginning of the string.
isspace
β
isspace(self: Self) -> Bool
Determines whether every character in the given StringSlice is a python whitespace String. This corresponds to Python's universal separators " \t\n\r\f\v\x1c\x1d\x1e\x85\u2028\u2029"
.
Returns:
True if the whole StringSlice is made up of whitespace characters listed above, otherwise False.
splitlines
β
splitlines(self: Self, keepends: Bool = 0) -> List[String, 0]
Split the string at line boundaries. This corresponds to Python's universal newlines "\t\n\r\r\n\f\v\x1c\x1d\x1e\x85\u2028\u2029"
.
Args:
- βkeepends (
Bool
): If True, line breaks are kept in the resulting strings.
Returns:
A List of Strings containing the input split by line boundaries.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?