From 893ec6f001b7ad82366f00ddcf2d5643632626f6 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 16:34:56 -0300 Subject: [PATCH 1/8] Remove StringLiteral StringRef dependencies Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 727ac65555..f655fbe748 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -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 @@ -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: @@ -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: @@ -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 @@ -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 @@ -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` From b58e64666f174c807d6621c14399ddd210e9a476 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 16:58:39 -0300 Subject: [PATCH 2/8] retry github CI Signed-off-by: martinvuyk From 0497d054fd2a77ac85e13823eabd31fa9eac6dd8 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 17:10:07 -0300 Subject: [PATCH 3/8] refactor stringslice eq Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 09372e41e1..8f73fa36d4 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -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() + 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: return True - for i in range(len(self)): - if self._slice[i] != rhs._slice.unsafe_ptr()[i]: - return False - return True + elif self._slice.unsafe_ptr() == rhs._slice.unsafe_ptr(): + return True # same pointer and length, so equal + return memcmp(self.unsafe_ptr(), rhs.unsafe_ptr(), s_len) == 0 @always_inline fn __eq__(self, rhs: String) -> Bool: From b5c9adfc4da6ddc43420b943b82a0f120a785f4f Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 17:13:17 -0300 Subject: [PATCH 4/8] fix detail Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 8f73fa36d4..ad41956fca 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -429,13 +429,12 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( """ var s_len = self.byte_length() + s_ptr, rhs_ptr = self.unsafe_ptr(), rhs.unsafe_ptr() if s_len != rhs.byte_length(): return False - elif s_len == 0: + elif s_len == 0 or s_ptr == rhs_ptr: # same pointer and length, so equal return True - elif self._slice.unsafe_ptr() == rhs._slice.unsafe_ptr(): - return True # same pointer and length, so equal - return memcmp(self.unsafe_ptr(), rhs.unsafe_ptr(), s_len) == 0 + return memcmp(s_ptr, rhs_ptr, s_len) == 0 @always_inline fn __eq__(self, rhs: String) -> Bool: From c44cc2dfde3a399bb79c0d48a97336d8b7449f0f Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 17:14:50 -0300 Subject: [PATCH 5/8] mojo format Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index ad41956fca..a862fef8df 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -432,7 +432,9 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( s_ptr, rhs_ptr = self.unsafe_ptr(), rhs.unsafe_ptr() if s_len != rhs.byte_length(): return False - elif s_len == 0 or s_ptr == rhs_ptr: # same pointer and length, so equal + elif ( + s_len == 0 or s_ptr == rhs_ptr + ): # same pointer and length, so equal return True return memcmp(s_ptr, rhs_ptr, s_len) == 0 From f7b9fe091d3bd2be9c0b5c1d7604f7da583028a0 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 17:15:26 -0300 Subject: [PATCH 6/8] mojo format Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index a862fef8df..75f439b96d 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -432,9 +432,8 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( s_ptr, rhs_ptr = self.unsafe_ptr(), rhs.unsafe_ptr() if s_len != rhs.byte_length(): return False - elif ( - s_len == 0 or s_ptr == rhs_ptr - ): # same pointer and length, so equal + # same pointer and length, so equal + elif s_len == 0 or s_ptr == rhs_ptr: return True return memcmp(s_ptr, rhs_ptr, s_len) == 0 From ec9607941d857eab6b91271bea0f574994f7b61b Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 13 Dec 2024 15:24:36 -0300 Subject: [PATCH 7/8] separate var assignment Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 75f439b96d..4c6a5b2ff8 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -429,7 +429,8 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( """ var s_len = self.byte_length() - s_ptr, rhs_ptr = self.unsafe_ptr(), rhs.unsafe_ptr() + var s_ptr = self.unsafe_ptr() + var rhs_ptr = rhs.unsafe_ptr() if s_len != rhs.byte_length(): return False # same pointer and length, so equal From 70f7ac2667000ddbf470f73cfae1762075d04052 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 26 Dec 2024 09:49:37 -0300 Subject: [PATCH 8/8] change import Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 04d7c7fa80..14ab9af03b 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -17,11 +17,16 @@ These are Mojo built-ins, so you don't need to import them. from collections import List from collections.string.format import _CurlyEntryFormattable, _FormatCurlyEntry -from collections.string.string_slice import _StringSliceIter, _to_string_list +from collections.string.string_slice import ( + StringSlice, + StaticString, + _StringSliceIter, + _to_string_list, +) from hashlib._hasher import _HashableWithHasher, _Hasher from memory import UnsafePointer, memcpy, Span from sys.ffi import c_char -from utils import StaticString, StringSlice, Writable, Writer +from utils import Writable, Writer from utils._visualizers import lldb_formatter_wrapping_type