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

Fix decoding of UTF-8 sequence length (fix #922) #923

Merged
merged 1 commit into from
Aug 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions jv_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ int jvp_utf8_is_valid(const char* in, const char* end) {
return 1;
}

/* Assumes startchar is the first byte of a valid character sequence */
int jvp_utf8_decode_length(char startchar) {
if ((startchar & 0x80) == 0) return 1;
else if ((startchar & 0xC0) == 0xC0) return 2;
else if ((startchar & 0xE0) == 0xE0) return 3;
else return 4;
if ((startchar & 0x80) == 0) return 1; // 0___ ____
else if ((startchar & 0xE0) == 0xC0) return 2; // 110_ ____
else if ((startchar & 0xF0) == 0xE0) return 3; // 1110 ____
else return 4; // 1111 ____
}

int jvp_utf8_encode_length(int codepoint) {
Expand Down
5 changes: 5 additions & 0 deletions tests/onig.test
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ gsub("(?<d>\\d)"; ":\(.d);")
gsub("(?<x>.)[^a]*"; "+\(.x)-")
"Abcabc"
"+A-+a-"

# utf-8
sub("(?<x>.)"; "\(.x)!")
"’"
"’!"