diff options
| author | Alex Brachet <abrachet@google.com> | 2023-04-11 01:11:32 +0000 |
|---|---|---|
| committer | Alex Brachet <abrachet@google.com> | 2023-04-11 01:16:56 +0000 |
| commit | 82ca29ce54d3b7d8e47aa151e0ce3dff37727c1a (patch) | |
| tree | 76237a36a0a78c0d572b696575f49037eae258ec /libc/src/string/string_utils.h | |
| parent | d8804ecd76db331b622d011f5b72838b25bbf910 (diff) | |
[libc] Move str{,r}chr implementation to headers
Differential Revision: https://reviews.llvm.org/D147463
Diffstat (limited to 'libc/src/string/string_utils.h')
| -rw-r--r-- | libc/src/string/string_utils.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h index c778ccf17013..572d47d63956 100644 --- a/libc/src/string/string_utils.h +++ b/libc/src/string/string_utils.h @@ -224,6 +224,25 @@ LIBC_INLINE size_t strlcpy(char *__restrict dst, const char *__restrict src, return len; } +template <bool ReturnNull = true> +constexpr static char *strchr_implementation(const char *src, int c) { + char ch = static_cast<char>(c); + for (; *src && *src != ch; ++src) + ; + char *ret = ReturnNull ? nullptr : const_cast<char *>(src); + return *src == ch ? const_cast<char *>(src) : ret; +} + +constexpr static char *strrchr_implementation(const char *src, int c) { + char ch = static_cast<char>(c); + char *last_occurrence = nullptr; + for (; *src; ++src) { + if (*src == ch) + last_occurrence = const_cast<char *>(src); + } + return last_occurrence; +} + } // namespace internal } // namespace __llvm_libc |
