forked from blechschmidt/massdns
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
string.h
114 lines (102 loc) · 2.02 KB
/
string.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef INC_STRING
#define INC_STRING
#include "security.h"
#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
char *strmcpy(const char *str)
{
size_t len = strlen(str);
char *result = safe_malloc(len + 1);
memcpy(result, str, len);
result[len] = 0;
return result;
}
size_t string_copy(char *dest, const char *src, size_t n)
{
size_t i;
for (i = 0; i < n - 1 && src[i] != '\0'; i++) {
dest[i] = src[i];
}
dest[i] = 0;
return i;
}
void *flatcopy(void *src, size_t len)
{
void *dst = safe_malloc(len);
memcpy(dst, src, len);
return dst;
}
void strtolower(char *str)
{
while (*str != '\0')
{
if (*str >= 'A' && *str <= 'Z')
{
*str = (char) (*str | (1 << 5));
}
str++;
}
}
char *trim_start(char *str)
{
while (0 != *str)
{
if(!isspace(*str))
{
return str;
}
str++;
}
return str;
}
void trim_end(char* str)
{
while (0 != *str)
{
if(isspace(*str))
{
*str = 0;
return;
}
str++;
}
}
bool endswith(char* haystack, char* needle, bool case_sensitive)
{
int (*cmp)(const char*, const char*) = strcmp;
if(!case_sensitive)
{
cmp = strcasecmp;
}
size_t haystack_len = strlen(haystack);
size_t needle_len = strlen(needle);
return needle_len <= haystack_len && cmp(haystack + haystack_len - needle_len, needle) == 0;
}
bool startswith(char* haystack, char* needle, bool case_sensitive) // Supports ASCII only
{
while(true)
{
char nchar = *needle++;
char hchar = *haystack++;
if(nchar == 0)
{
return true;
}
else if(hchar == 0)
{
return false;
}
if(case_sensitive)
{
nchar = (char)tolower(nchar);
hchar = (char)tolower(hchar);
}
if(nchar != hchar)
{
return false;
}
}
}
#endif