Skip to content

Commit c6c0fbd

Browse files
committed
Implement ASCII only str.__getitem__
1 parent e57a0df commit c6c0fbd

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎py/string.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,24 @@ func (a String) M__imod__(other Object) Object{
164164
returna.M__mod__(other)
165165
}
166166

167+
func (sString) M__getitem__(keyObject) Object{
168+
// FIXME doesn't take into account unicode yet - ASCII only!!!
169+
ifslice, ok:=key.(*Slice); ok{
170+
start, stop, step, slicelength:=slice.GetIndices(len(s))
171+
ifstep==1{
172+
// Return a subslice since strings are immutable
173+
returns[start:stop]
174+
}
175+
newString:=make([]byte, slicelength)
176+
fori, j:=start, 0; j<slicelength; i, j=i+step, j+1{
177+
newString[j] =s[i]
178+
}
179+
returnString(newString)
180+
}
181+
i:=IndexIntCheck(key, len(s))
182+
returns[i : i+1]
183+
}
184+
167185
// Check stringerface is satisfied
168186
var_richComparison=String("")
169187
var_sequenceArithmetic=String("")
@@ -172,3 +190,4 @@ var _ I__rmod__ = String("")
172190
var_I__imod__=String("")
173191
var_I__len__=String("")
174192
var_I__bool__=String("")
193+
var_I__getitem__=String("")

0 commit comments

Comments
(0)