diff --git a/ostd/string.hh b/ostd/string.hh index 4235e10..c9e07c0 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -850,10 +850,11 @@ String to_string(std::initializer_list init) { return to_string(iter(init)); } -template +template struct TempCString { private: - T *p_buf; + using Value = RemoveConst>; + Value *p_buf; bool p_allocated; public: @@ -863,15 +864,14 @@ public: s.p_buf = nullptr; s.p_allocated = false; } - TempCString(CharRangeBase input, T *sbuf, Size bufsize) + TempCString(R input, Value *sbuf, Size bufsize) : p_buf(nullptr), p_allocated(false) { - if (!input.size()) return; + if (input.empty()) return; if (input.size() >= bufsize) { - p_buf = new T[input.size() + 1]; + p_buf = new Value[input.size() + 1]; p_allocated = true; } else p_buf = sbuf; - memcpy(p_buf, input.data(), input.size()); - p_buf[input.size()] = '\0'; + p_buf[input.copy(p_buf)] = '\0'; } ~TempCString() { if (p_allocated) delete[] p_buf; @@ -883,8 +883,8 @@ public: return *this; } - operator const T *() const { return p_buf; } - const T *get() const { return p_buf; } + operator const Value *() const { return p_buf; } + const Value *get() const { return p_buf; } void swap(TempCString &s) { detail::swap_adl(p_buf, s.p_buf); @@ -892,9 +892,10 @@ public: } }; -inline TempCString to_temp_cstr(ConstCharRange input, - char *buf, Size bufsize) { - return TempCString(input, buf, bufsize); +template +inline TempCString to_temp_cstr(R input, RemoveConst> *buf, + Size bufsize) { + return TempCString(input, buf, bufsize); } } /* namespace ostd */