File: strprefix.h

package info (click to toggle)
shadow 1%3A4.19.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,824 kB
  • sloc: sh: 44,185; ansic: 34,143; xml: 12,350; exp: 3,691; makefile: 1,655; python: 1,409; perl: 120; sed: 16
file content (53 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_


#include "config.h"

#include <stddef.h>
#include <string.h>

#include "attr.h"
#include "cast.h"


// string prefix
#define strprefix(s, prefix)                                          \
({                                                                    \
	const char  *p_;                                              \
	                                                              \
	p_ = strprefix_(s, prefix);                                   \
	                                                              \
	_Generic(s,                                                   \
		const char *:                     p_,                 \
		const void *:                     p_,                 \
		char *:        const_cast(char *, p_),                \
		void *:        const_cast(char *, p_)                 \
	);                                                            \
})


ATTR_STRING(1)
ATTR_STRING(2)
inline const char *strprefix_(const char *s, const char *prefix);


/*
 * Return NULL if s does not start with prefix.
 * Return `s + strlen(prefix)` if s starts with prefix.
 */
inline const char *
strprefix_(const char *s, const char *prefix)
{
	if (strncmp(s, prefix, strlen(prefix)) != 0)
		return NULL;

	return s + strlen(prefix);
}


#endif  // include guard