wren icon indicating copy to clipboard operation
wren copied to clipboard

String: Add .lastIndexOf(search, start) to find last occurance

Open RobLoach opened this issue 4 years ago • 4 comments

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.

RobLoach avatar May 16 '21 19:05 RobLoach

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.

PureFox48 avatar May 16 '21 20:05 PureFox48

Do you think we need lastIndexOf() in List too?

ChayimFriedman2 avatar May 17 '21 21:05 ChayimFriedman2

It would make sense to add it to both I'd think.

joshgoebel avatar May 17 '21 22:05 joshgoebel

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.

PureFox48 avatar May 18 '21 09:05 PureFox48