blob: c88d97739f7449d77ba3e55ebcfb9f44ae705523 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_CSTRING
11#define _LIBCPP_CSTRING
12
13/*
14 cstring synopsis
15
16Macros:
17
18 NULL
Howard Hinnant3b6579a2010-08-22 00:02:43 +000019
Howard Hinnantc51e1022010-05-11 19:42:16 +000020namespace std
21{
22
23Types:
24
25 size_t
26
27void* memcpy(void* restrict s1, const void* restrict s2, size_t n);
28void* memmove(void* s1, const void* s2, size_t n);
29char* strcpy (char* restrict s1, const char* restrict s2);
30char* strncpy(char* restrict s1, const char* restrict s2, size_t n);
31char* strcat (char* restrict s1, const char* restrict s2);
32char* strncat(char* restrict s1, const char* restrict s2, size_t n);
33int memcmp(const void* s1, const void* s2, size_t n);
34int strcmp (const char* s1, const char* s2);
35int strncmp(const char* s1, const char* s2, size_t n);
36int strcoll(const char* s1, const char* s2);
37size_t strxfrm(char* restrict s1, const char* restrict s2, size_t n);
38const void* memchr(const void* s, int c, size_t n);
39 void* memchr( void* s, int c, size_t n);
40const char* strchr(const char* s, int c);
41 char* strchr( char* s, int c);
42size_t strcspn(const char* s1, const char* s2);
43const char* strpbrk(const char* s1, const char* s2);
44 char* strpbrk( char* s1, const char* s2);
45const char* strrchr(const char* s, int c);
46 char* strrchr( char* s, int c);
47size_t strspn(const char* s1, const char* s2);
48const char* strstr(const char* s1, const char* s2);
49 char* strstr( char* s1, const char* s2);
50char* strtok(char* restrict s1, const char* restrict s2);
51void* memset(void* s, int c, size_t n);
52char* strerror(int errnum);
53size_t strlen(const char* s);
54
55} // std
56
57*/
58
Louis Dionneb4fce352022-03-25 12:55:36 -040059#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +000060#include <__config>
Nikolas Klausera1d08f32022-12-07 18:31:14 +010061#include <__type_traits/is_constant_evaluated.h>
Louis Dionned3bbe792022-08-08 17:03:56 -040062
Howard Hinnantc51e1022010-05-11 19:42:16 +000063#include <string.h>
64
Louis Dionned3bbe792022-08-08 17:03:56 -040065#ifndef _LIBCPP_STRING_H
66# error <cstring> tried including <string.h> but didn't find libc++'s <string.h> header. \
67 This usually means that your header search paths are not configured properly. \
68 The header search paths should contain the C++ Standard Library headers before \
69 any C Standard Library, and you are probably using compiler flags that make that \
70 not be the case.
71#endif
72
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000073#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050074# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000075#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000076
77_LIBCPP_BEGIN_NAMESPACE_STD
78
Louis Dionne9c660c62021-06-02 10:41:37 -040079using ::size_t _LIBCPP_USING_IF_EXISTS;
80using ::memcpy _LIBCPP_USING_IF_EXISTS;
81using ::memmove _LIBCPP_USING_IF_EXISTS;
82using ::strcpy _LIBCPP_USING_IF_EXISTS;
83using ::strncpy _LIBCPP_USING_IF_EXISTS;
84using ::strcat _LIBCPP_USING_IF_EXISTS;
85using ::strncat _LIBCPP_USING_IF_EXISTS;
86using ::memcmp _LIBCPP_USING_IF_EXISTS;
87using ::strcmp _LIBCPP_USING_IF_EXISTS;
88using ::strncmp _LIBCPP_USING_IF_EXISTS;
89using ::strcoll _LIBCPP_USING_IF_EXISTS;
90using ::strxfrm _LIBCPP_USING_IF_EXISTS;
91using ::memchr _LIBCPP_USING_IF_EXISTS;
92using ::strchr _LIBCPP_USING_IF_EXISTS;
93using ::strcspn _LIBCPP_USING_IF_EXISTS;
94using ::strpbrk _LIBCPP_USING_IF_EXISTS;
95using ::strrchr _LIBCPP_USING_IF_EXISTS;
96using ::strspn _LIBCPP_USING_IF_EXISTS;
97using ::strstr _LIBCPP_USING_IF_EXISTS;
Louis Dionne9c660c62021-06-02 10:41:37 -040098using ::strtok _LIBCPP_USING_IF_EXISTS;
Louis Dionne9c660c62021-06-02 10:41:37 -040099using ::memset _LIBCPP_USING_IF_EXISTS;
100using ::strerror _LIBCPP_USING_IF_EXISTS;
101using ::strlen _LIBCPP_USING_IF_EXISTS;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102
Nikolas Klausera1d08f32022-12-07 18:31:14 +0100103inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_strlen(const char* __str) {
104 // GCC currently doesn't support __builtin_strlen for heap-allocated memory during constant evaluation.
105 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816
106#ifdef _LIBCPP_COMPILER_GCC
107 if (__libcpp_is_constant_evaluated()) {
108 size_t __i = 0;
109 for (; __str[__i] != '\0'; ++__i)
110 ;
111 return __i;
112 }
113#endif
114 return __builtin_strlen(__str);
115}
116
117template <class _Tp>
118_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
119__constexpr_memcmp(const _Tp* __lhs, const _Tp* __rhs, size_t __count) {
120#ifdef _LIBCPP_COMPILER_GCC
121 if (__libcpp_is_constant_evaluated()) {
122 for (; __count; --__count, ++__lhs, ++__rhs) {
123 if (*__lhs < *__rhs)
124 return -1;
125 if (*__rhs < *__lhs)
126 return 1;
127 }
128 return 0;
129 }
130#endif
131 return __builtin_memcmp(__lhs, __rhs, __count);
132}
133
134inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const char*
135__constexpr_char_memchr(const char* __str, int __char, size_t __count) {
136#if __has_builtin(__builtin_char_memchr)
137 return __builtin_char_memchr(__str, __char, __count);
138#else
139 if (!__libcpp_is_constant_evaluated())
140 return static_cast<const char*>(std::memchr(__str, __char, __count));
141 for (; __count; --__count) {
142 if (*__str == __char)
143 return __str;
144 ++__str;
145 }
146 return nullptr;
147#endif
148}
149
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150_LIBCPP_END_NAMESPACE_STD
151
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400152#endif // _LIBCPP_CSTRING