Add utility function for joining a sequence of strings with a separator
Categories
(Core :: XPCOM, enhancement)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox86 | --- | fixed |
People
(Reporter: sg, Assigned: sg)
References
Details
Attachments
(2 files)
A utility function for joining a sequence of strings with a separator would be helpful (along the lines of https://www.boost.org/doc/libs/1_71_0/boost/algorithm/string/join.hpp).
For a sequence that is a standard C++ range, this might be implemented (with proper templating) using
std::accumulate(sequence.cbegin(), sequence.cend(), nsCString{},
[](nsCString &str, const nsCString &item) -> decltype(auto) { return s += item; });
In addition to the basic variant, a function accepting a functor that appends to the result string on each iteration, would also be helpful, which can be called like this:
nsTArray<int> sequence = {1, 2, 3};
const auto res = StringJoin(sequence, NS_LITERAL_CSTRING(", "), [](nsCString& str, int item) { str. Append('"'); str.AppendInt(item); str. Append('"'); });
resulting in res == NS_LITERAL_CSTRING("\"1\", \"2\", \"3\"").
Besides sequences that are standard C++ ranges, the utility functions should also work with containers such as nsTHashtable via ConstIter (or nsTHashtable is made a C++ range as well).
| Assignee | ||
Updated•4 years ago
|
| Assignee | ||
Comment 1•4 years ago
|
||
| Assignee | ||
Comment 2•4 years ago
|
||
Depends on D98749
| Assignee | ||
Comment 3•4 years ago
|
||
nsTHashtable is a C++ range now.
Updated•4 years ago
|
Comment 5•4 years ago
|
||
| bugherder | ||
Comment 7•4 years ago
|
||
Backed out for causing bustage on nsReadableUtils.h
backout: https://hg.mozilla.org/integration/autoland/rev/36e58b614815fb3da6d7840d6f787b6ec2eed618
failure log: https://treeherder.mozilla.org/logviewer?job_id=324743715&repo=autoland&lineNumber=6468
[task 2020-12-16T20:31:27.455Z] 20:31:27 INFO - In file included from /builds/worker/workspace/obj-build/dist/include/nsString.h:18:
[task 2020-12-16T20:31:27.455Z] 20:31:27 ERROR - /builds/worker/workspace/obj-build/dist/include/nsReadableUtils.h:456:15: error: loop variable 'item' is always a copy because the range of type 'const mozilla::EnumSet<unsigned long, unsigned long>' does not return a reference [-Werror,-Wrange-loop-analysis]
[task 2020-12-16T20:31:27.455Z] 20:31:27 INFO - for (auto&& item : aInputRange) {
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - ^
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - /builds/worker/workspace/obj-build/dist/include/nsReadableUtils.h:484:3: note: in instantiation of function template specialization 'StringJoinAppend<char, mozilla::EnumSet<unsigned long, unsigned long>, (lambda at /builds/worker/workspace/obj-build/dist/include/mozilla/dom/SyncedContextInlines.h:25:21)>' requested here
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - StringJoinAppend(res, aSeparator, aInputRange, std::forward<Func>(aFunc));
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - ^
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - /builds/worker/workspace/obj-build/dist/include/mozilla/dom/SyncedContextInlines.h:38:23: note: in instantiation of function template specialization 'mozilla::dom::syncedcontext::FormatValidationError<mozilla::dom::BrowsingContext>' requested here
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - nsCString error = FormatValidationError<Context>(
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - ^
[task 2020-12-16T20:31:27.456Z] 20:31:27 INFO - /builds/worker/workspace/obj-build/dist/include/nsReadableUtils.h:456:8: note: use non-reference type 'unsigned long'
[task 2020-12-16T20:31:27.457Z] 20:31:27 INFO - for (auto&& item : aInputRange) {
[task 2020-12-16T20:31:27.458Z] 20:31:27 INFO - ^~~~~~~~~~~~~
[task 2020-12-16T20:31:27.458Z] 20:31:27 ERROR - /builds/worker/workspace/obj-build/dist/include/nsReadableUtils.h:456:15: error: loop variable 'item' is always a copy because the range of type 'const mozilla::EnumSet<unsigned long, unsigned long>' does not return a reference [-Werror,-Wrange-loop-analysis]
[task 2020-12-16T20:31:27.458Z] 20:31:27 INFO - for (auto&& item : aInputRange) {
[task 2020-12-16T20:31:27.458Z] 20:31:27 INFO - ^
[task 2020-12-16T20:31:27.458Z] 20:31:27 INFO - /builds/worker/workspace/obj-build/dist/include/nsReadableUtils.h:484:3: note: in instantiation of function template specialization 'StringJoinAppend<char, mozilla::EnumSet<unsigned long, unsigned long>, (lambda at /builds/worker/workspace/obj-build/dist/include/mozilla/dom/SyncedContextInlines.h:25:21)>' requested here
[task 2020-12-16T20:31:27.458Z] 20:31:27 INFO - StringJoinAppend(res, aSeparator, aInputRange, std::forward<Func>(aFunc));
[task 2020-12-16T20:31:27.458Z] 20:31:27 INFO - ^
Comment 8•4 years ago
|
||
Comment 10•4 years ago
|
||
| bugherder | ||
| Assignee | ||
Updated•4 years ago
|
Description
•