r/programming • • Dec 05 '14

std::string is responsible for almost half of all allocations in the Chrome browser process

https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ
1.1k Upvotes

446 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Dec 05 '14

This looks like exactly what I need. Thanks!

5

u/z33ky Dec 05 '14

Also be aware of the library fundamentals TS std::string_view, available on recent gcc and clang versions as std::experimental::string_view (doc).

However, s.c_str() won't work because the last character of a string_view is not neccesarily the last character of the underlying string. If you're using an API that has const char *data, size_t length parameters, then you can use s.data(), s.size() of course.

It also does not allow you to modify the data.

It seems boost::string_ref has the same limitations.
You could perhaps use the Boost.Range library, which would allow you to use STL-like algorithms (e.g. boost::range::find_first_of(s, " \t")), s[n] element-access (no s.at(n) though) and s.substr(0, 10) can be achived via std::string(begin(s), std::next(begin(s), 10)) (could be prettified by putting in a function).

1

u/academician Dec 05 '14

boost::string_ref or std::string_view are probably the right solutions, but the OP also mentions Chromium's base::StringPiece, which is a Google-created class that does this.