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

AsciiBuffer.indexOf returns incorrect result for 2-bytes characters #2352

Merged
merged 2 commits into from
Sep 12, 2022

Conversation

idelpivnitskiy
Copy link
Member

Motivation:

AsciiBuffer.indexOf casts char to byte by dropping the higher 8 bits. As the result, it may return an index of an ascii character that has the same lower 8 bits.

Modifications:

  • Check character length and return -1 if it's 2-bytes character;

Result:

Correct result when users try to find an index of a 2-bytes character in AsciiBuffer. Consistency with other CharSequence implementations, like String and StringBuilder.

Motivation:

`AsciiBuffer.indexOf` casts `char` to `byte` by dropping the higher 8
bits. As the result, it may return an index of an ascii character that
has the same lower 8 bits.

Modifications:

- Check character length and return `-1` if it's 2-bytes character;

Result:

Correct result when users try to find an index of a 2-bytes character in
`AsciiBuffer`. Consistency with other `CharSequence` implementations,
like `String` and `StringBuilder`.
Copy link
Member

@Scottmitch Scottmitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

@@ -114,9 +114,16 @@ Buffer unwrap() {
* @throws NullPointerException if {@code subString} is {@code null}.
*/
int indexOf(char ch, int start) {
if (!singleByte(ch)) {
return -1;
}
return buffer.indexOf(start, buffer.writerIndex(), (byte) ch);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: simplify and remove negation: return singleByte(ch) ? buffer.indexOf(start, buffer.writerIndex(), (byte) ch) : -1;

@idelpivnitskiy idelpivnitskiy merged commit f11310c into apple:main Sep 12, 2022
@idelpivnitskiy idelpivnitskiy deleted the indexOf branch September 12, 2022 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants