forked from FT-Labs/picom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_utils.c
157 lines (128 loc) · 3.33 KB
/
string_utils.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
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Yuxuan Shui <[email protected]>
#include <string.h>
#include <test.h>
#include "compiler.h"
#include "string_utils.h"
#include "utils.h"
#pragma GCC diagnostic push
// gcc warns about legitimate strncpy in mstrjoin and mstrextend
// strncpy(str, src1, len1) intentional truncates the null byte from src1.
// strncpy(str+len1, src2, len2) uses bound depends on the source argument,
// but str is allocated with len1+len2+1, so this strncpy can't overflow
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#pragma GCC diagnostic ignored "-Wstringop-overflow"
/**
* Allocate the space and join two strings.
*/
char *mstrjoin(const char *src1, const char *src2) {
auto len1 = strlen(src1);
auto len2 = strlen(src2);
auto len = len1 + len2 + 1;
auto str = ccalloc(len, char);
strncpy(str, src1, len1);
strncpy(str + len1, src2, len2);
str[len - 1] = '\0';
return str;
}
TEST_CASE(mstrjoin) {
char *str = mstrjoin("asdf", "qwer");
TEST_STREQUAL(str, "asdfqwer");
free(str);
str = mstrjoin("", "qwer");
TEST_STREQUAL(str, "qwer");
free(str);
str = mstrjoin("asdf", "");
TEST_STREQUAL(str, "asdf");
free(str);
}
/**
* Concatenate a string on heap with another string.
*/
void mstrextend(char **psrc1, const char *src2) {
if (!*psrc1) {
*psrc1 = strdup(src2);
return;
}
auto len1 = strlen(*psrc1);
auto len2 = strlen(src2);
auto len = len1 + len2 + 1;
*psrc1 = crealloc(*psrc1, len);
strncpy(*psrc1 + len1, src2, len2);
(*psrc1)[len - 1] = '\0';
}
TEST_CASE(mstrextend) {
char *str1 = NULL;
mstrextend(&str1, "asdf");
TEST_STREQUAL(str1, "asdf");
mstrextend(&str1, "asd");
TEST_STREQUAL(str1, "asdfasd");
mstrextend(&str1, "");
TEST_STREQUAL(str1, "asdfasd");
free(str1);
}
#pragma GCC diagnostic pop
/// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*)
double strtod_simple(const char *src, const char **end) {
double neg = 1;
if (*src == '-') {
neg = -1;
src++;
} else if (*src == '+') {
src++;
}
double ret = 0;
while (*src >= '0' && *src <= '9') {
ret = ret * 10 + (*src - '0');
src++;
}
if (*src == '.') {
double frac = 0, mult = 0.1;
src++;
while (*src >= '0' && *src <= '9') {
frac += mult * (*src - '0');
mult *= 0.1;
src++;
}
ret += frac;
}
*end = src;
return ret * neg;
}
TEST_CASE(strtod_simple) {
const char *end;
double result = strtod_simple("1.0", &end);
TEST_EQUAL(result, 1);
TEST_EQUAL(*end, '\0');
result = strtod_simple("-1.0", &end);
TEST_EQUAL(result, -1);
TEST_EQUAL(*end, '\0');
result = strtod_simple("+.5", &end);
TEST_EQUAL(result, 0.5);
TEST_EQUAL(*end, '\0');
}
const char *trim_both(const char *src, size_t *length) {
size_t i = 0;
while (isspace(src[i])) {
i++;
}
size_t j = strlen(src) - 1;
while (j > i && isspace(src[j])) {
j--;
}
*length = j - i + 1;
return src + i;
}
TEST_CASE(trim_both) {
size_t length;
const char *str = trim_both(" \t\n\r\f", &length);
TEST_EQUAL(length, 0);
TEST_EQUAL(*str, '\0');
str = trim_both(" asdfas ", &length);
TEST_EQUAL(length, 6);
TEST_STRNEQUAL(str, "asdfas", length);
str = trim_both(" asdf asdf ", &length);
TEST_EQUAL(length, 9);
TEST_STRNEQUAL(str, "asdf asdf", length);
}