Introduce base::cstring_view

We currently have vocabulary types to receive a string parameter:
- std::string for owning + NUL-terminated
- std::string_view for non-owning and non-NUL-terminated
- const char* for non-owning and NUL-terminated

However the use of const char* will require unsafe pointer arithmetic
which is incompatible with -Wunsafe-buffer-usage (every use of the
pointer will cause compilation errors) so we need an alternative way
to express NUL-terminated string views that include bounds safety.
This type will allow conversion from pointer, or pointer+len, to a
bounds-safe view type that maintains its NUL terminator.

Throwing away the NUL byte is not a good option, as it forces a copy
into a std::string every time you want the NUL again. We should have
a non-owning view type that encodes in the type system that there is
a NUL terminator, and which provides a .c_str() method to get the
NUL-terminated pointer for C apis like string has.

So far the type has:
- default ctor
- ctor from ptr+len [unsafe buffer usage]
- ctor from string literal
- c_str
- data/size
- size_bytes (like span)
- begin/end/cbegin/cend
- operator[]
- operator==
- operator<=>

Eventually it will have all of string_view API, plus:
- have .c_str() representing that it returns a NUL-terminated char
  pointer suitable for passing to C APIs (done).
- be constructible from string literal (done).
- have a template parameter to retain compile-time size when possible,
  like span (done).
- avoid holding a size field when it's known at compile time, like
  span (done).

In order to use them, introduce the following macros in base:
- LIFETIME_BOUND
- PURE_FUNCTION
As proposed in the cxx@ thread:
https://groups.google.com/a/chromium.org/g/cxx/c/lVQOJTng1RU/m/91r04iLAAwAJ

As well as the __attribute__((enable_if())) extension via the macro
in order to catch bugs at compile time and ensure cstring_view is
only constructed from string literals (and not non-NUL-terminated
char arrays):
- ENABLE_IF_ATTR

Bug: 329476354, 40285824
Change-Id: I1047a3b049401ecf56636f9f8e1a69801098da9e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5370959
Commit-Queue: danakj <danakj@chromium.org>
Auto-Submit: danakj <danakj@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1277039}
NOKEYCHECK=True
GitOrigin-RevId: c077a30ed629e1389043c1688eea88b9b38a8cf6
1 file changed