@@ -670,8 +670,6 @@ SlashValue str_to_str(Interpreter *interpreter, SlashValue self)
670
670
return self ;
671
671
}
672
672
673
-
674
- // SlashValue range_item_get(Interpreter *interpreter, SlashValue self, SlashValue other)
675
673
SlashValue str_item_get (Interpreter * interpreter , SlashValue self , SlashValue other )
676
674
{
677
675
assert (IS_STR (self ));
@@ -705,6 +703,27 @@ SlashValue str_item_get(Interpreter *interpreter, SlashValue self, SlashValue ot
705
703
return AS_VALUE (new );
706
704
}
707
705
706
+ void str_item_assign (Interpreter * interpreter , SlashValue self , SlashValue index , SlashValue other )
707
+ {
708
+ assert (IS_STR (self ));
709
+ assert (IS_STR (other ));
710
+ assert (IS_NUM (index )); // TODO: implement for range
711
+ if (!NUM_IS_INT (index ))
712
+ REPORT_RUNTIME_ERROR ("Str index can not be a floating point number: '%f'" , index .num );
713
+
714
+ SlashStr * str = AS_STR (self );
715
+ /* Ensure the index is valid */
716
+ int idx = (int )index .num ;
717
+ if (idx < 0 || (size_t )idx >= str -> len )
718
+ REPORT_RUNTIME_ERROR ("Str index '%d' out of range for str with len '%zu'" , idx , str -> len );
719
+
720
+ SlashStr * str_other = AS_STR (other );
721
+ if (str_other -> len != 1 )
722
+ REPORT_RUNTIME_ERROR ("Can only assign a string of length one, not length." );
723
+
724
+ str -> str [idx ] = str_other -> str [0 ];
725
+ }
726
+
708
727
bool str_item_in (SlashValue self , SlashValue other )
709
728
{
710
729
assert (IS_STR (self ) && IS_STR (other ));
@@ -968,7 +987,7 @@ SlashTypeInfo str_type_info = { .name = "str",
968
987
.print = str_print ,
969
988
.to_str = str_to_str ,
970
989
.item_get = str_item_get ,
971
- .item_assign = NULL ,
990
+ .item_assign = str_item_assign ,
972
991
.item_in = str_item_in ,
973
992
.truthy = str_truthy ,
974
993
.eq = str_eq ,
0 commit comments