diff --git a/docs/changelog.md b/docs/changelog.md index 6babee4c83..9a66f56c6b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -75,6 +75,7 @@ what we publish. - `StringRef` is being deprecated. Use `StringSlice` instead. - removed `StringRef.startswith()` and `StringRef.endswith()` + - Removed `StringRef.split()` ### 🛠️ Fixed diff --git a/stdlib/src/utils/stringref.mojo b/stdlib/src/utils/stringref.mojo index 4e92e96848..7006582dc5 100644 --- a/stdlib/src/utils/stringref.mojo +++ b/stdlib/src/utils/stringref.mojo @@ -615,41 +615,6 @@ struct StringRef( end -= 1 return StringRef(ptr + start, end - start) - fn split(self, delimiter: StringRef) raises -> List[StringRef]: - """Split the StringRef by a delimiter. - - Args: - delimiter: The StringRef to split on. - - Returns: - A List of StringRefs containing the input split by the delimiter. - - Raises: - Error if an empty delimiter is specified. - """ - if not delimiter: - raise Error("empty delimiter not allowed to be passed to split.") - - var output = List[StringRef]() - var ptr = self.unsafe_ptr() - - var current_offset = 0 - while True: - var loc = self.find(delimiter, current_offset) - # delimiter not found, so add the search slice from where we're currently at - if loc == -1: - output.append( - StringRef(ptr + current_offset, len(self) - current_offset) - ) - break - - # We found a delimiter, so add the preceding string slice - output.append(StringRef(ptr + current_offset, loc - current_offset)) - - # Advance our search offset past the delimiter - current_offset = loc + len(delimiter) - return output - # ===-----------------------------------------------------------------------===# # Utilities diff --git a/stdlib/test/utils/test_stringref.mojo b/stdlib/test/utils/test_stringref.mojo index 67965d22a4..2469020f90 100644 --- a/stdlib/test/utils/test_stringref.mojo +++ b/stdlib/test/utils/test_stringref.mojo @@ -108,47 +108,6 @@ def test_find(): assert_equal(StringRef("").find("abc"), -1) -fn test_stringref_split() raises: - # Reject empty delimiters - with assert_raises( - contains="empty delimiter not allowed to be passed to split." - ): - _ = StringRef("hello").split("") - - # Split in middle - var d1 = StringRef("n") - var in1 = StringRef("faang") - var res1 = in1.split(d1) - assert_equal(len(res1), 2) - assert_equal(res1[0], "faa") - assert_equal(res1[1], "g") - - # Matches should be properly split in multiple case - var d2 = StringRef(" ") - var in2 = StringRef("modcon is coming soon") - var res2 = in2.split(d2) - assert_equal(len(res2), 4) - assert_equal(res2[0], "modcon") - assert_equal(res2[1], "is") - assert_equal(res2[2], "coming") - assert_equal(res2[3], "soon") - - # No match from the delimiter - var d3 = StringRef("x") - var in3 = StringRef("hello world") - var res3 = in3.split(d3) - assert_equal(len(res3), 1) - assert_equal(res3[0], "hello world") - - # Multiple character delimiter - var d4 = StringRef("ll") - var in4 = StringRef("hello") - var res4 = in4.split(d4) - assert_equal(len(res4), 2) - assert_equal(res4[0], "he") - assert_equal(res4[1], "o") - - def test_str_and_ref(): assert_equal(StringRef("abc").__str__(), "abc") assert_equal(StringRef("abc").__repr__(), "StringRef('abc')") @@ -170,7 +129,6 @@ def test_str_and_ref(): def main(): test_strref_from_start() - test_stringref_split() test_comparison_operators() test_intable() test_indexing()