encode characters into utf-8 in format with our API

master
Daniel Kolesa 2018-01-01 03:11:00 +01:00
parent 8e6852572c
commit c35f7377bf
1 changed files with 7 additions and 23 deletions

View File

@ -1012,28 +1012,20 @@ private:
template<typename R, typename C> template<typename R, typename C>
void write_char_raw(R &writer, C val) const { void write_char_raw(R &writer, C val) const {
if constexpr(std::is_same_v<C, char>) { if constexpr(sizeof(C) == sizeof(std::uint8_t)) {
writer.put(val); writer.put(val);
} else { } else {
/* TODO: replace this ugly thing with custom unicode APIs */ char32_t enc;
char buf[MB_LEN_MAX]; if constexpr(sizeof(C) == sizeof(std::uint16_t)) {
int n; enc = static_cast<std::uint16_t>(val);
if constexpr(std::is_same_v<C, wchar_t>) {
/* convert according to locale */
n = detail::wc_to_mb_loc(val, buf, p_loc);
} else { } else {
fmt_codecvt<C> fac{}; enc = static_cast<std::uint32_t>(val);
n = detail::ac_to_mb(val, fac, buf);
} }
if (n < 0) { if (!utf::encode_u8(writer, enc)) {
/* replacement character */ /* replacement character on failure */
writer.put(0xEF); writer.put(0xEF);
writer.put(0xBF); writer.put(0xBF);
writer.put(0xBD); writer.put(0xBD);
} else {
for (int i = 0; i < n; ++i) {
writer.put(buf[i]);
}
} }
} }
} }
@ -1553,14 +1545,6 @@ private:
~fmt_num_put() {} ~fmt_num_put() {}
}; };
template<typename C>
struct fmt_codecvt final: std::codecvt<C, char, std::mbstate_t> {
fmt_codecvt(std::size_t refs = 0):
std::codecvt<C, char, std::mbstate_t>(refs)
{}
~fmt_codecvt() {}
};
string_range p_fmt; string_range p_fmt;
std::locale p_loc; std::locale p_loc;
char p_buf[32]; char p_buf[32];