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?