Skip to content

Commit c60d425

Browse files
authored
py: add strip, rstrip and lstrip methods to string class
1 parent 7512ac2 commit c60d425

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

py/string.go

+60
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ replaced.`)
206206
return Bool(false), nil
207207
}, 0, "startswith(prefix[, start[, end]]) -> bool")
208208

209+
StringType.Dict["strip"] = MustNewMethod("strip", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
210+
return self.(String).Strip(args)
211+
}, 0, "strip(chars) -> replace chars from begining and end of string")
212+
213+
StringType.Dict["rstrip"] = MustNewMethod("rstrip", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
214+
return self.(String).RStrip(args)
215+
}, 0, "rstrip(chars) -> replace chars from end of string")
216+
217+
StringType.Dict["lstrip"] = MustNewMethod("lstrip", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
218+
return self.(String).LStrip(args)
219+
}, 0, "lstrip(chars) -> replace chars from begining of string")
220+
209221
}
210222

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

694+
func stripFunc(args Tuple) (func(rune) bool, error) {
695+
var (
696+
pyval Object = None
697+
)
698+
err := ParseTuple(args, "|s", &pyval)
699+
if err != nil {
700+
return nil, err
701+
}
702+
f := unicode.IsSpace
703+
switch v := pyval.(type) {
704+
case String:
705+
chars := []rune(string(v))
706+
f = func(s rune) bool {
707+
for _, i := range chars {
708+
if s == i {
709+
return true
710+
}
711+
}
712+
return false
713+
}
714+
}
715+
return f, nil
716+
}
717+
718+
func (s String) Strip(args Tuple) (Object, error) {
719+
f, err := stripFunc(args)
720+
if err != nil {
721+
return nil, err
722+
}
723+
return String(strings.TrimFunc(string(s), f)), nil
724+
}
725+
726+
func (s String) LStrip(args Tuple) (Object, error) {
727+
f, err := stripFunc(args)
728+
if err != nil {
729+
return nil, err
730+
}
731+
return String(strings.TrimLeftFunc(string(s), f)), nil
732+
}
733+
734+
func (s String) RStrip(args Tuple) (Object, error) {
735+
f, err := stripFunc(args)
736+
if err != nil {
737+
return nil, err
738+
}
739+
return String(strings.TrimRightFunc(string(s), f)), nil
740+
}
741+
682742
// Check stringerface is satisfied
683743
var (
684744
_ richComparison = String("")

py/tests/string.py

+11
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,17 @@ def index(s, i):
886886
assert uni[7:7:2] == ''
887887
assert uni[7:7:3] == ''
888888

889+
doc="string strip methods"
890+
a = " adfasd "
891+
assert a.rstrip() == " adfasd"
892+
assert a.lstrip() == "adfasd "
893+
assert a.strip() == "adfasd"
894+
895+
a = " a bada a"
896+
assert a.rstrip("a ") == " a bad"
897+
assert a.lstrip("a ") == "bada a"
898+
assert a.strip("a ") == "bad"
899+
889900
class Index:
890901
def __index__(self):
891902
return 1

0 commit comments

Comments
 (0)