* Calculate the length in characters of a null-terminated UTF-8 string. * * Returns -1 if the input is not valid UTF-8. */
| 999 | * Returns -1 if the input is not valid UTF-8. |
| 1000 | */ |
| 1001 | static int |
| 1002 | pg_utf8_string_len(const char *source) |
| 1003 | { |
| 1004 | const unsigned char *p = (const unsigned char *) source; |
| 1005 | int l; |
| 1006 | int num_chars = 0; |
| 1007 | |
| 1008 | while (*p) |
| 1009 | { |
| 1010 | l = pg_utf_mblen(p); |
| 1011 | |
| 1012 | if (!pg_utf8_islegal(p, l)) |
| 1013 | return -1; |
| 1014 | |
| 1015 | p += l; |
| 1016 | num_chars++; |
| 1017 | } |
| 1018 | |
| 1019 | return num_chars; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /* |
no test coverage detected