[llvm-dev] RFC: Constructing StringRefs at compile time

Malcolm Parsons via llvm-dev llvm-dev at lists.llvm.org
Fri Dec 2 03:35:55 PST 2016


On 29 November 2016 at 17:52, Malcolm Parsons <malcolm.parsons at gmail.com> wrote:
> On 29 November 2016 at 17:38, Zachary Turner <zturner at google.com> wrote:
>> I see, but I looked over your proposed implementation from earlier in the
>> thread, and if I'm not mistaken I see this:
>
> That's a different suggestion.

llvm_strlen could be something like this:

#if __cpp_lib_constexpr_char_traits
constexpr size_t llvm_strlen(const char *S) {
  return std::char_traits<char>::length(S);
}
#elif __has_builtin(__builtin_strlen) || defined(__GNUC__)
constexpr size_t llvm_strlen(const char *S) { return __builtin_strlen(S); }
#elif __cpp_constexpr >= 201304
constexpr size_t llvm_strlen(const char *S) {
  const char *const A = S;
  while (*S != '\0') {
    ++S;
  }
  return S - A;
}
#else
constexpr size_t llvm_strlen(const char *S, size_t L = 0) {
  return *S ? llvm_strlen(S + 1, L + 1) : L;
}
#endif

-- 
Malcolm Parsons


More information about the llvm-dev mailing list