* Read a single UTF-8 character starting at @s, * returning the length, in bytes, of the character read. * * This function assumes input is valid UTF-8, * and that there are enough characters in front of @s. */
| 237 | * and that there are enough characters in front of @s. |
| 238 | */ |
| 239 | static int utf8_read_char(const char *s, uchar_t *out) |
| 240 | { |
| 241 | const unsigned char *c = (const unsigned char*) s; |
| 242 | |
| 243 | ASSERT(utf8_validate_cz(s)); |
| 244 | |
| 245 | if (c[0] <= 0x7F) |
| 246 | { |
| 247 | /* 00..7F */ |
| 248 | *out = c[0]; |
| 249 | return 1; |
| 250 | } |
| 251 | else if (c[0] <= 0xDF) |
| 252 | { |
| 253 | /* C2..DF (unless input is invalid) */ |
| 254 | *out = ((uchar_t)c[0] & 0x1F) << 6 | |
| 255 | ((uchar_t)c[1] & 0x3F); |
| 256 | return 2; |
| 257 | } |
| 258 | else if (c[0] <= 0xEF) |
| 259 | { |
| 260 | /* E0..EF */ |
| 261 | *out = ((uchar_t)c[0] & 0xF) << 12 | |
| 262 | ((uchar_t)c[1] & 0x3F) << 6 | |
| 263 | ((uchar_t)c[2] & 0x3F); |
| 264 | return 3; |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | /* F0..F4 (unless input is invalid) */ |
| 269 | *out = ((uchar_t)c[0] & 0x7) << 18 | |
| 270 | ((uchar_t)c[1] & 0x3F) << 12 | |
| 271 | ((uchar_t)c[2] & 0x3F) << 6 | |
| 272 | ((uchar_t)c[3] & 0x3F); |
| 273 | return 4; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Write a single UTF-8 character to @s, |
no test coverage detected