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

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

py/list.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ type List struct {
1313
Items []Object
1414
}
1515

16+
func init() {
17+
ListType.Dict["append"] = MustNewMethod("append", func(self Object, args Tuple) (Object, error) {
18+
listSelf := self.(*List)
19+
if len(args) != 1 {
20+
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
21+
}
22+
listSelf.Items = append(listSelf.Items, args[0])
23+
return NoneType{}, nil
24+
}, 0, "append(item)")
25+
26+
ListType.Dict["extend"] = MustNewMethod("extend", func(self Object, args Tuple) (Object, error) {
27+
listSelf := self.(*List)
28+
if len(args) != 1 {
29+
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
30+
}
31+
if oList, ok := args[0].(*List); ok {
32+
listSelf.Items = append(listSelf.Items, oList.Items...)
33+
}
34+
return NoneType{}, nil
35+
}, 0, "extend([item])")
36+
37+
}
38+
1639
// Type of this List object
1740
func (o *List) Type() *Type {
1841
return ListType

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
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ class C():
5959
assert not( 1 == "potato")
6060
assert 1 != "potato"
6161

62+
doc="startswith"
63+
assert "HELLO THERE".startswith("HELL")
64+
assert not "HELLO THERE".startswith("THERE")
65+
assert "HELLO".startswith("LLO", 2)
66+
assert "HELLO THERE".startswith(("HERE", "HELL"))
67+
68+
doc="endswith"
69+
assert "HELLO THERE".endswith("HERE")
70+
assert not "HELLO THERE".endswith("HELL")
71+
assert "HELLO THERE".endswith(("HELL", "HERE"))
72+
6273
doc="bool"
6374
assert "true"
6475
assert not ""

0 commit comments

Comments
 (0)