Skip to main content

struct

StringLiteral

This type represents a string literal.

String literals are all null-terminated for compatibility with C APIs, but this is subject to change. String literals store their length as an integer, and this does not include the null terminator.

Aliases

  • type = string:

Fields

  • value (string): The underlying storage for the string literal.

Implemented traits

AnyType, Boolable, CollectionElement, Comparable, Copyable, EqualityComparable, Formattable, Hashable, IntableRaising, KeyElement, Movable, Representable, Sized, Stringable

Methods

__init__

__init__(inout self: Self, value: string)

Create a string literal from a builtin string type.

Args:

  • value (string): The string value.

__bool__

__bool__(self: Self) -> Bool

Convert the string to a bool value.

Returns:

True if the string is not empty.

__lt__

__lt__(self: Self, rhs: Self) -> Bool

Compare this StringLiteral to the RHS using LT comparison.

Args:

  • rhs (Self): The other StringLiteral to compare against.

Returns:

True if this StringLiteral is strictly less than the RHS StringLiteral and False otherwise.

__le__

__le__(self: Self, rhs: Self) -> Bool

Compare this StringLiteral to the RHS using LE comparison.

Args:

  • rhs (Self): The other StringLiteral to compare against.

Returns:

True if this StringLiteral is less than or equal to the RHS StringLiteral and False otherwise.

__eq__

__eq__(self: Self, rhs: Self) -> Bool

Compare two string literals for equality.

Args:

  • rhs (Self): The string to compare.

Returns:

True if they are equal.

__ne__

__ne__(self: Self, rhs: Self) -> Bool

Compare two string literals for inequality.

Args:

  • rhs (Self): The string to compare.

Returns:

True if they are not equal.

__gt__

__gt__(self: Self, rhs: Self) -> Bool

Compare this StringLiteral to the RHS using GT comparison.

Args:

  • rhs (Self): The other StringLiteral to compare against.

Returns:

True if this StringLiteral is strictly greater than the RHS StringLiteral and False otherwise.

__ge__

__ge__(self: Self, rhs: Self) -> Bool

Compare this StringLiteral to the RHS using GE comparison.

Args:

  • rhs (Self): The other StringLiteral to compare against.

Returns:

True if this StringLiteral is greater than or equal to the RHS StringLiteral 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.

__add__

__add__(self: Self, rhs: Self) -> Self

Concatenate two string literals.

Args:

  • rhs (Self): The string to concat.

Returns:

The concatenated string.

unsafe_ptr

unsafe_ptr(self: Self) -> UnsafePointer[SIMD[int8, 1], 0]

Get raw pointer to the underlying data.

Returns:

The raw pointer to the data.

unsafe_uint8_ptr

unsafe_uint8_ptr(self: Self) -> UnsafePointer[SIMD[uint8, 1], 0]

Get raw pointer to the underlying data.

Returns:

The raw pointer to the data.

as_uint8_ptr

as_uint8_ptr(self: Self) -> DTypePointer[uint8, 0]

Get raw pointer to the underlying data.

Returns:

The raw pointer to the data.

as_string_slice

as_string_slice(self: Self) -> StringSlice[0, #lit.lifetime]

Returns a string slice of this static string literal.

Returns:

A string slice pointing to this static string literal.

as_bytes_slice

as_bytes_slice(self: Self) -> Span[SIMD[uint8, 1], 0, #lit.lifetime]

Returns a contiguous slice of the bytes owned by this string.

Returns:

A contiguous slice pointing to the bytes owned by this string.

format_to

format_to(self: Self, inout writer: Formatter)

Formats this string literal to the provided formatter.

Args:

  • writer (Formatter): The formatter to write to.

find

find(self: Self, substr: Self, start: Int = 0) -> Int

Finds the offset of the first occurrence of substr starting at start. If not found, returns -1.

Args:

  • substr (Self): 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: Self, substr: Self, start: Int = 0) -> Int

Finds the offset of the last occurrence of substr starting at start. If not found, returns -1.

Args:

  • substr (Self): The substring to find.
  • start (Int): The offset from which to find.

Returns:

The offset of substr relative to the beginning of the string.