Skip to content

Adding strip, rstrip and lstrip methods to string class #214

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 1 commit into from
Jan 11, 2023
Merged
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
60 changes: 60 additions & 0 deletions py/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ replaced.`)
return Bool(false), nil
}, 0, "startswith(prefix[, start[, end]]) -> bool")

StringType.Dict["strip"] = MustNewMethod("strip", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
return self.(String).Strip(args)
}, 0, "strip(chars) -> replace chars from begining and end of string")

StringType.Dict["rstrip"] = MustNewMethod("rstrip", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
return self.(String).RStrip(args)
}, 0, "rstrip(chars) -> replace chars from end of string")

StringType.Dict["lstrip"] = MustNewMethod("lstrip", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
return self.(String).LStrip(args)
}, 0, "lstrip(chars) -> replace chars from begining of string")

}

// Type of this object
Expand Down Expand Up @@ -679,6 +691,54 @@ func (s String) Replace(args Tuple) (Object, error) {
return String(strings.Replace(string(s), old, new, cnt)), nil
}

func stripFunc(args Tuple) (func(rune) bool, error) {
var (
pyval Object = None
)
err := ParseTuple(args, "|s", &pyval)
if err != nil {
return nil, err
}
f := unicode.IsSpace
switch v := pyval.(type) {
case String:
chars := []rune(string(v))
f = func(s rune) bool {
for _, i := range chars {
if s == i {
return true
}
}
return false
}
}
return f, nil
}

func (s String) Strip(args Tuple) (Object, error) {
f, err := stripFunc(args)
if err != nil {
return nil, err
}
return String(strings.TrimFunc(string(s), f)), nil
}

func (s String) LStrip(args Tuple) (Object, error) {
f, err := stripFunc(args)
if err != nil {
return nil, err
}
return String(strings.TrimLeftFunc(string(s), f)), nil
}

func (s String) RStrip(args Tuple) (Object, error) {
f, err := stripFunc(args)
if err != nil {
return nil, err
}
return String(strings.TrimRightFunc(string(s), f)), nil
}

// Check stringerface is satisfied
var (
_ richComparison = String("")
Expand Down
11 changes: 11 additions & 0 deletions py/tests/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,17 @@ def index(s, i):
assert uni[7:7:2] == ''
assert uni[7:7:3] == ''

doc="string strip methods"
a = " adfasd "
assert a.rstrip() == " adfasd"
assert a.lstrip() == "adfasd "
assert a.strip() == "adfasd"

a = " a bada a"
assert a.rstrip("a ") == " a bad"
assert a.lstrip("a ") == "bada a"
assert a.strip("a ") == "bad"

class Index:
def __index__(self):
return 1
Expand Down