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] Add split overloads #4015

Open
wants to merge 5 commits into
base: main
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
69 changes: 55 additions & 14 deletions mojo/stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -687,58 +687,99 @@ struct StringLiteral(
"""
return String(elems, sep=self)

fn split(self, sep: StringSlice, maxsplit: Int = -1) raises -> List[String]:
"""Split the string literal by a separator.
@always_inline
fn split(self, sep: StringSlice, maxsplit: Int) raises -> List[String]:
"""Split the string by a separator.

Args:
sep: The string to split on.
maxsplit: The maximum amount of items to split from String.
Defaults to unlimited.

Returns:
A List of Strings containing the input split by the separator.

Examples:
```mojo
# Splitting with maxsplit
_ = "1,2,3".split(",", maxsplit=1) # ['1', '2,3']
# Splitting with starting or ending separators
_ = ",1,2,3,".split(",", maxsplit=1) # ['', '1,2,3,']
```
.
"""
# TODO(#3528): add this example
# _ = "123".split("", maxsplit=1) # ['', '123']
return _to_string_list(self.as_string_slice().split(sep, maxsplit))

@always_inline
fn split(self, sep: StringSlice) raises -> List[String]:
"""Split the string by a separator.

Args:
sep: The string to split on.

Returns:
A List of Strings containing the input split by the separator.

Examples:
```mojo
# Splitting a space
_ = "hello world".split(" ") # ["hello", "world"]
# Splitting adjacent separators
_ = "hello,,world".split(",") # ["hello", "", "world"]
# Splitting with starting or ending separators
_ = ",1,2,3,".split(",") # ['', '1', '2', '3', '']
```
.
"""
# TODO(#3528): add this example
# _ = "123".split("") # ['', '1', '2', '3', '']
return _to_string_list(self.as_string_slice().split(sep, -1))

@always_inline
fn split(self, *, maxsplit: Int) -> List[String]:
"""Split the string by every Whitespace separator.

Args:
maxsplit: The maximum amount of items to split from String.

Returns:
A List of Strings containing the input split by the separator.

Examples:
```mojo
# Splitting with maxsplit
_ = "1,2,3".split(",", 1) # ['1', '2,3']
_ = "1 2 3".split(maxsplit=1) # ['1', '2 3']
```
.
"""
return String(self).split(sep, maxsplit)
return _to_string_list(self.as_string_slice().split(maxsplit=maxsplit))

fn split(self, sep: NoneType = None, maxsplit: Int = -1) -> List[String]:
"""Split the string literal by every whitespace separator.
@always_inline
fn split(self, sep: NoneType = None) -> List[String]:
"""Split the string by every Whitespace separator.

Args:
sep: None.
maxsplit: The maximum amount of items to split from string. Defaults
to unlimited.

Returns:
A List of Strings containing the input split by the separator.

Examples:

```mojo
# Splitting an empty string or filled with whitespaces
_ = " ".split() # []
_ = "".split() # []

# Splitting a string with leading, trailing, and middle whitespaces
_ = " hello world ".split() # ["hello", "world"]
# Splitting adjacent universal newlines:
_ = "hello \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e\\x85\\u2028\\u2029world".split()
# ["hello", "world"]
_ = (
"hello \\t\\n\\r\\f\\v\\x1c\\x1d\\x1e\\x85\\u2028\\u2029world"
).split() # ["hello", "world"]
```
.
"""
return String(self).split(sep, maxsplit)
return _to_string_list(self.as_string_slice().split(sep))

fn splitlines(self, keepends: Bool = False) -> List[String]:
"""Split the string literal at line boundaries. This corresponds to Python's
Expand Down
92 changes: 58 additions & 34 deletions mojo/stdlib/src/collections/string/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1413,76 +1413,100 @@ struct String(
return self.as_string_slice().isspace()

# TODO(MSTDL-590): String.split() should return `StringSlice`s.
fn split(self, sep: StringSlice, maxsplit: Int = -1) raises -> List[String]:
# FIX: #3528
@always_inline
fn split(self, sep: StringSlice, maxsplit: Int) raises -> List[String]:
"""Split the string by a separator.

Args:
sep: The string to split on.
maxsplit: The maximum amount of items to split from String.
Defaults to unlimited.

Returns:
A List of Strings containing the input split by the separator.

Raises:
If the separator is empty.

Examples:
```mojo
# Splitting with maxsplit
_ = "1,2,3".split(",", maxsplit=1) # ['1', '2,3']
# Splitting with starting or ending separators
_ = ",1,2,3,".split(",", maxsplit=1) # ['', '1,2,3,']
```
.
"""
# TODO(#3528): add this example
# _ = "123".split("", maxsplit=1) # ['', '123']
return _to_string_list(self.as_string_slice().split(sep, maxsplit))

@always_inline
fn split(self, sep: StringSlice) raises -> List[String]:
"""Split the string by a separator.

Args:
sep: The string to split on.

Returns:
A List of Strings containing the input split by the separator.

Examples:
```mojo
# Splitting a space
_ = String("hello world").split(" ") # ["hello", "world"]
_ = "hello world".split(" ") # ["hello", "world"]
# Splitting adjacent separators
_ = String("hello,,world").split(",") # ["hello", "", "world"]
_ = "hello,,world".split(",") # ["hello", "", "world"]
# Splitting with starting or ending separators
_ = ",1,2,3,".split(",") # ['', '1', '2', '3', '']
```
.
"""
# TODO(#3528): add this example
# _ = "123".split("") # ['', '1', '2', '3', '']
return _to_string_list(self.as_string_slice().split(sep, -1))

@always_inline
fn split(self, *, maxsplit: Int) -> List[String]:
"""Split the string by every Whitespace separator.

Args:
maxsplit: The maximum amount of items to split from String.

Returns:
A List of Strings containing the input split by the separator.

Examples:
```mojo
# Splitting with maxsplit
_ = String("1,2,3").split(",", 1) # ['1', '2,3']
_ = "1 2 3".split(maxsplit=1) # ['1', '2 3']
```
.
"""
return self.as_string_slice().split[sep.mut, sep.origin](
sep, maxsplit=maxsplit
)
return _to_string_list(self.as_string_slice().split(maxsplit=maxsplit))

fn split(self, sep: NoneType = None, maxsplit: Int = -1) -> List[String]:
@always_inline
fn split(self, sep: NoneType = None) -> List[String]:
"""Split the string by every Whitespace separator.

Args:
sep: None.
maxsplit: The maximum amount of items to split from String. Defaults
to unlimited.

Returns:
A List of Strings containing the input split by the separator.

Examples:

```mojo
# Splitting an empty string or filled with whitespaces
_ = String(" ").split() # []
_ = String("").split() # []

_ = " ".split() # []
_ = "".split() # []
# Splitting a string with leading, trailing, and middle whitespaces
_ = String(" hello world ").split() # ["hello", "world"]
_ = " hello world ".split() # ["hello", "world"]
# Splitting adjacent universal newlines:
_ = String(
"hello \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e\\x85\\u2028\\u2029world"
_ = (
"hello \\t\\n\\r\\f\\v\\x1c\\x1d\\x1e\\x85\\u2028\\u2029world"
).split() # ["hello", "world"]
```
.
"""

# TODO(MSTDL-590): Avoid the need to loop to convert `StringSlice` to
# `String` by making `String.split()` return `StringSlice`s.
var str_slices = self.as_string_slice()._split_whitespace(
maxsplit=maxsplit
)

var output = List[String](capacity=len(str_slices))

for str_slice in str_slices:
output.append(String(str_slice[]))

return output^
return _to_string_list(self.as_string_slice().split(sep))

fn splitlines(self, keepends: Bool = False) -> List[String]:
"""Split the string at line boundaries. This corresponds to Python's
Expand Down
Loading