* This ensures that the buffer is in proper utf8 format. * * If it isn't, it assumes any non-utf8 characters are Latin1, * and does the conversion. */
| 1661 | * and does the conversion. |
| 1662 | */ |
| 1663 | static int ensure_utf8(struct strbuf *buf) |
| 1664 | { |
| 1665 | int ok = 1; |
| 1666 | size_t pos = 0; |
| 1667 | |
| 1668 | for (;;) { |
| 1669 | size_t bad; |
| 1670 | unsigned char c; |
| 1671 | unsigned char replace[2]; |
| 1672 | |
| 1673 | if (!has_invalid_utf8(buf->buf + pos, buf->len - pos, &bad)) |
| 1674 | return ok; |
| 1675 | pos += bad; |
| 1676 | ok = 0; |
| 1677 | c = buf->buf[pos]; |
| 1678 | strbuf_remove(buf, pos, 1); |
| 1679 | |
| 1680 | /* We know 'c' must be in the range 128-255 */ |
| 1681 | replace[0] = 0xc0 + (c >> 6); |
| 1682 | replace[1] = 0x80 + (c & 0x3f); |
| 1683 | strbuf_insert(buf, pos, replace, 2); |
| 1684 | pos += 2; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | static const char commit_utf8_warn[] = |
| 1689 | N_("Warning: commit message did not conform to UTF-8.\n" |
no test coverage detected