diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-07-08 15:54:05 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-07-08 15:54:05 +0000 |
commit | fc7be18c76af2e700641f3598c4856baeef1428e (patch) | |
tree | 11dbca45eb321c3a4dd08b12152acc6ef5dd3fa9 /library/cpp/yt/string/string-inl.h | |
parent | ec0e7ed6da6fb317741fd8468602949a1362eca5 (diff) | |
parent | c92cb9d3a19331916f0c274d80e67f02a62caa9b (diff) | |
download | ydb-fc7be18c76af2e700641f3598c4856baeef1428e.tar.gz |
Merge branch 'rightlib' into mergelibs-240708-1553
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 |