On Wed, 14 Jun 2017, Sage Weil wrote:
But nobody does that. It's stuff like
strlen("\r\nffffffffffffffff;chunk-signature=")
which can just be
sizeof("whatever") - 1
and it is clear to the reader of the code that it isn't strlen and \0's in
the string aren't relevant.
In that case, I agree, though it's worth considering that operator sizeof
won't give us the right result when the argument is deduced as a pointer.
That is:
const char *msg = "Hello, World!";
auto sz = sizeof(msg) - 1;
...sz != strlen(msg). We certainly have constants and macros in our code.
Consider also the innocuous-looking:
const char s[10] = {};
...where again sizeof(s) - 1 and strlen(s) differ.
Nothing about /having/ a compile-time strlen() function precludes someone
from using sizeof(s) - 1 when we have an array, of course.
With respect to having character arrays in our code with multiple NULL
characters, there are instances in in our own submodules, in our test
code, and at least one case where Ceph code uses that technique at runtime
to send split strings to Python-- so, I think that if it is put in a
function and given a name, we don't want to claim it computes a string
length if it is computing size - 1.
The proposed version from Adam with the runtime check should be okay if we
want a checked version, or we can use another implementation.
IOW I don't see why we would need a fully C-like constexpr strlen...
In this specific situation, I guess the use is to help solve Willem's
problem. :-) In a general library where we're doing compile-time slicing
and dicing, it would be pretty important.
Another idea is to wrap the compiler intrinsics, which will give us
constexpr-ness across at least GCC and clang (and may be helpful in
Willem's case). Something like this might work:
namespace ceph {
namespace util {
constexpr size_t strlen(const char *s)
{
#if __clang__
return __builtin_strlen(s);
#else
return std::strlen(s);
#endif
}
}} // namespace ceph::util
I hope this is useful-- it's always fun when "simple" things generate
lively discussion!
-Jesse
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html