struct
StringRef
Represent a constant reference to a string, i.e. a sequence of characters and a length, which need not be null terminated.
Fields
- data (
UnsafePointer[SIMD[uint8, 1], 0]
): A pointer to the beginning of the string data being referenced. - length (
Int
): The length of the string being referenced.
Implemented traits
AnyType
,
Boolable
,
CollectionElement
,
Comparable
,
Copyable
,
EqualityComparable
,
Hashable
,
IntableRaising
,
Movable
,
Sized
,
Stringable
Methods
__init__
__init__() -> Self
Construct a StringRef value with length zero.
Returns:
Constructed StringRef
object.
__init__(str: StringLiteral) -> Self
Construct a StringRef value given a constant string.
Args:
- str (
StringLiteral
): The input constant string.
Returns:
Constructed StringRef
object.
__init__(ptr: DTypePointer[int8, 0], len: Int) -> Self
Construct a StringRef value given a (potentially non-0 terminated string).
The constructor takes a raw pointer and a length.
Note that you should use the constructor from DTypePointer[DType.uint8]
instead
as we are now storing the bytes as UInt8.
See https://github.com/modularml/mojo/issues/2317 for more information.
Args:
- ptr (
DTypePointer[int8, 0]
): DTypePointer to the string. - len (
Int
): The length of the string.
Returns:
Constructed StringRef
object.
__init__(ptr: DTypePointer[uint8, 0], len: Int) -> Self
Construct a StringRef value given a (potentially non-0 terminated string).
The constructor takes a raw pointer and a length.
Args:
- ptr (
DTypePointer[uint8, 0]
): DTypePointer to the string. - len (
Int
): The length of the string.
Returns:
Constructed StringRef
object.
__init__(ptr: UnsafePointer[SIMD[uint8, 1], 0]) -> Self
Construct a StringRef value given a null-terminated string.
Args:
- ptr (
UnsafePointer[SIMD[uint8, 1], 0]
): UnsafePointer to the string.
Returns:
Constructed StringRef
object.
__init__(ptr: DTypePointer[int8, 0]) -> Self
Construct a StringRef value given a null-terminated string.
Note that you should use the constructor from DTypePointer[DType.uint8]
instead
as we are now storing the bytes as UInt8.
See https://github.com/modularml/mojo/issues/2317 for more information.
Args:
- ptr (
DTypePointer[int8, 0]
): DTypePointer to the string.
Returns:
Constructed StringRef
object.
__init__(ptr: DTypePointer[uint8, 0]) -> Self
Construct a StringRef value given a null-terminated string.
Args:
- ptr (
DTypePointer[uint8, 0]
): DTypePointer to the string.
Returns:
Constructed StringRef
object.
__bool__
__bool__(self: Self) -> Bool
Checks if the string is empty or not.
Returns:
Returns True if the string is not empty and False otherwise.
__getitem__
__getitem__(self: Self, idx: Int) -> Self
Get the string value at the specified position.
Args:
- idx (
Int
): The index position.
Returns:
The character at the specified position.
__lt__
__lt__(self: Self, rhs: Self) -> Bool
Compare this StringRef to the RHS using LT comparison.
Args:
- rhs (
Self
): The other StringRef to compare against.
Returns:
True if this string is strictly less than the RHS string and False otherwise.
__le__
__le__(self: Self, rhs: Self) -> Bool
Compare this StringRef to the RHS using LE comparison.
Args:
- rhs (
Self
): The other StringRef to compare against.
Returns:
True if this string is less than or equal to the RHS string and False otherwise.
__eq__
__eq__(self: Self, rhs: Self) -> Bool
Compares two strings are equal.
Args:
- rhs (
Self
): The other string.
Returns:
True if the strings match and False otherwise.
__ne__
__ne__(self: Self, rhs: Self) -> Bool
Compares two strings are not equal.
Args:
- rhs (
Self
): The other string.
Returns:
True if the strings do not match and False otherwise.
__gt__
__gt__(self: Self, rhs: Self) -> Bool
Compare this StringRef to the RHS using GT comparison.
Args:
- rhs (
Self
): The other StringRef to compare against.
Returns:
True if this string is strictly greater than the RHS string and False otherwise.
__ge__
__ge__(self: Self, rhs: Self) -> Bool
Compare this StringRef to the RHS using GE comparison.
Args:
- rhs (
Self
): The other StringRef to compare against.
Returns:
True if this string is greater than or equal to the RHS string and False otherwise.
__contains__
__contains__(self: Self, substr: Self) -> Bool
Returns True if the substring is contained within the current string.
Args:
- substr (
Self
): The substring to check.
Returns:
True if the string contains the substring.
take_front
take_front(self: Self, num_bytes: Int = 1) -> Self
Return a StringRef equal to 'self' but with only the first num_bytes
elements remaining. If num_bytes
is greater than the length of the string, the entire string is returned.
Args:
- num_bytes (
Int
): The number of bytes to include.
Returns:
A new slice that starts with those bytes.
take_back
take_back(self: Self, num_bytes: Int = 1) -> Self
Return a StringRef equal to 'self' but with only the last num_bytes
elements remaining. If num_bytes
is greater than the length of the string, the entire string is returned.
Args:
- num_bytes (
Int
): The number of bytes to include.
Returns:
A new slice that ends with those bytes.
drop_front
drop_front(self: Self, num_bytes: Int = 1) -> Self
Return a StringRef equal to 'self' but with the first num_bytes
elements skipped. If num_bytes
is greater than the length of the string, an empty StringRef is returned.
Args:
- num_bytes (
Int
): The number of bytes to drop.
Returns:
A new slice with those bytes skipped.
drop_back
drop_back(self: Self, num_bytes: Int = 1) -> Self
Return a StringRef equal to 'self' but with the last num_bytes
elements skipped. If num_bytes
is greater than the length of the string, the entire string is returned.
Args: