forked from haasn/libplacebo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
99 lines (82 loc) · 3.39 KB
/
string.c
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
#include "tests.h"
static const pl_str null = {0};
static const pl_str test = PL_STR0("test");
static const pl_str empty = PL_STR0("");
static inline bool is_null(pl_str str)
{
return !str.len && !str.buf;
}
static inline bool is_empty(pl_str str)
{
return !str.len;
}
int main()
{
void *tmp = pl_tmp(NULL);
REQUIRE(is_null(pl_str0(NULL)));
REQUIRE(is_null(pl_strdup(tmp, null)));
char *empty0 = pl_strdup0(tmp, null);
REQUIRE(empty0 && !empty0[0]);
REQUIRE(pl_str_equals0(empty, empty0));
pl_str buf = {0};
pl_str_append(tmp, &buf, null);
REQUIRE(is_null(buf));
pl_str_append_asprintf(tmp, &buf, "%.*s", PL_STR_FMT(test));
REQUIRE(pl_str_equals(buf, test));
REQUIRE(pl_strchr(null, ' ') < 0);
REQUIRE(pl_strspn(null, " ") == 0);
REQUIRE(pl_strcspn(null, " ") == 0);
REQUIRE(is_null(pl_str_strip(null)));
REQUIRE(pl_strchr(test, 's') == 2);
REQUIRE(pl_strspn(test, "et") == 2);
REQUIRE(pl_strcspn(test, "xs") == 2);
REQUIRE(is_null(pl_str_take(null, 10)));
REQUIRE(is_empty(pl_str_take(test, 0)));
REQUIRE(is_null(pl_str_drop(null, 10)));
REQUIRE(is_null(pl_str_drop(test, test.len)));
REQUIRE(pl_str_equals(pl_str_drop(test, 0), test));
REQUIRE(pl_str_find(null, test) < 0);
REQUIRE(pl_str_find(null, null) == 0);
REQUIRE(pl_str_find(test, null) == 0);
REQUIRE(pl_str_find(test, test) == 0);
pl_str rest;
REQUIRE(is_null(pl_str_split_char(null, ' ', &rest)) && is_null(rest));
REQUIRE(is_null(pl_str_split_str(null, test, &rest)) && is_null(rest));
REQUIRE(is_empty(pl_str_split_str(test, test, &rest)) && is_empty(rest));
REQUIRE(is_null(pl_str_getline(null, &rest)) && is_null(rest));
pl_str right, left = pl_str_split_char(pl_str0("left right"), ' ', &right);
REQUIRE(pl_str_equals0(left, "left"));
REQUIRE(pl_str_equals0(right, "right"));
left = pl_str_split_str0(pl_str0("leftTESTright"), "TEST", &right);
REQUIRE(pl_str_equals0(left, "left"));
REQUIRE(pl_str_equals0(right, "right"));
pl_str out;
REQUIRE(pl_str_decode_hex(tmp, null, &out) && is_empty(out));
REQUIRE(!pl_str_decode_hex(tmp, pl_str0("invalid"), &out));
REQUIRE(pl_str_equals(null, null));
REQUIRE(pl_str_equals(null, empty));
REQUIRE(pl_str_startswith(null, null));
REQUIRE(pl_str_startswith(test, null));
REQUIRE(pl_str_startswith(test, test));
REQUIRE(pl_str_endswith(null, null));
REQUIRE(pl_str_endswith(test, null));
REQUIRE(pl_str_endswith(test, test));
float f;
int i;
REQUIRE(pl_str_parse_float(pl_str0("1.3984"), &f) && feq(f, 1.3984f, 1e-8));
REQUIRE(pl_str_parse_float(pl_str0("-8.9100083"), &f) && feq(f, -8.9100083f, 1e-8));
REQUIRE(pl_str_parse_float(pl_str0("-0"), &f) && feq(f, 0.0f, 1e-8));
REQUIRE(pl_str_parse_float(pl_str0("-3.14e20"), &f) && feq(f, -3.14e20f, 1e-8));
REQUIRE(pl_str_parse_float(pl_str0("0.5e-5"), &f) && feq(f, 0.5e-5f, 1e-8));
REQUIRE(pl_str_parse_int(pl_str0("64239"), &i) && i == 64239);
REQUIRE(pl_str_parse_int(pl_str0("-102"), &i) && i == -102);
REQUIRE(pl_str_parse_int(pl_str0("-0"), &i) && i == 0);
REQUIRE(!pl_str_parse_float(null, &f));
REQUIRE(!pl_str_parse_float(test, &f));
REQUIRE(!pl_str_parse_float(empty, &f));
REQUIRE(!pl_str_parse_int(null, &i));
REQUIRE(!pl_str_parse_int(test, &i));
REQUIRE(!pl_str_parse_int(empty, &i));
pl_free(tmp);
return 0;
}