From c35f7377bfbf260a73810a461fa9fed78d6d467b Mon Sep 17 00:00:00 2001 From: q66 Date: Mon, 1 Jan 2018 03:11:00 +0100 Subject: [PATCH] encode characters into utf-8 in format with our API --- ostd/format.hh | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/ostd/format.hh b/ostd/format.hh index c027168..37b7394 100644 --- a/ostd/format.hh +++ b/ostd/format.hh @@ -1012,28 +1012,20 @@ private: template void write_char_raw(R &writer, C val) const { - if constexpr(std::is_same_v) { + if constexpr(sizeof(C) == sizeof(std::uint8_t)) { writer.put(val); } else { - /* TODO: replace this ugly thing with custom unicode APIs */ - char buf[MB_LEN_MAX]; - int n; - if constexpr(std::is_same_v) { - /* convert according to locale */ - n = detail::wc_to_mb_loc(val, buf, p_loc); + char32_t enc; + if constexpr(sizeof(C) == sizeof(std::uint16_t)) { + enc = static_cast(val); } else { - fmt_codecvt fac{}; - n = detail::ac_to_mb(val, fac, buf); + enc = static_cast(val); } - if (n < 0) { - /* replacement character */ + if (!utf::encode_u8(writer, enc)) { + /* replacement character on failure */ writer.put(0xEF); writer.put(0xBF); writer.put(0xBD); - } else { - for (int i = 0; i < n; ++i) { - writer.put(buf[i]); - } } } } @@ -1553,14 +1545,6 @@ private: ~fmt_num_put() {} }; - template - struct fmt_codecvt final: std::codecvt { - fmt_codecvt(std::size_t refs = 0): - std::codecvt(refs) - {} - ~fmt_codecvt() {} - }; - string_range p_fmt; std::locale p_loc; char p_buf[32];