From 3e426ce0b943bc7dd52741e2814c8965b099822a Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 15:28:37 -0300 Subject: [PATCH 1/2] Remove StringRef.split() Signed-off-by: martinvuyk --- stdlib/src/utils/stringref.mojo | 35 ---------------------- stdlib/test/utils/test_stringref.mojo | 42 --------------------------- 2 files changed, 77 deletions(-) diff --git a/stdlib/src/utils/stringref.mojo b/stdlib/src/utils/stringref.mojo index 75e864b405..486e70c713 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 - fn startswith( self, prefix: StringRef, start: Int = 0, end: Int = -1 ) -> Bool: diff --git a/stdlib/test/utils/test_stringref.mojo b/stdlib/test/utils/test_stringref.mojo index 0dc7686197..b1174b3c51 100644 --- a/stdlib/test/utils/test_stringref.mojo +++ b/stdlib/test/utils/test_stringref.mojo @@ -128,47 +128,6 @@ def test_endswith(): assert_true(ab.endswith("ab")) -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')") @@ -190,7 +149,6 @@ def test_str_and_ref(): def main(): test_strref_from_start() - test_stringref_split() test_comparison_operators() test_intable() test_indexing() From 5bdb554387b7b55a7404eea3cb9288b29d3749ab Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 15:31:00 -0300 Subject: [PATCH 2/2] add changelog entry Signed-off-by: martinvuyk --- docs/changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index cbae1be090..6c6f6447b1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -29,4 +29,7 @@ what we publish. ### ❌ Removed +- `StringRef` is being deprecated. Use `StringSlice` instead. + - Removed `StringRef.split()` + ### 🛠️ Fixed