Skip to content

Commit

Permalink
Add primitive implementation for String>>#charAt:
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Apr 4, 2024
1 parent b1d5754 commit 1cc69ff
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/som/primitives/string_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def _substring(rcvr, start, end):
return String(string[s:e])


def _char_at(rcvr, idx):
i = idx.get_embedded_integer() - 1
string = rcvr.get_embedded_string()

if i < 0 or i >= len(string):
return String("Error - index out of bounds")
return String(string[i])


def _hashcode(rcvr):
return Integer(compute_hash(rcvr.get_embedded_string()))

Expand Down Expand Up @@ -81,6 +90,7 @@ def _is_digits(self):

class StringPrimitivesBase(Primitives):
def install_primitives(self):
self._install_instance_primitive(BinaryPrimitive("charAt:", _char_at))
self._install_instance_primitive(BinaryPrimitive("concatenate:", _concat))
self._install_instance_primitive(UnaryPrimitive("asSymbol", _as_symbol))
self._install_instance_primitive(UnaryPrimitive("length", _length))
Expand Down

0 comments on commit 1cc69ff

Please sign in to comment.