* Returns first character length in bytes for multi-byte `text` according to * `encoding`. * * - The `text` pointer is updated to point at the next character. * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes * we can consume from text, and on exit `*remainder_p` is reduced by returned * character length. Otherwise `text` is treated as limited by NUL. */
| 666 | * character length. Otherwise `text` is treated as limited by NUL. |
| 667 | */ |
| 668 | int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding) |
| 669 | { |
| 670 | int chrlen; |
| 671 | const char *p = *text; |
| 672 | size_t r = (remainder_p ? *remainder_p : SIZE_MAX); |
| 673 | |
| 674 | if (r < 1) |
| 675 | return 0; |
| 676 | |
| 677 | if (is_encoding_utf8(encoding)) { |
| 678 | pick_one_utf8_char(&p, &r); |
| 679 | |
| 680 | chrlen = p ? (p - *text) |
| 681 | : 1 /* not valid UTF-8 -> raw byte sequence */; |
| 682 | } |
| 683 | else { |
| 684 | /* |
| 685 | * TODO use iconv to decode one char and obtain its chrlen |
| 686 | * for now, let's treat encodings != UTF-8 as one-byte |
| 687 | */ |
| 688 | chrlen = 1; |
| 689 | } |
| 690 | |
| 691 | *text += chrlen; |
| 692 | if (remainder_p) |
| 693 | *remainder_p -= chrlen; |
| 694 | |
| 695 | return chrlen; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Pick the next char from the stream, ignoring codepoints an HFS+ would. |
no test coverage detected