Mojo struct
StringSlice
A non-owning view to encoded string data.
Notes: TODO: The underlying string data is guaranteed to be encoded using UTF-8.
Parameters
- is_mutable (
Bool
): Whether the slice is mutable. - origin (
Origin[is_mutable.value]
): The origin of the underlying string data.
Implemented traits
AnyType
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
FloatableRaising
,
Hashable
,
IntableRaising
,
Movable
,
Sized
,
Stringable
,
Stringlike
,
Writable
Methods
__init__
__init__(out self: StringSlice[StaticConstantOrigin], lit: StringLiteral)
Construct a new StringSlice
from a StringLiteral
.
Args:
- lit (
StringLiteral
): The literal to construct thisStringSlice
from.
__init__(out self, *, owned unsafe_from_utf8: Span[SIMD[uint8, 1], origin])
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[SIMD[uint8, 1], origin]
): ASpan[Byte]
encoded in UTF-8.
__init__(out 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
origin
.
- unsafe_from_utf8_strref
MUST be valid UTF-8 encoded data.
Args:
- unsafe_from_utf8_strref (
StringRef
): AStringRef
of bytes encoded in UTF-8.
__init__(out self, *, ptr: UnsafePointer[SIMD[uint8, 1]], length: Int)
Construct a StringSlice
from a pointer to a sequence of UTF-8 encoded bytes and a length.
Safety:
- ptr
MUST point to at least length
bytes of valid UTF-8 encoded
data.
- ptr
must point to data that is live for the duration of
origin
.
Args:
- ptr (
UnsafePointer[SIMD[uint8, 1]]
): A pointer to a sequence of bytes encoded in UTF-8. - length (
Int
): The number of bytes of encoded data.
__init__(out self, *, other: Self)
Explicitly construct a deep copy of the provided StringSlice
.
Args:
- other (
Self
): TheStringSlice
to copy.
__init__[O: ImmutableOrigin, //](out self: StringSlice[O], ref [O] value: String)
Construct an immutable StringSlice.
Parameters:
- O (
ImmutableOrigin
): The immutable origin.
Args:
- value (
String
): The string value.
__bool__
__bool__(self) -> Bool
Check if a string slice is non-empty.
Returns:
True if a string slice is non-empty, False otherwise.
__getitem__
__getitem__[IndexerType: Indexer](self, idx: IndexerType) -> String
Gets the character at the specified position.
Parameters:
- IndexerType (
Indexer
): The inferred type of an indexer argument.
Args:
- idx (
IndexerType
): The index value.
Returns:
A new string containing the character at the specified position.
__lt__
__lt__(self, rhs: StringSlice[origin]) -> Bool
Verify if the StringSlice
bytes are strictly less than the input in overlapping content.
Args:
- rhs (
StringSlice[origin]
): The otherStringSlice
to compare against.
Returns:
If the StringSlice
bytes are strictly less than the input in overlapping content.
__eq__
__eq__(self, rhs: StringSlice[origin]) -> Bool
Verify if a StringSlice
is equal to another StringSlice
.
Args:
- rhs (
StringSlice[origin]
): TheStringSlice
to compare against.
Returns:
If the StringSlice
is equal to the input in length and contents.
__eq__(self, rhs: String) -> Bool
Verify if a StringSlice
is equal to a string.
Args:
- rhs (
String
): TheString
to compare against.
Returns:
If the StringSlice
is equal to the input in length and contents.
__eq__(self, rhs: StringLiteral) -> Bool
Verify if a StringSlice
is equal to a literal.
Args:
- rhs (
StringLiteral
): TheStringLiteral
to compare against.
Returns:
If the StringSlice
is equal to the input in length and contents.
__ne__
__ne__(self, rhs: StringSlice[origin]) -> Bool
Verify if span is not equal to another StringSlice
.
Args:
- rhs (
StringSlice[origin]
): TheStringSlice
to compare against.
Returns:
If the StringSlice
is not equal to the input in length and contents.
__ne__(self, rhs: String) -> Bool
Verify if span is not equal to another StringSlice
.
Args:
- rhs (
String
): TheStringSlice
to compare against.
Returns:
If the StringSlice
is not equal to the input in length and contents.
__ne__(self, rhs: StringLiteral) -> Bool
Verify if span is not equal to a StringLiteral
.
Args:
- rhs (
StringLiteral
): TheStringLiteral
to compare against.
Returns:
If the StringSlice
is not equal to the input in length and contents.
__contains__
__contains__(ref self, substr: StringSlice[origin]) -> Bool
Returns True if the substring is contained within the current string.
Args:
- substr (
StringSlice[origin]
): The substring to check.
Returns:
True if the string contains the substring.
__mul__
__mul__(self, n: Int) -> String
Concatenates the string n
times.
Args:
- n (
Int
): The number of times to concatenate the string.
Returns:
The string concatenated n
times.
__str__
__str__(self) -> String
Gets this slice as a standard String
.
Returns:
The string representation of the slice.
__len__
__len__(self) -> Int
Nominally returns the length in Unicode codepoints (not bytes!).
Returns:
The length in Unicode codepoints.
write_to
write_to[W: Writer](self, inout writer: W)
Formats this string slice to the provided Writer
.
Parameters:
- W (
Writer
): A type conforming to theWritable
trait.
Args:
- writer (
W
): The object to write to.
__hash__
__hash__(self) -> UInt
Hash the underlying buffer using builtin hash.
Returns:
A 64-bit hash value. This value is not suitable for cryptographic uses. Its intended usage is for data structures. See the hash
builtin documentation for more details.
__iter__
__iter__(self) -> _StringSliceIter[origin]
Iterate over the string, returning immutable references.
Returns:
An iterator of references to the string elements.
__reversed__
__reversed__(self) -> _StringSliceIter[origin, 0]
Iterate backwards over the string, returning immutable references.
Returns:
A reversed iterator of references to the string elements.
__int__
__int__(self) -> Int
Parses the given string as a base-10 integer and returns that value. If the string cannot be parsed as an int, an error is raised.
Returns:
An integer value that represents the string, or otherwise raises.
__float__
__float__(self) -> SIMD[float64, 1]
Parses the string as a float point number and returns that value. If the string cannot be parsed as a float, an error is raised.
Returns:
A float value that represents the string, or otherwise raises.
strip
strip(self) -> Self
Gets a StringRef with leading and trailing whitespaces removed. This only takes ASCII whitespace into account: " \t\n\v\f\r\x1c\x1d\x1e"
.
Examples:
print(" mojo ".strip()) # "mojo"
print(" mojo ".strip()) # "mojo"
.
Returns:
A StringRef with leading and trailing whitespaces removed.
as_bytes
as_bytes(self) -> Span[SIMD[uint8, 1], origin]
Get the sequence of encoded bytes of the underlying string.
Returns:
A slice containing the underlying sequence of encoded bytes.
unsafe_ptr
unsafe_ptr(self) -> UnsafePointer[SIMD[uint8, 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) -> Int
Get the length of this string slice in bytes.
Returns:
The length of this string slice in bytes.
startswith
startswith(self, prefix: StringSlice[origin], start: Int = 0, end: Int = -1) -> Bool
Verify if the StringSlice
starts with the specified prefix between start and end positions.
Args:
- prefix (
StringSlice[origin]
): The prefix to check. - start (
Int
): The start offset from which to check. - end (
Int
): The end offset from which to check.
Returns:
True if the self[start:end]
is prefixed by the input prefix.
endswith
endswith(self, suffix: StringSlice[origin], start: Int = 0, end: Int = -1) -> Bool
Verify if the StringSlice
end with the specified suffix between start and end positions.
Args:
- suffix (
StringSlice[origin]
): The suffix to check. - start (
Int
): The start offset from which to check. - end (
Int
): The end offset from which to check.
Returns:
True if the self[start:end]
is suffixed by the input suffix.
format
format[*Ts: _CurlyEntryFormattable](self, *args: *Ts) -> String
Format a template with *args
.
Examples:
# Manual indexing:
print("{0} {1} {0}".format("Mojo", 1.125)) # Mojo 1.125 Mojo
# Automatic indexing:
print("{} {}".format(True, "hello world")) # True hello world
# Manual indexing:
print("{0} {1} {0}".format("Mojo", 1.125)) # Mojo 1.125 Mojo
# Automatic indexing:
print("{} {}".format(True, "hello world")) # True hello world
.
Parameters:
- *Ts (
_CurlyEntryFormattable
): The types of substitution values that implementRepresentable
andStringable
(to be changed and made more flexible).
Args:
- *args (
*Ts
): The substitution values.
Returns:
The template with the given values substituted.
find
find(ref self, substr: StringSlice[origin], start: Int = 0) -> Int
Finds the offset of the first occurrence of substr
starting at start
. If not found, returns -1
.
Args:
- substr (
StringSlice[origin]
): The substring to find. - start (
Int
): The offset from which to find.
Returns:
The offset of substr
relative to the beginning of the string.
rfind
rfind(self, substr: StringSlice[origin], start: Int = 0) -> Int
Finds the offset of the last occurrence of substr
starting at start
. If not found, returns -1
.
Args:
- substr (
StringSlice[origin]
): 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) -> Bool
Determines whether every character in the given StringSlice is a python whitespace String. This corresponds to Python's universal separators: " \t\n\v\f\r\x1c\x1d\x1e\x85\u2028\u2029"
.
Returns:
True if the whole StringSlice is made up of whitespace characters listed above, otherwise False.
isnewline
isnewline[single_character: Bool = 0](self) -> Bool
Determines whether every character in the given StringSlice is a python newline character. This corresponds to Python's universal newlines: "\r\n"
and "\t\n\v\f\r\x1c\x1d\x1e\x85\u2028\u2029"
.
Parameters:
- single_character (
Bool
): Whether to evaluate the stringslice as a single unicode character (avoids overhead when already iterating).
Returns:
True if the whole StringSlice is made up of whitespace characters listed above, otherwise False.
splitlines
splitlines[O: ImmutableOrigin, //](self: StringSlice[O], keepends: Bool = 0) -> List[StringSlice[$0]]
Split the string at line boundaries. This corresponds to Python's universal newlines: "\r\n"
and "\t\n\v\f\r\x1c\x1d\x1e\x85\u2028\u2029"
.
Parameters:
- O (
ImmutableOrigin
): The immutable origin.
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!