Skip to content

Commit 4258b79

Browse files
committed
Use explicit int size in utf8 apis
1 parent 71fbe82 commit 4258b79

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/include/utf8.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#define HERB_UTF8_H
33

44
#include <stdbool.h>
5+
#include <stdint.h>
56
#include <stdlib.h>
67

7-
int utf8_char_byte_length(unsigned char first_byte);
8-
int utf8_sequence_length(const char* str, size_t position, size_t max_length);
8+
uint32_t utf8_char_byte_length(unsigned char first_byte);
9+
uint32_t utf8_sequence_length(const char* str, size_t position, size_t max_length);
910
bool utf8_is_valid_continuation_byte(unsigned char byte);
1011

1112
#endif

src/utf8.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// 110xxxxx = 2 bytes
66
// 1110xxxx = 3 bytes
77
// 11110xxx = 4 bytes
8-
int utf8_char_byte_length(unsigned char first_byte) {
8+
uint32_t utf8_char_byte_length(unsigned char first_byte) {
99
if ((first_byte & 0x80) == 0) {
1010
return 1;
1111
} else if ((first_byte & 0xE0) == 0xC0) {
@@ -24,18 +24,18 @@ bool utf8_is_valid_continuation_byte(unsigned char byte) {
2424
return (byte & 0xC0) == 0x80;
2525
}
2626

27-
int utf8_sequence_length(const char* str, size_t position, size_t max_length) {
27+
uint32_t utf8_sequence_length(const char* str, size_t position, size_t max_length) {
2828
if (position >= max_length) { return 0; }
2929

3030
unsigned char first_byte = (unsigned char) str[position];
31-
int expected_length = utf8_char_byte_length(first_byte);
31+
uint32_t expected_length = utf8_char_byte_length(first_byte);
3232

3333
if (position + expected_length > max_length) {
3434
return 1; // Not enough bytes, treat as single byte
3535
}
3636

3737
if (expected_length > 1) {
38-
for (int i = 1; i < expected_length; i++) {
38+
for (uint32_t i = 1; i < expected_length; i++) {
3939
if (!utf8_is_valid_continuation_byte((unsigned char) str[position + i])) {
4040
return 1; // Invalid continuation byte, treat first byte as single byte
4141
}

0 commit comments

Comments
 (0)