wren
wren copied to clipboard
String: Add .lastIndexOf(search, start) to find last occurance
Allow for backwards searching with .indexOf(search, start) by passing a negative number.
- https://wren.io/modules/core/string.html#indexof(search,-start)
- https://github.com/wren-lang/wren/blob/main/src/vm/wren_core.c#L1086-L1089
Alternatively, we could take JavaScript's approach of lastIndexOf instead.
Well, start can already be a negative number which means, in effect, that it starts searching forwards from index str.bytes.count + start.
So, if we are to have a version which searches backwards from the end of the string, it will have to be called lastIndexOf or similar.
Do you think we need lastIndexOf() in List too?
It would make sense to add it to both I'd think.
Yes, if we do one then we should do both.
List.lastIndexOf() would also be easier to implement as you wouldn't need to worry about being in the middle of a UTF-8 byte sequence when searching backwards through a string's bytes.