wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.86k stars 550 forks source link

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

Open RobLoach opened 3 years ago

RobLoach commented 3 years ago

Allow for backwards searching with .indexOf(search, start) by passing a negative number.

Alternatively, we could take JavaScript's approach of lastIndexOf instead.

PureFox48 commented 3 years ago

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.

ChayimFriedman2 commented 3 years ago

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

joshgoebel commented 3 years ago

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

PureFox48 commented 3 years ago

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.