ruby / net-imap

Ruby client api for Internet Message Access Protocol
https://ruby.github.io/net-imap
Other
49 stars 25 forks source link

Use String#byteindex instead of String#index #286

Open shugo opened 1 month ago

shugo commented 1 month ago

Because String#index takes O(N), where N is the second argument, while String#byteindex takes O(1).

shugo commented 1 month ago

We should wait until Ruby 3.1 gets EOL.

nevans commented 1 month ago

@shugo I added it to a new v0.6 milestone. 🙂

admsev commented 1 month ago

@gooroodev WDYT?

gooroodev commented 1 month ago

Thanks, @admsev! Happy to help!

1. Summary of Changes

The pull request makes the following changes:

2. Issues, Bugs, and Typos

Issue 1: Inconsistent Handling of Return Values

The original code uses String#index which returns the character index, while the new code uses String#byteindex which returns the byte index. This change can affect the handling of multibyte characters. However, the code seems to handle byte offsets correctly by using $~.byteoffset(0)[1].

Issue 2: Typo in Comment

The comment "an 'accept' versiun of #text" contains a typo. The word "versiun" should be "version".

Proposed Fix:

# an "accept" version of #text

Issue 3: Consistency in Method Naming

The method accept_spaces uses byteindex while other methods like next_token still use index in some places. For consistency, all instances should be updated to use byteindex.

Proposed Fix: Replace all instances of index with byteindex in the next_token method.

3. General Review of Code Quality and Style

Code Quality

Style

Proposed Code Improvements

Improvement 1: Consistent Use of byteindex

Current Code:

@str.index(/\S*/n, @pos)

Improved Code:

@str.byteindex(/\S*/n, @pos)

Improvement 2: Fix Typo in Comment

Current Code:

# an "accept" versiun of #text

Improved Code:

# an "accept" version of #text

Improvement 3: Handle Multibyte Characters Consistently

Ensure all instances of index are replaced with byteindex where necessary to handle multibyte characters correctly.

Conclusion

The pull request effectively updates the code to use String#byteindex instead of String#index, which is a positive change for handling multibyte characters. The proposed improvements should be implemented to maintain consistency and fix minor issues. Overall, the code quality and style are good, and the changes are well-justified.

Yours, Gooroo.dev. I'd love to hear your feedback! React or reply.

nevans commented 1 month ago

@admsev @gooroodev please don't. It's obnoxious and not very helpful.