Closed Bug 1583109 Opened 5 years ago Closed 3 years ago

Add utility function for joining a sequence of strings with a separator

Categories

(Core :: XPCOM, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
86 Branch
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: nobody → sgiesecke
Status: NEW → ASSIGNED

nsTHashtable is a C++ range now.

Component: String → XPCOM
Pushed by sgiesecke@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/c7bcdc88920a
Add StringJoin(Append) utility functions that join a sequence of items into a string. r=xpcom-reviewers,nika
Status: ASSIGNED → RESOLVED
Closed: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → 86 Branch
Blocks: 1682871
Pushed by sgiesecke@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/16d174e7c342
Use StringJoin(Append) where easily possible. r=nika

Backed out for causing bustage on nsReadableUtils.h

backout: https://hg.mozilla.org/integration/autoland/rev/36e58b614815fb3da6d7840d6f787b6ec2eed618

push: https://treeherder.mozilla.org/jobs?repo=autoland&revision=16d174e7c342ad3e0add694a67d35b868e90f19a&group_state=expanded

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 - ^

Flags: needinfo?(sgiesecke)
See Also: → 1674448
Pushed by sgiesecke@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/2556404e58f7
Use StringJoin(Append) where easily possible. r=nika
Flags: needinfo?(sgiesecke)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: