123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #ifndef STRING_INL_H_
- #error "Direct inclusion of this file is not allowed, include string.h"
- #include "string.h"
- #endif
- #include "format.h"
- namespace NYT {
- 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();
- }
- template <class TIterator>
- TString JoinToString(
- const TIterator& begin,
- const TIterator& end,
- TStringBuf delimiter)
- {
- return JoinToString(begin, end, TDefaultFormatter(), delimiter);
- }
- 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);
- }
- template <class TCollection>
- TString JoinToString(
- const TCollection& collection,
- TStringBuf delimiter)
- {
- return JoinToString(collection, TDefaultFormatter(), delimiter);
- }
- template <class... Ts>
- TString ConcatToString(Ts... args)
- {
- size_t length = 0;
- ((length += args.length()), ...);
- TString result;
- result.reserve(length);
- (result.append(args), ...);
- return result;
- }
- 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;
- }
- template <class TIter>
- std::vector<TString> ConvertToStrings(
- const TIter& begin,
- const TIter& end,
- size_t maxSize)
- {
- return ConvertToStrings(begin, end, TDefaultFormatter(), maxSize);
- }
- 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);
- }
- template <class TCollection>
- std::vector<TString> ConvertToStrings(
- const TCollection& collection,
- size_t maxSize)
- {
- return ConvertToStrings(collection, TDefaultFormatter(), maxSize);
- }
- }
|