10b57cec5SDimitry Andric// -*- C++ -*- 2349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP_CSTRING 110b57cec5SDimitry Andric#define _LIBCPP_CSTRING 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric/* 140b57cec5SDimitry Andric cstring synopsis 150b57cec5SDimitry Andric 160b57cec5SDimitry AndricMacros: 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric NULL 190b57cec5SDimitry Andric 200b57cec5SDimitry Andricnamespace std 210b57cec5SDimitry Andric{ 220b57cec5SDimitry Andric 230b57cec5SDimitry AndricTypes: 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric size_t 260b57cec5SDimitry Andric 270b57cec5SDimitry Andricvoid* memcpy(void* restrict s1, const void* restrict s2, size_t n); 280b57cec5SDimitry Andricvoid* memmove(void* s1, const void* s2, size_t n); 290b57cec5SDimitry Andricchar* strcpy (char* restrict s1, const char* restrict s2); 300b57cec5SDimitry Andricchar* strncpy(char* restrict s1, const char* restrict s2, size_t n); 310b57cec5SDimitry Andricchar* strcat (char* restrict s1, const char* restrict s2); 320b57cec5SDimitry Andricchar* strncat(char* restrict s1, const char* restrict s2, size_t n); 330b57cec5SDimitry Andricint memcmp(const void* s1, const void* s2, size_t n); 340b57cec5SDimitry Andricint strcmp (const char* s1, const char* s2); 350b57cec5SDimitry Andricint strncmp(const char* s1, const char* s2, size_t n); 360b57cec5SDimitry Andricint strcoll(const char* s1, const char* s2); 370b57cec5SDimitry Andricsize_t strxfrm(char* restrict s1, const char* restrict s2, size_t n); 380b57cec5SDimitry Andricconst void* memchr(const void* s, int c, size_t n); 390b57cec5SDimitry Andric void* memchr( void* s, int c, size_t n); 400b57cec5SDimitry Andricconst char* strchr(const char* s, int c); 410b57cec5SDimitry Andric char* strchr( char* s, int c); 420b57cec5SDimitry Andricsize_t strcspn(const char* s1, const char* s2); 430b57cec5SDimitry Andricconst char* strpbrk(const char* s1, const char* s2); 440b57cec5SDimitry Andric char* strpbrk( char* s1, const char* s2); 450b57cec5SDimitry Andricconst char* strrchr(const char* s, int c); 460b57cec5SDimitry Andric char* strrchr( char* s, int c); 470b57cec5SDimitry Andricsize_t strspn(const char* s1, const char* s2); 480b57cec5SDimitry Andricconst char* strstr(const char* s1, const char* s2); 490b57cec5SDimitry Andric char* strstr( char* s1, const char* s2); 500b57cec5SDimitry Andricchar* strtok(char* restrict s1, const char* restrict s2); 510b57cec5SDimitry Andricvoid* memset(void* s, int c, size_t n); 520b57cec5SDimitry Andricchar* strerror(int errnum); 530b57cec5SDimitry Andricsize_t strlen(const char* s); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric} // std 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric*/ 580b57cec5SDimitry Andric 5981ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 600b57cec5SDimitry Andric#include <__config> 61*bdd1243dSDimitry Andric#include <__type_traits/is_constant_evaluated.h> 62*bdd1243dSDimitry Andric 630b57cec5SDimitry Andric#include <string.h> 640b57cec5SDimitry Andric 65*bdd1243dSDimitry Andric#ifndef _LIBCPP_STRING_H 66*bdd1243dSDimitry Andric# error <cstring> tried including <string.h> but didn't find libc++'s <string.h> header. \ 67*bdd1243dSDimitry Andric This usually means that your header search paths are not configured properly. \ 68*bdd1243dSDimitry Andric The header search paths should contain the C++ Standard Library headers before \ 69*bdd1243dSDimitry Andric any C Standard Library, and you are probably using compiler flags that make that \ 70*bdd1243dSDimitry Andric not be the case. 71*bdd1243dSDimitry Andric#endif 72*bdd1243dSDimitry Andric 730b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 740b57cec5SDimitry Andric# pragma GCC system_header 750b57cec5SDimitry Andric#endif 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 780b57cec5SDimitry Andric 79fe6060f1SDimitry Andricusing ::size_t _LIBCPP_USING_IF_EXISTS; 80fe6060f1SDimitry Andricusing ::memcpy _LIBCPP_USING_IF_EXISTS; 81fe6060f1SDimitry Andricusing ::memmove _LIBCPP_USING_IF_EXISTS; 82fe6060f1SDimitry Andricusing ::strcpy _LIBCPP_USING_IF_EXISTS; 83fe6060f1SDimitry Andricusing ::strncpy _LIBCPP_USING_IF_EXISTS; 84fe6060f1SDimitry Andricusing ::strcat _LIBCPP_USING_IF_EXISTS; 85fe6060f1SDimitry Andricusing ::strncat _LIBCPP_USING_IF_EXISTS; 86fe6060f1SDimitry Andricusing ::memcmp _LIBCPP_USING_IF_EXISTS; 87fe6060f1SDimitry Andricusing ::strcmp _LIBCPP_USING_IF_EXISTS; 88fe6060f1SDimitry Andricusing ::strncmp _LIBCPP_USING_IF_EXISTS; 89fe6060f1SDimitry Andricusing ::strcoll _LIBCPP_USING_IF_EXISTS; 90fe6060f1SDimitry Andricusing ::strxfrm _LIBCPP_USING_IF_EXISTS; 91fe6060f1SDimitry Andricusing ::memchr _LIBCPP_USING_IF_EXISTS; 92fe6060f1SDimitry Andricusing ::strchr _LIBCPP_USING_IF_EXISTS; 93fe6060f1SDimitry Andricusing ::strcspn _LIBCPP_USING_IF_EXISTS; 94fe6060f1SDimitry Andricusing ::strpbrk _LIBCPP_USING_IF_EXISTS; 95fe6060f1SDimitry Andricusing ::strrchr _LIBCPP_USING_IF_EXISTS; 96fe6060f1SDimitry Andricusing ::strspn _LIBCPP_USING_IF_EXISTS; 97fe6060f1SDimitry Andricusing ::strstr _LIBCPP_USING_IF_EXISTS; 98fe6060f1SDimitry Andricusing ::strtok _LIBCPP_USING_IF_EXISTS; 99fe6060f1SDimitry Andricusing ::memset _LIBCPP_USING_IF_EXISTS; 100fe6060f1SDimitry Andricusing ::strerror _LIBCPP_USING_IF_EXISTS; 101fe6060f1SDimitry Andricusing ::strlen _LIBCPP_USING_IF_EXISTS; 1020b57cec5SDimitry Andric 103*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_strlen(const char* __str) { 104*bdd1243dSDimitry Andric // GCC currently doesn't support __builtin_strlen for heap-allocated memory during constant evaluation. 105*bdd1243dSDimitry Andric // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816 106*bdd1243dSDimitry Andric#ifdef _LIBCPP_COMPILER_GCC 107*bdd1243dSDimitry Andric if (__libcpp_is_constant_evaluated()) { 108*bdd1243dSDimitry Andric size_t __i = 0; 109*bdd1243dSDimitry Andric for (; __str[__i] != '\0'; ++__i) 110*bdd1243dSDimitry Andric ; 111*bdd1243dSDimitry Andric return __i; 112*bdd1243dSDimitry Andric } 113*bdd1243dSDimitry Andric#endif 114*bdd1243dSDimitry Andric return __builtin_strlen(__str); 115*bdd1243dSDimitry Andric} 116*bdd1243dSDimitry Andric 117*bdd1243dSDimitry Andrictemplate <class _Tp> 118*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int 119*bdd1243dSDimitry Andric__constexpr_memcmp(const _Tp* __lhs, const _Tp* __rhs, size_t __count) { 120*bdd1243dSDimitry Andric#ifdef _LIBCPP_COMPILER_GCC 121*bdd1243dSDimitry Andric if (__libcpp_is_constant_evaluated()) { 122*bdd1243dSDimitry Andric for (; __count; --__count, ++__lhs, ++__rhs) { 123*bdd1243dSDimitry Andric if (*__lhs < *__rhs) 124*bdd1243dSDimitry Andric return -1; 125*bdd1243dSDimitry Andric if (*__rhs < *__lhs) 126*bdd1243dSDimitry Andric return 1; 127*bdd1243dSDimitry Andric } 128*bdd1243dSDimitry Andric return 0; 129*bdd1243dSDimitry Andric } 130*bdd1243dSDimitry Andric#endif 131*bdd1243dSDimitry Andric return __builtin_memcmp(__lhs, __rhs, __count); 132*bdd1243dSDimitry Andric} 133*bdd1243dSDimitry Andric 134*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const char* 135*bdd1243dSDimitry Andric__constexpr_char_memchr(const char* __str, int __char, size_t __count) { 136*bdd1243dSDimitry Andric#if __has_builtin(__builtin_char_memchr) 137*bdd1243dSDimitry Andric return __builtin_char_memchr(__str, __char, __count); 138*bdd1243dSDimitry Andric#else 139*bdd1243dSDimitry Andric if (!__libcpp_is_constant_evaluated()) 140*bdd1243dSDimitry Andric return static_cast<const char*>(std::memchr(__str, __char, __count)); 141*bdd1243dSDimitry Andric for (; __count; --__count) { 142*bdd1243dSDimitry Andric if (*__str == __char) 143*bdd1243dSDimitry Andric return __str; 144*bdd1243dSDimitry Andric ++__str; 145*bdd1243dSDimitry Andric } 146*bdd1243dSDimitry Andric return nullptr; 147*bdd1243dSDimitry Andric#endif 148*bdd1243dSDimitry Andric} 149*bdd1243dSDimitry Andric 1500b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric#endif // _LIBCPP_CSTRING 153