* Append a single UTF-8 character to a buffer, converting it to locale * encoding. Returns the number of columns consumed by that character, * as best we can determine it. */
| 1172 | * as best we can determine it. |
| 1173 | */ |
| 1174 | static ssize_t |
| 1175 | xo_buf_append_locale_from_utf8 (xo_handle_t *xop, xo_buffer_t *xbp, |
| 1176 | const char *ibuf, ssize_t ilen) |
| 1177 | { |
| 1178 | wchar_t wc; |
| 1179 | ssize_t len; |
| 1180 | |
| 1181 | /* |
| 1182 | * Build our wide character from the input buffer; the number of |
| 1183 | * bits we pull off the first character is dependent on the length, |
| 1184 | * but we put 6 bits off all other bytes. |
| 1185 | */ |
| 1186 | wc = xo_utf8_char(ibuf, ilen); |
| 1187 | if (wc == (wchar_t) -1) { |
| 1188 | xo_failure(xop, "invalid UTF-8 byte sequence"); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | if (XOF_ISSET(xop, XOF_NO_LOCALE)) { |
| 1193 | if (!xo_buf_has_room(xbp, ilen)) |
| 1194 | return 0; |
| 1195 | |
| 1196 | memcpy(xbp->xb_curp, ibuf, ilen); |
| 1197 | xbp->xb_curp += ilen; |
| 1198 | |
| 1199 | } else { |
| 1200 | if (!xo_buf_has_room(xbp, MB_LEN_MAX + 1)) |
| 1201 | return 0; |
| 1202 | |
| 1203 | bzero(&xop->xo_mbstate, sizeof(xop->xo_mbstate)); |
| 1204 | len = wcrtomb(xbp->xb_curp, wc, &xop->xo_mbstate); |
| 1205 | |
| 1206 | if (len <= 0) { |
| 1207 | xo_failure(xop, "could not convert wide char: %lx", |
| 1208 | (unsigned long) wc); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | xbp->xb_curp += len; |
| 1212 | } |
| 1213 | |
| 1214 | return xo_wcwidth(wc); |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Append a UTF-8 string to a buffer, converting it into locale encoding |
no test coverage detected