Count the number of utf8 characters, ignoring invalid char, considering size 1
| 196 | |
| 197 | // Count the number of utf8 characters, ignoring invalid char, considering size 1 |
| 198 | FORCE_INLINE |
| 199 | gdv_int32 utf8_length_ignore_invalid(const char* data, gdv_int32 data_len) { |
| 200 | int char_len = 0; |
| 201 | int count = 0; |
| 202 | for (int i = 0; i < data_len; i += char_len) { |
| 203 | char_len = utf8_char_length(data[i]); |
| 204 | if (char_len == 0 || i + char_len > data_len) { // invalid byte or incomplete glyph |
| 205 | // if invalid byte or incomplete glyph, ignore it |
| 206 | char_len = 1; |
| 207 | } |
| 208 | for (int j = 1; j < char_len; ++j) { |
| 209 | if ((data[i + j] & 0xC0) != 0x80) { // bytes following head-byte of glyph |
| 210 | char_len += 1; |
| 211 | } |
| 212 | } |
| 213 | ++count; |
| 214 | } |
| 215 | return count; |
| 216 | } |
| 217 | |
| 218 | // Get the byte position corresponding to a character position for a non-empty utf8 |
| 219 | // sequence |
no outgoing calls
no test coverage detected