Skip to content

Commit 1cc69ff

Browse files
committed
Add primitive implementation for String>>#charAt:
Signed-off-by: Stefan Marr <[email protected]>
1 parent b1d5754 commit 1cc69ff

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/som/primitives/string_primitives.py

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ def _substring(rcvr, start, end):
3939
return String(string[s:e])
4040

4141

42+
def _char_at(rcvr, idx):
43+
i = idx.get_embedded_integer() - 1
44+
string = rcvr.get_embedded_string()
45+
46+
if i < 0 or i >= len(string):
47+
return String("Error - index out of bounds")
48+
return String(string[i])
49+
50+
4251
def _hashcode(rcvr):
4352
return Integer(compute_hash(rcvr.get_embedded_string()))
4453

@@ -81,6 +90,7 @@ def _is_digits(self):
8190

8291
class StringPrimitivesBase(Primitives):
8392
def install_primitives(self):
93+
self._install_instance_primitive(BinaryPrimitive("charAt:", _char_at))
8494
self._install_instance_primitive(BinaryPrimitive("concatenate:", _concat))
8595
self._install_instance_primitive(UnaryPrimitive("asSymbol", _as_symbol))
8696
self._install_instance_primitive(UnaryPrimitive("length", _length))

0 commit comments

Comments
 (0)