Fix unicode character counting bug #17
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug when counting unicode characters in
num_utf8_chars()
.The bug is in the code that does the bit shift to check the top two bits. Because the type is a signed char, when the top bit is set (indicating unicode) the value gets sign extended to an int (0xffffffXX) which then fails the comparison. The correct way to do this check would be:
((src[i] & 0xff) >> 6) != 2
.During investigation of the bug, I realized the reason for counting unicode characters was to determine if the string was all ASCII or not. I also realized a faster way of doing this check would be to check the top bit of all characters in the string and return when one is found. This way, the loop will return as soon as a non-ASCII character is encountered.
The following changes were made:
num_utf8_chars()
tois_ascii()
, update the logic to iterate through the string until a non-ASCII character is found, and updateunicode_from_str()
to useis_ascii()
.