From b2d656e481f11372a323579274994e2e3eaae32d Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 22 Jul 2015 01:21:27 +0100 Subject: [PATCH] use ConstCharRange when formatting strings --- ostd/format.hh | 52 ++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/ostd/format.hh b/ostd/format.hh index 9b08e42..ee62972 100644 --- a/ostd/format.hh +++ b/ostd/format.hh @@ -579,16 +579,16 @@ namespace detail { return nullptr; } - inline String escape_fmt_str(const char *val) { + inline String escape_fmt_str(ConstCharRange val) { String ret; ret.push('"'); - while (*val) { - const char *esc = escape_fmt_char(*val, '"'); + while (!val.empty()) { + const char *esc = escape_fmt_char(val.front(), '"'); if (esc) ret.append(esc); else - ret.push(*val); - ++val; + ret.push(val.front()); + val.pop_front(); } ret.push('"'); return ret; @@ -629,39 +629,31 @@ namespace detail { WriteSpec(): FormatSpec() {} WriteSpec(ConstCharRange fmt, bool esc): FormatSpec(fmt, esc) {} - /* C string */ + /* string base writer */ template - Ptrdiff write(R &writer, bool escape, const char *val, Size n) { + Ptrdiff write_str(R &writer, bool escape, ConstCharRange val) { if (escape) { - String esc = escape_fmt_str(val); - return write(writer, false, (const char *)esc.data(), - esc.size()); + return write_str(writer, false, escape_fmt_str(val)); } + Size n = val.size(); if (this->precision()) n = this->precision(); Ptrdiff r = n; r += this->write_spaces(writer, n, true); - writer.put_n(val, n); + writer.put_n(&val[0], n); r += this->write_spaces(writer, n, false); return r; } - template - Ptrdiff write(R &writer, bool escape, const char *val) { + /* any string value */ + template + Ptrdiff write(R &writer, bool escape, const T &val, EnableIf< + IsConstructible::value, bool + > = true) { if (this->spec() != 's') { assert(false && "cannot print strings with the given spec"); return -1; } - return write(writer, escape, val, strlen(val)); - } - - /* OctaSTD string */ - template - Ptrdiff write(R &writer, bool escape, const AnyString &val) { - if (this->spec() != 's') { - assert(false && "cannot print strings with the given spec"); - return -1; - } - return write(writer, escape, val.data(), val.size()); + return write_str(writer, escape, val); } /* character */ @@ -756,7 +748,9 @@ namespace detail { /* pointer value */ template - Ptrdiff write(R &writer, bool, T *val) { + Ptrdiff write(R &writer, bool, T *val, EnableIf< + !IsConstructible::value, bool + > = true) { if (this->p_spec == 's') { this->p_spec = 'x'; this->p_flags |= FMT_FLAG_HASH; @@ -767,7 +761,9 @@ namespace detail { /* generic value */ template Ptrdiff write(R &writer, bool, const T &val, EnableIf< - !IsArithmetic::value && FmtTostrTest::value && + !IsArithmetic::value && + !IsConstructible::value && + FmtTostrTest::value && !FmtTofmtTest>::value, bool > = true) { if (this->spec() != 's') { @@ -790,7 +786,9 @@ namespace detail { /* generic failure case */ template Ptrdiff write(R &, bool, const T &, EnableIf< - !IsArithmetic::value && !FmtTostrTest::value && + !IsArithmetic::value && + !IsConstructible::value && + !FmtTostrTest::value && !FmtTofmtTest>::value, bool > = true) { assert(false && "value cannot be formatted");