Skip to content

Adding split method to string class #60

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

Merged
merged 6 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions py/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.`, StrNew, nil)


func init() {
StringType.Dict["split"] = MustNewMethod("split", func(self Object, value Object) (Object, error) {
selfStr := self.(String)
if valStr, ok := value.(String); ok {
ss := strings.Split(string(selfStr), string(valStr))
o := List{}
for _, j := range ss {
o.Items = append(o.Items, String(j))
}
return &o, nil
}
return nil, fmt.Errorf("Not split by string")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a TypeError

>>> str.split(8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'split' requires a 'str' object but received a 'int'

}, 0, "split(sub) -> split string with sub.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python help is

    S.split([sep [,maxsplit]]) -> list of strings
    
    Return a list of the words in the string S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are removed
    from the result.

Which makes me see that we are missing two things.

  • If sep isn't passed (or is None) in then we should be using strings.Fields in go terms (This doesn't have a maxfields parameter though.)
  • There is another optional parameter to specify the number of splits.

Here are some cases to consider

>>> "a,d,c".split(",")
['a', 'd', 'c']
>>> "a,d,c".split(",",1)
['a', 'd,c']
>>> " a   d   b   ".split()
['a', 'd', 'b']
>>> " a   d   b   ".split(None, 1)
['a', 'd   b   ']

}

// Type of this object
func (s String) Type() *Type {
return StringType
Expand Down
3 changes: 3 additions & 0 deletions py/tests/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class C():
asc="hello"
uni="£100世界𠜎" # 1,2,3,4 byte unicode characters

doc="split"
assert 4 == len(list("0,1,2,4".split(",")))

doc="ascii len"
assert len(asc) == 5

Expand Down