Skip to content

Commit f4b6199

Browse files
committed
core-builtin: add and use built in shim wrapper for strcspn
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
1 parent 685154c commit f4b6199

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

Makefile.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,7 @@ functions: \
26412641
BUILTIN_STDC_ROTATE_RIGHT \
26422642
BUILTIN_STRCHR \
26432643
BUILTIN_STRCMP \
2644+
BUILTIN_STRCSPN \
26442645
BUILTIN_STRDUP \
26452646
BUILTIN_STRLEN \
26462647
BUILTIN_STRNCMP \
@@ -3733,6 +3734,9 @@ BUILTIN_STRCHR:
37333734
BUILTIN_STRCMP:
37343735
$(call check,test-builtin-strcmp,HAVE_BUILTIN_STRCMP,__builtin_strcmp)
37353736

3737+
BUILTIN_STRCSPN:
3738+
$(call check,test-builtin-strcspn,HAVE_BUILTIN_STRCSPN,__builtin_strcspn)
3739+
37363740
BUILTIN_STRDUP:
37373741
$(call check,test-builtin-strdup,HAVE_BUILTIN_STRDUP,__builtin_strdup)
37383742

core-builtin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
#define shim_strcmp(s1, s2) strcmp((s1), (s2))
7474
#endif
7575

76+
#if defined(HAVE_BUILTIN_STRCSPN)
77+
#define shim_strcspn(s, reject) __builtin_strcspn((s), (reject))
78+
#else
79+
#define shim_strcspn(s, reject) strcspn((s), (reject))
80+
#endif
81+
7682
#if defined(HAVE_BUILTIN_STRLEN)
7783
#define shim_strlen(str) __builtin_strlen((str))
7884
#else

core-rapl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ int stress_rapl_domains_get(stress_rapl_domain_t **rapl_domains)
164164

165165
(void)shim_memset(domain_name, 0, sizeof(domain_name));
166166
if (fgets(domain_name, sizeof(domain_name), fp) != NULL) {
167-
const size_t idx = strcspn(domain_name, "\n");
167+
const size_t idx = shim_strcspn(domain_name, "\n");
168168

169169
if (LIKELY(idx < sizeof(domain_name)))
170170
domain_name[idx] = '\0';

core-thermal-zone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int stress_tz_init(stress_tz_info_t **tz_info_list)
130130

131131
(void)shim_memset(type, 0, sizeof(type));
132132
if (fgets(type, sizeof(type), fp) != NULL) {
133-
const size_t idx = strcspn(type, "\n");
133+
const size_t idx = shim_strcspn(type, "\n");
134134

135135
if (idx < sizeof(type))
136136
type[idx] = '\0';

test/test-builtin-strcspn.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2026 Colin Ian King
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*
18+
*/
19+
#include <string.h>
20+
21+
22+
int main(int argc, char **argv)
23+
{
24+
return (int)__builtin_strcspn("test123\n", "\n");
25+
}
26+

0 commit comments

Comments
 (0)