Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Remove StringLiteral StringRef dependencies #3869

12 changes: 6 additions & 6 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ from sys.ffi import c_char

from memory import UnsafePointer, memcpy, Span

from utils import StaticString, StringRef, StringSlice, Writable, Writer
from utils import StaticString, StringSlice, Writable, Writer
from utils._visualizers import lldb_formatter_wrapping_type
from utils.format import _CurlyEntryFormattable, _FormatCurlyEntry
from utils.string_slice import _StringSliceIter, _to_string_list
Expand Down Expand Up @@ -241,7 +241,7 @@ struct StringLiteral(
Returns:
True if they are not equal.
"""
return StringRef(self) != StringRef(rhs)
return self.as_string_slice() != rhs.as_string_slice()

@always_inline("nodebug")
fn __eq__(self, rhs: StringSlice) -> Bool:
Expand Down Expand Up @@ -277,7 +277,7 @@ struct StringLiteral(
Returns:
True if this StringLiteral is strictly less than the RHS StringLiteral and False otherwise.
"""
return StringRef(self) < StringRef(rhs)
return self.as_string_slice() < rhs.as_string_slice()

@always_inline("nodebug")
fn __le__(self, rhs: StringLiteral) -> Bool:
Expand Down Expand Up @@ -324,7 +324,7 @@ struct StringLiteral(
Returns:
True if the string contains the substring.
"""
return substr in StringRef(self)
return substr in self.as_string_slice()

# ===-------------------------------------------------------------------===#
# Trait implementations
Expand Down Expand Up @@ -606,7 +606,7 @@ struct StringLiteral(
Returns:
The offset of `substr` relative to the beginning of the string.
"""
return StringRef(self).find(substr, start=start)
return self.as_string_slice().find(substr, start=start)

fn rfind(self, substr: StringLiteral, start: Int = 0) -> Int:
"""Finds the offset of the last occurrence of `substr` starting at
Expand All @@ -619,7 +619,7 @@ struct StringLiteral(
Returns:
The offset of `substr` relative to the beginning of the string.
"""
return StringRef(self).rfind(substr, start=start)
return self.as_string_slice().rfind(substr, start=start)

fn replace(self, old: StringLiteral, new: StringLiteral) -> StringLiteral:
"""Return a copy of the string with all occurrences of substring `old`
Expand Down
14 changes: 6 additions & 8 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,15 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]](
Returns:
If the `StringSlice` is equal to the input in length and contents.
"""
if not self and not rhs:
return True
if len(self) != len(rhs):

var s_len = self.byte_length()
s_ptr, rhs_ptr = self.unsafe_ptr(), rhs.unsafe_ptr()
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
if s_len != rhs.byte_length():
return False
# same pointer and length, so equal
if self._slice.unsafe_ptr() == rhs._slice.unsafe_ptr():
elif s_len == 0 or s_ptr == rhs_ptr:
return True
for i in range(len(self)):
if self._slice[i] != rhs._slice.unsafe_ptr()[i]:
return False
return True
return memcmp(s_ptr, rhs_ptr, s_len) == 0

@always_inline
fn __eq__(self, rhs: String) -> Bool:
Expand Down
Loading