[PATCH] D24880: Add StringExtras join_items function

Mehdi AMINI via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 20:34:25 PDT 2016


mehdi_amini added a comment.

In https://reviews.llvm.org/D24880#553448, @zturner wrote:

> In https://reviews.llvm.org/D24880#553444, @mehdi_amini wrote:
>
> > What about: ```auto Result = (Twine(foo()) + bar() + baz()).str()```?
>
>
> Hadn't thought of that, but that also doesn't work of `foo()`, `bar()`, and `baz()` are of type `const char *`.  In that case you need to invoke `operator +=` on the `std::string`


No: the twine capture the pointers, and create a string *at the end only* (the call to str()). That said it is not terribly efficient: it'll stream to a SmallVector and copy the result to the string. (You can instead create a small vector and supply it to Twine::toVector() to save a copy).
Anyway...

> Also I feel like Twine(foo() + '/' bar() + '/' + baz()).str() is a big more difficult to visually grok than llvm::join_items('/', foo(), bar(), baz());


Right, I agree with that.


================
Comment at: include/llvm/ADT/StringExtras.h:222
@@ +221,3 @@
+inline std::string join_items(Sep Separator, Args &&... Items) {
+  std::string Result;
+  detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
----------------
Could you add a "reserve" stage here?



https://reviews.llvm.org/D24880





More information about the llvm-commits mailing list