Add additional string and list Methods · go-python/gpython@59f6a86 · GitHub
Skip to content

Commit 59f6a86

Browse files
kellrottncw
authored andcommitted
Add additional string and list Methods
This adds - string.startswith - string.endswith - list.append - list.extend
1 parent 7f6244e commit 59f6a86

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

py/list.go

Lines changed: 23 additions & 0 deletions

py/string.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,65 @@ func init() {
100100
}
101101
return &o, nil
102102
}, 0, "split(sub) -> split string with sub.")
103+
104+
StringType.Dict["startswith"] = MustNewMethod("startswith", func(self Object, args Tuple) (Object, error) {
105+
selfStr := string(self.(String))
106+
prefix := []string{}
107+
if len(args) > 0 {
108+
if s, ok := args[0].(String); ok {
109+
prefix = append(prefix, string(s))
110+
} else if s, ok := args[0].(Tuple); ok {
111+
for _, t := range s {
112+
if v, ok := t.(String); ok {
113+
prefix = append(prefix, string(v))
114+
}
115+
}
116+
} else {
117+
return nil, ExceptionNewf(TypeError, "startswith first arg must be str, unicode, or tuple, not %s", args[0].Type())
118+
}
119+
} else {
120+
return nil, ExceptionNewf(TypeError, "startswith() takes at least 1 argument (0 given)")
121+
}
122+
if len(args) > 1 {
123+
if s, ok := args[1].(Int); ok {
124+
selfStr = selfStr[s:len(selfStr)]
125+
}
126+
}
127+
128+
for _, s := range prefix {
129+
if strings.HasPrefix(selfStr, s) {
130+
return Bool(true), nil
131+
}
132+
}
133+
return Bool(false), nil
134+
}, 0, "startswith(prefix[, start[, end]]) -> bool")
135+
136+
StringType.Dict["endswith"] = MustNewMethod("endswith", func(self Object, args Tuple) (Object, error) {
137+
selfStr := string(self.(String))
138+
suffix := []string{}
139+
if len(args) > 0 {
140+
if s, ok := args[0].(String); ok {
141+
suffix = append(suffix, string(s))
142+
} else if s, ok := args[0].(Tuple); ok {
143+
for _, t := range s {
144+
if v, ok := t.(String); ok {
145+
suffix = append(suffix, string(v))
146+
}
147+
}
148+
} else {
149+
return nil, ExceptionNewf(TypeError, "endswith first arg must be str, unicode, or tuple, not %s", args[0].Type())
150+
}
151+
} else {
152+
return nil, ExceptionNewf(TypeError, "endswith() takes at least 1 argument (0 given)")
153+
}
154+
for _, s := range suffix {
155+
if strings.HasSuffix(selfStr, s) {
156+
return Bool(true), nil
157+
}
158+
}
159+
return Bool(false), nil
160+
}, 0, "endswith(suffix[, start[, end]]) -> bool")
161+
103162
}
104163

105164
// Type of this object

py/tests/list.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Use of this source code is governed by a BSD-style
33
# license that can be found in the LICENSE file.
44

5+
from libtest import assertRaises
6+
57
doc="str"
68
assert str([]) == "[]"
79
assert str([1,2,3]) == "[1, 2, 3]"
@@ -22,4 +24,13 @@
2224
assert idxs[idx] == a[idx][0]
2325
assert values[idx] == a[idx][1]
2426

27+
doc="append"
28+
a = [1,2,3]
29+
a.append(4)
30+
assert repr(a) == "[1, 2, 3, 4]"
31+
a = ['a', 'b', 'c']
32+
a.extend(['d', 'e', 'f'])
33+
assert repr(a) == "['a', 'b', 'c', 'd', 'e', 'f']"
34+
assertRaises(TypeError, lambda: [].append())
35+
2536
doc="finished"

py/tests/string.py

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)