Skip to content

String and List Methods #61

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 5 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adding startswith/endswith methods to strings
  • Loading branch information
kellrott committed Jun 21, 2019
commit df338e331624145dbb30ebfe9175938fae3856f4
53 changes: 53 additions & 0 deletions py/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,59 @@ func init() {
}
return &o, nil
}, 0, "split(sub) -> split string with sub.")

StringType.Dict["startswith"] = MustNewMethod("startswith", func(self Object, args Tuple) (Object, error) {
selfStr := string(self.(String))
prefix := []string{}
if len(args) > 0 {
if s, ok := args[0].(String); ok {
prefix = append(prefix, string(s))
} else if s, ok := args[0].(Tuple); ok {
for _, t := range s {
if v, ok := t.(String); ok {
prefix = append(prefix, string(v))
}
}
} else {
return nil, ExceptionNewf(TypeError, "startswith first arg must be str, unicode, or tuple, not %s", args[0].Type())
}
} else {
return nil, ExceptionNewf(TypeError, "startswith() takes at least 1 argument (0 given)")
}
if len(args) > 1 {
if s, ok := args[1].(Int); ok {
selfStr = selfStr[s:len(selfStr)]
}
}

for _, s := range prefix {
if strings.HasPrefix(selfStr, s) {
return Bool(true), nil
}
}
return Bool(false), nil
}, 0, "startswith(prefix[, start[, end]]) -> bool")

StringType.Dict["endswith"] = MustNewMethod("endswith", func(self Object, args Tuple) (Object, error) {
selfStr := string(self.(String))
suffix := []string{}
if len(args) > 0 {
if s, ok := args[0].(String); ok {
suffix = append(suffix, string(s))
} else {
return nil, ExceptionNewf(TypeError, "endswith first arg must be str, unicode, or tuple, not %s", args[0].Type())
}
} else {
return nil, ExceptionNewf(TypeError, "endswith() takes at least 1 argument (0 given)")
}
for _, s := range suffix {
if strings.HasSuffix(selfStr, s) {
return Bool(true), nil
}
}
return Bool(false), nil
}, 0, "endswith(suffix[, start[, end]]) -> bool")

}

// Type of this object
Expand Down
10 changes: 10 additions & 0 deletions py/tests/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class C():
assert not( 1 == "potato")
assert 1 != "potato"

doc="startswith"
assert "HELLO THERE".startswith("HELL")
assert not "HELLO THERE".startswith("THERE")
assert "HELLO".startswith("LLO", 2)
assert "HELLO THERE".startswith(("HERE", "HELL"))

doc="endswith"
assert "HELLO THERE".endswith("HERE")
assert not "HELLO THERE".endswith("HELL")

doc="bool"
assert "true"
assert not ""
Expand Down