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 StringRef.split() #3865

Open
wants to merge 3 commits into
base: nightly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ what we publish.

- `StringRef` is being deprecated. Use `StringSlice` instead.
- removed `StringRef.startswith()` and `StringRef.endswith()`
- Removed `StringRef.split()`

### 🛠️ Fixed

Expand Down
35 changes: 0 additions & 35 deletions stdlib/src/utils/stringref.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 0 additions & 42 deletions stdlib/test/utils/test_stringref.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -108,47 +108,6 @@ def test_find():
assert_equal(StringRef("").find("abc"), -1)


fn test_stringref_split() raises:
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
# 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')")
Expand All @@ -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()
Expand Down
Loading