blob: 805d7119eb8500798b42a0b97681c0779beae897 (
plain)
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
54
55
56
|
/* string.c - various string-related functions that don't come with string.h */
#include <string.h>
#include <stdbool.h>
/* startswith() - Check if string starts with prefix.
*
* Args:
* string - String to check (haystack).
* prefix - Prefix to check (needle).
*
* Returns:
* true if 'string' starts with 'prefix', otherwise false.
*/
bool startswith(const char *string, const char *prefix) {
if ((string == NULL) || (prefix == NULL)) {
return false;
}
while (*prefix) {
if (*prefix++ != *string++) {
return false;
}
}
return true;
}
/* endswith() - Check if string ends with suffix.
*
* Args:
* string - String to check (haystack).
* suffix - Suffix to check (needle).
*
* Returns:
* true if 'string' ends with 'suffix', otherwise false.
*/
bool endswith(const char *string, const char *suffix) {
size_t string_length;
size_t suffix_length;
if ((string == NULL) || (suffix == NULL)) {
return false;
}
string_length = strlen(string);
suffix_length = strlen(suffix);
if (suffix_length > string_length) {
return false;
}
return (strncmp(string + string_length - suffix_length,
suffix,
suffix_length) == 0) ? true : false;
}
|