blob: 81eaccf0fb900740c32b62d0751e41dcc94f8c45 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughes09c39d62014-08-19 14:30:30 -070028
29#ifndef _STRING_H
30#define _STRING_H
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
32#include <sys/cdefs.h>
33#include <stddef.h>
Dan Albertdfb5ce42014-07-09 22:51:34 +000034#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Josh Gaoc3cec272016-04-07 13:39:49 -070036#include <bits/strcasecmp.h>
37
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038__BEGIN_DECLS
39
Elliott Hughes5470c182016-07-22 11:36:17 -070040#if defined(__clang__)
41#pragma clang diagnostic push
42#pragma clang diagnostic ignored "-Wnullability-completeness"
43#endif
44
Elliott Hughes76f89162015-01-26 13:34:58 -080045#if defined(__USE_BSD)
46#include <strings.h>
47#endif
48
Elliott Hughes5470c182016-07-22 11:36:17 -070049void* memccpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, int, size_t);
George Burgess IV7cc779f2017-02-09 00:00:31 -080050void* memchr(const void* _Nonnull, int, size_t) __attribute_pure__ __overloadable
51 __RENAME_CLANG(memchr);
52void* memrchr(const void* _Nonnull, int, size_t) __attribute_pure__ __overloadable
53 __RENAME_CLANG(memrchr);
Elliott Hughes95fa0612016-09-28 12:29:52 -070054int memcmp(const void* _Nonnull, const void* _Nonnull, size_t) __attribute_pure__;
George Burgess IV7cc779f2017-02-09 00:00:31 -080055void* memcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t)
56 __overloadable __RENAME_CLANG(memcpy);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080057#if defined(__USE_GNU)
Elliott Hughes5470c182016-07-22 11:36:17 -070058void* mempcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080059#endif
George Burgess IV7cc779f2017-02-09 00:00:31 -080060void* memmove(void* _Nonnull, const void* _Nonnull, size_t) __overloadable
61 __RENAME_CLANG(memmove);
62void* memset(void* _Nonnull, int, size_t) __overloadable __RENAME_CLANG(memset);
Elliott Hughes95fa0612016-09-28 12:29:52 -070063void* memmem(const void* _Nonnull, size_t, const void* _Nonnull, size_t) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
George Burgess IV7cc779f2017-02-09 00:00:31 -080065char* strchr(const char* _Nonnull, int) __attribute_pure__ __overloadable
66 __RENAME_CLANG(strchr);
Elliott Hughes5470c182016-07-22 11:36:17 -070067char* __strchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070068#if defined(__USE_GNU)
69#if defined(__cplusplus)
Josh Gao16d9ba82017-06-30 13:20:28 -070070extern "C++" char* strchrnul(char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
71extern "C++" const char* strchrnul(const char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070072#else
Elliott Hughes95fa0612016-09-28 12:29:52 -070073char* strchrnul(const char* _Nonnull, int) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070074#endif
75#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040076
George Burgess IV7cc779f2017-02-09 00:00:31 -080077char* strrchr(const char* _Nonnull, int) __attribute_pure__ __overloadable
78 __RENAME_CLANG(strrchr);
Elliott Hughes5470c182016-07-22 11:36:17 -070079char* __strrchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
George Burgess IV7cc779f2017-02-09 00:00:31 -080081size_t strlen(const char* _Nonnull) __attribute_pure__ __overloadable
82 __RENAME_CLANG(strlen);
Elliott Hughes5470c182016-07-22 11:36:17 -070083size_t __strlen_chk(const char* _Nonnull, size_t) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
George Burgess IV7cc779f2017-02-09 00:00:31 -080085int strcmp(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
86char* stpcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict)
87 __overloadable __RENAME_CLANG(stpcpy) __INTRODUCED_IN(21);
88char* strcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict)
89 __overloadable __RENAME_CLANG(strcpy);
90char* strcat(char* _Nonnull __restrict, const char* _Nonnull __restrict)
91 __overloadable __RENAME_CLANG(strcat);
Elliott Hughes5470c182016-07-22 11:36:17 -070092char* strdup(const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes95fa0612016-09-28 12:29:52 -070094char* strstr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
95char* strcasestr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -070096char* strtok(char* __restrict, const char* _Nonnull __restrict);
97char* strtok_r(char* __restrict, const char* _Nonnull __restrict, char** _Nonnull __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
Elliott Hughes5470c182016-07-22 11:36:17 -070099char* strerror(int);
100char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
Dan Albert8b154b12017-02-14 16:33:06 -0800101#if defined(__USE_GNU) && __ANDROID_API__ >= 23
Elliott Hughes5470c182016-07-22 11:36:17 -0700102char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700103#else /* POSIX */
Elliott Hughes5470c182016-07-22 11:36:17 -0700104int strerror_r(int, char*, size_t);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700105#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughes95fa0612016-09-28 12:29:52 -0700107size_t strnlen(const char* _Nonnull, size_t) __attribute_pure__;
George Burgess IV7cc779f2017-02-09 00:00:31 -0800108char* strncat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
109 __overloadable __RENAME_CLANG(strncat);
Elliott Hughes5470c182016-07-22 11:36:17 -0700110char* strndup(const char* _Nonnull, size_t);
Elliott Hughes95fa0612016-09-28 12:29:52 -0700111int strncmp(const char* _Nonnull, const char* _Nonnull, size_t) __attribute_pure__;
George Burgess IV7cc779f2017-02-09 00:00:31 -0800112char* stpncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
113 __overloadable __RENAME_CLANG(stpncpy) __INTRODUCED_IN(21);
114char* strncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
115 __overloadable __RENAME_CLANG(strncpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
George Burgess IV7cc779f2017-02-09 00:00:31 -0800117size_t strlcat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
118 __overloadable __RENAME_CLANG(strlcat);
119size_t strlcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t)
120 __overloadable __RENAME_CLANG(strlcpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Elliott Hughes95fa0612016-09-28 12:29:52 -0700122size_t strcspn(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
123char* strpbrk(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700124char* strsep(char** _Nonnull __restrict, const char* _Nonnull __restrict);
125size_t strspn(const char* _Nonnull, const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Elliott Hughes5470c182016-07-22 11:36:17 -0700127char* strsignal(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Elliott Hughes95fa0612016-09-28 12:29:52 -0700129int strcoll(const char* _Nonnull, const char* _Nonnull) __attribute_pure__;
Elliott Hughes5470c182016-07-22 11:36:17 -0700130size_t strxfrm(char* __restrict, const char* _Nonnull __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800132#if __ANDROID_API__ >= __ANDROID_API_L__
Elliott Hughes95fa0612016-09-28 12:29:52 -0700133int strcoll_l(const char* _Nonnull, const char* _Nonnull, locale_t) __attribute_pure__ __INTRODUCED_IN(21);
Elliott Hughes5470c182016-07-22 11:36:17 -0700134size_t strxfrm_l(char* __restrict, const char* _Nonnull __restrict, size_t, locale_t) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700135#else
136// Implemented as static inlines before 21.
137#endif
Dan Albertdfb5ce42014-07-09 22:51:34 +0000138
Josh Gaoeb9b9252015-11-03 18:46:02 -0800139#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700140/*
141 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
142 * It doesn't modify its argument, and in C++ it's const-correct.
143 */
144#if defined(__cplusplus)
Josh Gao16d9ba82017-06-30 13:20:28 -0700145extern "C++" char* basename(char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
146extern "C++" const char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700147#else
Elliott Hughes5470c182016-07-22 11:36:17 -0700148char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700149#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700150#endif
151
George Burgess IVb97049c2017-07-24 15:05:05 -0700152#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
153#include <bits/fortify/string.h>
154#endif
Nick Kralevich0a230152012-06-04 15:20:25 -0700155
George Burgess IVbd3d2082017-04-04 17:34:02 -0700156/* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
157#if defined(__cplusplus) && defined(__clang__)
158/*
159 * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
160 * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
161 */
162#define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
163extern "C++" {
164inline __always_inline
165void* __bionic_memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n) {
166 return memchr(s, c, n);
167}
168
169inline __always_inline
170const void* memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n)
171 __prefer_this_overload {
172 return __bionic_memchr(s, c, n);
173}
174
175inline __always_inline
176void* memchr(void* const _Nonnull s __pass_object_size, int c, size_t n) __prefer_this_overload {
177 return __bionic_memchr(s, c, n);
178}
179
180inline __always_inline
181char* __bionic_strchr(const char* const _Nonnull s __pass_object_size, int c) {
182 return strchr(s, c);
183}
184
185inline __always_inline
186const char* strchr(const char* const _Nonnull s __pass_object_size, int c)
187 __prefer_this_overload {
188 return __bionic_strchr(s, c);
189}
190
191inline __always_inline
192char* strchr(char* const _Nonnull s __pass_object_size, int c)
193 __prefer_this_overload {
194 return __bionic_strchr(s, c);
195}
196
197inline __always_inline
198char* __bionic_strrchr(const char* const _Nonnull s __pass_object_size, int c) {
199 return strrchr(s, c);
200}
201
202inline __always_inline
203const char* strrchr(const char* const _Nonnull s __pass_object_size, int c) __prefer_this_overload {
204 return __bionic_strrchr(s, c);
205}
206
207inline __always_inline
208char* strrchr(char* const _Nonnull s __pass_object_size, int c) __prefer_this_overload {
209 return __bionic_strrchr(s, c);
210}
211
212/* Functions with no FORTIFY counterpart. */
213inline __always_inline
214char* __bionic_strstr(const char* _Nonnull h, const char* _Nonnull n) { return strstr(h, n); }
215
216inline __always_inline
217const char* strstr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
218 return __bionic_strstr(h, n);
219}
220
221inline __always_inline
222char* strstr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
223 return __bionic_strstr(h, n);
224}
225
226inline __always_inline
227char* __bionic_strpbrk(const char* _Nonnull h, const char* _Nonnull n) { return strpbrk(h, n); }
228
229inline __always_inline
230char* strpbrk(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
231 return __bionic_strpbrk(h, n);
232}
233
234inline __always_inline
235const char* strpbrk(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
236 return __bionic_strpbrk(h, n);
237}
238}
239#undef __prefer_this_overload
240#endif
241
Elliott Hughes5470c182016-07-22 11:36:17 -0700242#if defined(__clang__)
243#pragma clang diagnostic pop
244#endif
245
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246__END_DECLS
247
Elliott Hughes09c39d62014-08-19 14:30:30 -0700248#endif /* _STRING_H */