diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-07-02 15:28:09 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-07-02 15:40:39 +0300 |
commit | 3336f268eb26aa0318cd3fa3f97116c43545f87b (patch) | |
tree | 047395a3b7bf0751a86c5676a9c103e168e47e3c /library/cpp/yt/string/string-inl.h | |
parent | aa95a328ada306559bd361a96c5348829d0e562e (diff) | |
download | ydb-3336f268eb26aa0318cd3fa3f97116c43545f87b.tar.gz |
YT-21868: Reorganize files to prevent unclear missing include errors
Moving files around so that include of either `string_builder.h` or `format.h` would result in being able to use both `Format` and `TStringBuilder`
8a2abbea2ae7027c1cd3a243ab3de55bdd5d3d27
Diffstat (limited to 'library/cpp/yt/string/string-inl.h')
-rw-r--r-- | library/cpp/yt/string/string-inl.h | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/library/cpp/yt/string/string-inl.h b/library/cpp/yt/string/string-inl.h new file mode 100644 index 0000000000..baf789b4f1 --- /dev/null +++ b/library/cpp/yt/string/string-inl.h @@ -0,0 +1,158 @@ +#ifndef STRING_INL_H_ +#error "Direct inclusion of this file is not allowed, include string.h" +// For the sake of sane code completion. +#include "string.h" +#endif + +#include "format.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +//! Joins a range of items into a string intermixing them with the delimiter. +/*! + * \param builder String builder where the output goes. + * \param begin Iterator pointing to the first item (inclusive). + * \param end Iterator pointing to the last item (not inclusive). + * \param formatter Formatter to apply to the items. + * \param delimiter A delimiter to be inserted between items: ", " by default. + * \return The resulting combined string. + */ +template <class TIterator, class TFormatter> +void JoinToString( + TStringBuilderBase* builder, + const TIterator& begin, + const TIterator& end, + const TFormatter& formatter, + TStringBuf delimiter) +{ + for (auto current = begin; current != end; ++current) { + if (current != begin) { + builder->AppendString(delimiter); + } + formatter(builder, *current); + } +} + +template <class TIterator, class TFormatter> +TString JoinToString( + const TIterator& begin, + const TIterator& end, + const TFormatter& formatter, + TStringBuf delimiter) +{ + TStringBuilder builder; + JoinToString(&builder, begin, end, formatter, delimiter); + return builder.Flush(); +} + +//! A handy shortcut with default formatter. +template <class TIterator> +TString JoinToString( + const TIterator& begin, + const TIterator& end, + TStringBuf delimiter) +{ + return JoinToString(begin, end, TDefaultFormatter(), delimiter); +} + +//! Joins a collection of given items into a string intermixing them with the delimiter. +/*! + * \param collection A collection containing the items to be joined. + * \param formatter Formatter to apply to the items. + * \param delimiter A delimiter to be inserted between items; ", " by default. + */ +template <class TCollection, class TFormatter> +TString JoinToString( + const TCollection& collection, + const TFormatter& formatter, + TStringBuf delimiter) +{ + using std::begin; + using std::end; + return JoinToString(begin(collection), end(collection), formatter, delimiter); +} + +//! A handy shortcut with the default formatter. +template <class TCollection> +TString JoinToString( + const TCollection& collection, + TStringBuf delimiter) +{ + return JoinToString(collection, TDefaultFormatter(), delimiter); +} + +//! Concatenates a bunch of TStringBuf-like instances into TString. +template <class... Ts> +TString ConcatToString(Ts... args) +{ + size_t length = 0; + ((length += args.length()), ...); + + TString result; + result.reserve(length); + (result.append(args), ...); + + return result; +} + +//! Converts a range of items into strings. +template <class TIter, class TFormatter> +std::vector<TString> ConvertToStrings( + const TIter& begin, + const TIter& end, + const TFormatter& formatter, + size_t maxSize) +{ + std::vector<TString> result; + for (auto it = begin; it != end; ++it) { + TStringBuilder builder; + formatter(&builder, *it); + result.push_back(builder.Flush()); + if (result.size() == maxSize) { + break; + } + } + return result; +} + +//! A handy shortcut with the default formatter. +template <class TIter> +std::vector<TString> ConvertToStrings( + const TIter& begin, + const TIter& end, + size_t maxSize) +{ + return ConvertToStrings(begin, end, TDefaultFormatter(), maxSize); +} + +//! Converts a given collection of items into strings. +/*! + * \param collection A collection containing the items to be converted. + * \param formatter Formatter to apply to the items. + * \param maxSize Size limit for the resulting vector. + */ +template <class TCollection, class TFormatter> +std::vector<TString> ConvertToStrings( + const TCollection& collection, + const TFormatter& formatter, + size_t maxSize) +{ + using std::begin; + using std::end; + return ConvertToStrings(begin(collection), end(collection), formatter, maxSize); +} + +//! A handy shortcut with default formatter. +template <class TCollection> +std::vector<TString> ConvertToStrings( + const TCollection& collection, + size_t maxSize) +{ + return ConvertToStrings(collection, TDefaultFormatter(), maxSize); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT |