-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.h
167 lines (153 loc) · 3.62 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#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)
{
char *last = str + strlen(str) - 1;
while (last >= str)
{
if(!isspace(*last))
{
return;
}
*last = 0;
last--;
}
}
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;
}
}
}
// Buffer needs to have at least one byte.
size_t json_escape(char *dst, const char *src, size_t dst_len)
{
#define require_space(N) if(dst_idx >= dst_len - (N)) goto json_escape_finalize;
const char complex_chars[] = "abtnvfr";
size_t dst_idx = 0;
for(size_t i = 0; src[i] != 0; i++)
{
size_t complex_idx = 0;
switch(src[i])
{
case '\\':
case '\"':
require_space(2);
dst[dst_idx++] = '\\';
dst[dst_idx++] = src[i];
break;
case '\r': complex_idx++;
case '\f': complex_idx++;
case '\v': complex_idx++;
case '\n': complex_idx++;
case '\t': complex_idx++;
case '\b': complex_idx++;
case '\a':
require_space(2);
dst[dst_idx++] = '\\';
dst[dst_idx++] = complex_chars[complex_idx];
break;
default:
if(isprint(src[i]))
{
require_space(1);
dst[dst_idx++] = src[i];
}
else
{
require_space(4);
dst[dst_idx++] = '\\';
dst[dst_idx++] = ((src[i] & 0300) >> 6) + '0';
dst[dst_idx++] = ((src[i] & 0070) >> 3) + '0';
dst[dst_idx++] = ((src[i] & 0007) >> 0) + '0';
}
break;
}
}
#undef require_space
json_escape_finalize:
dst[dst_idx++] = 0;
return dst_idx;
}
#endif