Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect results of Sting#index and String#rindex. #3513

Closed
arcage opened this issue Nov 7, 2016 · 1 comment
Closed

Incorrect results of Sting#index and String#rindex. #3513

arcage opened this issue Nov 7, 2016 · 1 comment

Comments

@arcage
Copy link
Contributor

arcage commented Nov 7, 2016

Hi,

It seem the Sting#index and String#rindex return incorrect results, in case that searches an ascii Char in the string that includes multibyte letters.

My environment:

  • Crystal 0.19.4 (2016-10-21)
  • macOS sierra 10.12.1

Code:

# the both of the following results should be 3.
p "あいう_えお".index('_')
#=> nil
p "あいう_えお".rindex('_')
#=> nil

When String#index or String#rindex is called with the search argument that is an ascii Char, it delegates the process to #to_slice.

But, index of string is not equal with it of string.to_slice when that string includes multibyte letters.

This causes the incorrect results of File.basename and File.extname.

Those methods are calling String#rindex with '.' or File::SEPARETOR(/ or '\\').

File.basename("日本語/日本語ファイル.txt")
#=> "ル.txt" (it shoud be "日本語ファイル.txt")

File.extname("日本語/日本語ファイル.txt")
#=> "" (it should be ".txt")
@asterite
Copy link
Member

asterite commented Nov 7, 2016

@arcage Thank you, you are totally correct! I applied this optimization recently but it was wrong, I was missing checking that the string is also made of ascii characters.

firejox pushed a commit to firejox/crystal that referenced this issue Dec 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants