// Copyright 2023 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include namespace DB { class FmtBuffer { public: FmtBuffer() = default; // See https://github.com/fmtlib/fmt/issues/2391 for wrapping the fmt API template FmtBuffer & fmtAppend(fmt::format_string fmt, T &&... args) { fmt::format_to(std::back_inserter(buffer), fmt, std::forward(args)...); return *this; } template < typename CompiledFormat, // typename... Args, fmt::enable_if_t<(fmt::detail::is_compiled_format::value), int> = 0> constexpr FmtBuffer & fmtAppend(const CompiledFormat & cf, const Args &... args) { fmt::format_to(std::back_inserter(buffer), cf, std::forward(args)...); return *this; } template < typename S, // typename... Args, fmt::enable_if_t<(fmt::detail::is_compiled_string::value), int> = 0> constexpr FmtBuffer & fmtAppend(const S & s, Args &&... args) { fmt::format_to(std::back_inserter(buffer), s, std::forward(args)...); return *this; } FmtBuffer & append(std::string_view s) { buffer.append(s.data(), s.data() + s.size()); return *this; } FmtBuffer & append(const char ch) { buffer.push_back(ch); return *this; } std::string toString() const { return fmt::to_string(buffer); } template FmtBuffer & joinStr(Iter first, Iter end) { return joinStr(first, end, ", "); } template FmtBuffer & joinStr(Iter first, Iter end, std::string_view delimiter) { auto func = [](const auto & s, FmtBuffer & fb) { fb.append(s); }; return joinStr(first, end, func, delimiter); } template FmtBuffer & joinStr( Iter first, Iter end, FF && toStringFunc, // void (const auto &, FmtBuffer &) std::string_view delimiter) { if (first == end) return *this; toStringFunc(*first, *this); ++first; for (; first != end; ++first) { append(delimiter); toStringFunc(*first, *this); } return *this; } void resize(size_t count) { buffer.resize(count); } void reserve(size_t capacity) { buffer.reserve(capacity); } size_t capacity() { return buffer.capacity(); } size_t size() { return buffer.size(); } void clear() { buffer.clear(); } private: fmt::memory_buffer buffer; }; } // namespace DB