C++ Logo


Advanced search

Avoid copies when using string_view with APIs expecting null terminated strings

From: Tom Mason <wheybags_at_[hidden]>
Date: Wed, 23 Dec 2020 00:33:35 +0000
A big problem with std::string_view for a lot of people, is that sometimes you need to pass the string to a platform API, or plain C library that expects a null-terminated string. This problem means I normally still default to using a const std::string& instead, throughout my codebase, as otherwise I would need to copy before calling these methods. The whole point of string_view is for it to be the default read only string type, and this really gets in the way of that because now you need to make a decision every time. Most of the time, the string_view will be a reference to an entire std::string anyway, so it would actually be ok to just use string_view::data(), because it is actually null terminated.

The problem is that there is no way to know if a given string_view is null terminated. My proposal is to add a method bool null_terminated() to string_view. When a string_view is constructed, the caller can tell the implementation that the pointer it is providing is null terminated. The conversion from std::string to std::string_view would set this flag.

At the point where you need a null-terminated string, you can check the null_terminated() member before proceeding, and handle it accordingly. I have a proof of concept implementation here: https://gist.github.com/wheybags/3c06795d251f004e007d47b3a5d823a7. This PoC stores the "null terminated" flag in the highest bit of the length field, so the memory footprint is unchanged. On a 64 bit machine this is no loss really, as storing exabytes of data in one std::string is not reasonable. On machines with small word sizes the null_terminated() member just returns false. Of course, you could also just use a separate flag boolean.

I hope this is not a duplicate posting, and that this writeup is acceptable. I did try searching both here and the old google groups list, and I didn't find anything that looked like this. Please let me know what you think.

PS: The submit a proposal page on the isocpp site (https://isocpp.org/std/submit-a-proposal) still links to the old google group.

Received on 2020-12-22 18:35:45