-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add join argument allowing formating list of values separated by a #466
Changes from 4 commits
6c87e34
7bc91f8
61a1ab2
74b1f38
6c092d5
19cc665
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3865,6 +3865,73 @@ void BasicFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) { | |
} | ||
write(writer_, start, s); | ||
} | ||
|
||
|
||
template <typename Char, typename It> | ||
struct ArgJoin | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The brace belongs to the previous line. Otherwise looks good. |
||
It first; | ||
It last; | ||
BasicCStringRef<Char> sep; | ||
|
||
ArgJoin(It first, It last, const BasicCStringRef<Char>& sep) : | ||
first(first), | ||
last(last), | ||
sep(sep) {} | ||
}; | ||
|
||
template <typename It> | ||
ArgJoin<char, It> join(It first, It last, const BasicCStringRef<char>& sep) { | ||
return ArgJoin<char, It>(first, last, sep); | ||
} | ||
|
||
template <typename It> | ||
ArgJoin<wchar_t, It> join(It first, It last, const BasicCStringRef<wchar_t>& sep) { | ||
return ArgJoin<wchar_t, It>(first, last, sep); | ||
} | ||
|
||
#if FMT_HAS_GXX_CXX11 | ||
template <typename Range> | ||
auto join(const Range& range, const BasicCStringRef<char>& sep) | ||
-> ArgJoin<char, decltype(std::begin(range))> { | ||
return join(std::begin(range), std::end(range), sep); | ||
} | ||
|
||
template <typename Range> | ||
auto join(const Range& range, const BasicCStringRef<wchar_t>& sep) | ||
-> ArgJoin<wchar_t, decltype(std::begin(range))> { | ||
return join(std::begin(range), std::end(range), sep); | ||
} | ||
#endif | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here |
||
template <typename ArgFormatter, typename Char, typename It> | ||
void format_arg(fmt::BasicFormatter<Char, ArgFormatter> &f, | ||
const Char *&format_str, const ArgJoin<Char, It>& e) { | ||
const Char* end = format_str; | ||
if (*end == ':') | ||
++end; | ||
while (*end && *end != '}') | ||
++end; | ||
if (*end != '}') | ||
FMT_THROW(FormatError("missing '}' in format string")); | ||
|
||
It it = e.first; | ||
if (it != e.last) { | ||
const Char* save = format_str; | ||
f.format(format_str, internal::MakeArg<fmt::BasicFormatter<Char, ArgFormatter> >(*it++)); | ||
while (it != e.last) { | ||
f.writer().write(e.sep); | ||
format_str = save; | ||
f.format(format_str, internal::MakeArg<fmt::BasicFormatter<Char, ArgFormatter> >(*it++)); | ||
} | ||
} | ||
format_str = end + 1; | ||
} | ||
|
||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and a bunch here |
||
} // namespace fmt | ||
|
||
#if FMT_USE_USER_DEFINED_LITERALS | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra empty line