modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo
Other
21.59k stars 2.52k forks source link

[BUG][mojo-stdlib] `String.find()` returns wrong value when starting index is non-zero #1355

Closed L29CtWQQV closed 5 months ago

L29CtWQQV commented 5 months ago

Bug description

I noticed, that String.find using an start-index, gives the difference between the start-index and the index of the sub-string occurrence. In Python on the other hand, the index of the occurrence is returned. So I think the Mojo behavior should match the Python one.

Steps to reproduce

Python:

>>> t = "...a"
>>> t.find("a",0)
3
>>> t.find("a",1)
3
>>> t.find("a",2)
3

Mojo REPL :

  1> var t : String = "...a"

  8> print(t.find("a",0)) 
3
  9> print(t.find("a",1))
2
 10> print(t.find("a",2))
1

System information

on Debian GNU/Linux 12 (bookworm)
with mojo version:
mojo 0.5.0 (6e50a738)
python version:
Python 3.11.2
JoeLoser commented 5 months ago

This is unintuitive and should be fixed - thanks for reporting it. I agree that the Mojo Standard Library should match the Python implementation here. Regardless of what start offset you pass, the caller should reasonably expect the right offset from the beginning of the string should be returned based on the found substring.

ConnorGray commented 5 months ago

Thanks for the report! This is fixed now, and will be in the the next Mojo release.