blob: 164146d4ec41ff72101ae161a0dca1e02e752e88 [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 Hughes76f89162015-01-26 13:34:58 -080040#if defined(__USE_BSD)
41#include <strings.h>
42#endif
43
Elliott Hughes3f66e742017-08-01 13:24:40 -070044void* memccpy(void*, const void*, int, size_t);
45void* memchr(const void*, int, size_t) __attribute_pure__ __overloadable __RENAME_CLANG(memchr);
46void* memrchr(const void*, int, size_t) __attribute_pure__ __overloadable __RENAME_CLANG(memrchr);
47int memcmp(const void*, const void*, size_t) __attribute_pure__;
48void* memcpy(void*, const void*, size_t)
George Burgess IV7cc779f2017-02-09 00:00:31 -080049 __overloadable __RENAME_CLANG(memcpy);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080050#if defined(__USE_GNU)
Elliott Hughes3f66e742017-08-01 13:24:40 -070051void* mempcpy(void*, const void*, size_t) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080052#endif
Elliott Hughes3f66e742017-08-01 13:24:40 -070053void* memmove(void*, const void*, size_t) __overloadable __RENAME_CLANG(memmove);
54void* memset(void*, int, size_t) __overloadable __RENAME_CLANG(memset);
55void* memmem(const void*, size_t, const void*, size_t) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Elliott Hughes3f66e742017-08-01 13:24:40 -070057char* strchr(const char*, int) __attribute_pure__ __overloadable __RENAME_CLANG(strchr);
58char* __strchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070059#if defined(__USE_GNU)
60#if defined(__cplusplus)
Dan Albert3f7e65e2017-08-15 14:42:31 -070061/* The versioner doesn't handle C++ blocks yet, so manually guarded. */
62#if __ANDROID_API__ >= 24
Elliott Hughes3f66e742017-08-01 13:24:40 -070063extern "C++" char* strchrnul(char*, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
64extern "C++" const char* strchrnul(const char*, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
Dan Albert3f7e65e2017-08-15 14:42:31 -070065#endif /* __ANDROID_API__ >= 24 */
Elliott Hughes7ac3c122015-08-26 09:59:29 -070066#else
Elliott Hughes3f66e742017-08-01 13:24:40 -070067char* strchrnul(const char*, int) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070068#endif
69#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040070
Elliott Hughes3f66e742017-08-01 13:24:40 -070071char* strrchr(const char*, int) __attribute_pure__ __overloadable __RENAME_CLANG(strrchr);
72char* __strrchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Elliott Hughes3f66e742017-08-01 13:24:40 -070074size_t strlen(const char*) __attribute_pure__ __overloadable
George Burgess IV7cc779f2017-02-09 00:00:31 -080075 __RENAME_CLANG(strlen);
Elliott Hughes3f66e742017-08-01 13:24:40 -070076size_t __strlen_chk(const char*, size_t) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
Elliott Hughes3f66e742017-08-01 13:24:40 -070078int strcmp(const char*, const char*) __attribute_pure__;
79char* stpcpy(char*, const char*) __overloadable __RENAME_CLANG(stpcpy) __INTRODUCED_IN(21);
80char* strcpy(char*, const char*)
George Burgess IV7cc779f2017-02-09 00:00:31 -080081 __overloadable __RENAME_CLANG(strcpy);
Elliott Hughes3f66e742017-08-01 13:24:40 -070082char* strcat(char*, const char*) __overloadable __RENAME_CLANG(strcat);
83char* strdup(const char*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
Elliott Hughes3f66e742017-08-01 13:24:40 -070085char* strstr(const char*, const char*) __attribute_pure__;
86char* strcasestr(const char*, const char*) __attribute_pure__;
87char* strtok(char*, const char*);
88char* strtok_r(char*, const char*, char**);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089
Elliott Hughes5470c182016-07-22 11:36:17 -070090char* strerror(int);
91char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
Dan Albert8b154b12017-02-14 16:33:06 -080092#if defined(__USE_GNU) && __ANDROID_API__ >= 23
Elliott Hughes5470c182016-07-22 11:36:17 -070093char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070094#else /* POSIX */
Elliott Hughes5470c182016-07-22 11:36:17 -070095int strerror_r(int, char*, size_t);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070096#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097
Elliott Hughes3f66e742017-08-01 13:24:40 -070098size_t strnlen(const char*, size_t) __attribute_pure__;
99char* strncat(char*, const char*, size_t) __overloadable __RENAME_CLANG(strncat);
100char* strndup(const char*, size_t);
101int strncmp(const char*, const char*, size_t) __attribute_pure__;
102char* stpncpy(char*, const char*, size_t) __overloadable __RENAME_CLANG(stpncpy) __INTRODUCED_IN(21);
103char* strncpy(char*, const char*, size_t) __overloadable __RENAME_CLANG(strncpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
Elliott Hughes3f66e742017-08-01 13:24:40 -0700105size_t strlcat(char*, const char*, size_t) __overloadable __RENAME_CLANG(strlcat);
106size_t strlcpy(char*, const char*, size_t) __overloadable __RENAME_CLANG(strlcpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107
Elliott Hughes3f66e742017-08-01 13:24:40 -0700108size_t strcspn(const char*, const char*) __attribute_pure__;
109char* strpbrk(const char*, const char*) __attribute_pure__;
110char* strsep(char**, const char*);
111size_t strspn(const char*, const char*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Elliott Hughes5470c182016-07-22 11:36:17 -0700113char* strsignal(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughes3f66e742017-08-01 13:24:40 -0700115int strcoll(const char*, const char*) __attribute_pure__;
116size_t strxfrm(char*, const char*, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800118#if __ANDROID_API__ >= __ANDROID_API_L__
Elliott Hughes3f66e742017-08-01 13:24:40 -0700119int strcoll_l(const char*, const char*, locale_t) __attribute_pure__ __INTRODUCED_IN(21);
120size_t strxfrm_l(char*, const char*, size_t, locale_t) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700121#else
122// Implemented as static inlines before 21.
123#endif
Dan Albertdfb5ce42014-07-09 22:51:34 +0000124
Josh Gaoeb9b9252015-11-03 18:46:02 -0800125#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700126/*
127 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
128 * It doesn't modify its argument, and in C++ it's const-correct.
129 */
130#if defined(__cplusplus)
Dan Albert3f7e65e2017-08-15 14:42:31 -0700131/* The versioner doesn't handle C++ blocks yet, so manually guarded. */
132#if __ANDROID_API__ >= 23
Elliott Hughes3f66e742017-08-01 13:24:40 -0700133extern "C++" char* basename(char*) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
134extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Dan Albert3f7e65e2017-08-15 14:42:31 -0700135#endif /* __ANDROID_API__ >= 23 */
Elliott Hughes09c39d62014-08-19 14:30:30 -0700136#else
Elliott Hughes3f66e742017-08-01 13:24:40 -0700137char* basename(const char*) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700138#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700139#endif
140
George Burgess IVb97049c2017-07-24 15:05:05 -0700141#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
142#include <bits/fortify/string.h>
143#endif
Nick Kralevich0a230152012-06-04 15:20:25 -0700144
George Burgess IVbd3d2082017-04-04 17:34:02 -0700145/* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
146#if defined(__cplusplus) && defined(__clang__)
147/*
148 * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
149 * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
150 */
151#define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
152extern "C++" {
153inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700154void* __bionic_memchr(const void* const s __pass_object_size, int c, size_t n) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700155 return memchr(s, c, n);
156}
157
158inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700159const void* memchr(const void* const s __pass_object_size, int c, size_t n)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700160 __prefer_this_overload {
161 return __bionic_memchr(s, c, n);
162}
163
164inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700165void* memchr(void* const s __pass_object_size, int c, size_t n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700166 return __bionic_memchr(s, c, n);
167}
168
169inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700170char* __bionic_strchr(const char* const s __pass_object_size, int c) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700171 return strchr(s, c);
172}
173
174inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700175const char* strchr(const char* const s __pass_object_size, int c)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700176 __prefer_this_overload {
177 return __bionic_strchr(s, c);
178}
179
180inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700181char* strchr(char* const s __pass_object_size, int c)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700182 __prefer_this_overload {
183 return __bionic_strchr(s, c);
184}
185
186inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700187char* __bionic_strrchr(const char* const s __pass_object_size, int c) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700188 return strrchr(s, c);
189}
190
191inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700192const char* strrchr(const char* const s __pass_object_size, int c) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700193 return __bionic_strrchr(s, c);
194}
195
196inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700197char* strrchr(char* const s __pass_object_size, int c) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700198 return __bionic_strrchr(s, c);
199}
200
201/* Functions with no FORTIFY counterpart. */
202inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700203char* __bionic_strstr(const char* h, const char* n) { return strstr(h, n); }
George Burgess IVbd3d2082017-04-04 17:34:02 -0700204
205inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700206const char* strstr(const char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700207 return __bionic_strstr(h, n);
208}
209
210inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700211char* strstr(char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700212 return __bionic_strstr(h, n);
213}
214
215inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700216char* __bionic_strpbrk(const char* h, const char* n) { return strpbrk(h, n); }
George Burgess IVbd3d2082017-04-04 17:34:02 -0700217
218inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700219char* strpbrk(char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700220 return __bionic_strpbrk(h, n);
221}
222
223inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700224const char* strpbrk(const char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700225 return __bionic_strpbrk(h, n);
226}
227}
228#undef __prefer_this_overload
229#endif
230
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231__END_DECLS
232
Elliott Hughes09c39d62014-08-19 14:30:30 -0700233#endif /* _STRING_H */