]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/string.c
perf tools: Move extra string util functions to util/string2.h
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / string.c
CommitLineData
a067558e
ACM
1#include "string2.h"
2#include <linux/kernel.h>
3#include <linux/string.h>
4#include <stdlib.h>
ea5cc87c 5
3d689ed6
ACM
6#include "sane_ctype.h"
7
d2fb8b41
HM
8#define K 1024LL
9/*
10 * perf_atoll()
11 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
12 * and return its numeric value
13 */
14s64 perf_atoll(const char *str)
15{
8ba7f6c2
AV
16 s64 length;
17 char *p;
18 char c;
d2fb8b41
HM
19
20 if (!isdigit(str[0]))
21 goto out_err;
22
8ba7f6c2
AV
23 length = strtoll(str, &p, 10);
24 switch (c = *p++) {
25 case 'b': case 'B':
26 if (*p)
d2fb8b41 27 goto out_err;
94bdd5ed
ACM
28
29 __fallthrough;
8ba7f6c2
AV
30 case '\0':
31 return length;
32 default:
33 goto out_err;
34 /* two-letter suffices */
35 case 'k': case 'K':
36 length <<= 10;
d2fb8b41 37 break;
8ba7f6c2
AV
38 case 'm': case 'M':
39 length <<= 20;
d2fb8b41 40 break;
8ba7f6c2
AV
41 case 'g': case 'G':
42 length <<= 30;
d2fb8b41 43 break;
8ba7f6c2
AV
44 case 't': case 'T':
45 length <<= 40;
d2fb8b41 46 break;
d2fb8b41 47 }
8ba7f6c2
AV
48 /* we want the cases to match */
49 if (islower(c)) {
50 if (strcmp(p, "b") != 0)
51 goto out_err;
52 } else {
53 if (strcmp(p, "B") != 0)
54 goto out_err;
55 }
56 return length;
d2fb8b41
HM
57
58out_err:
8ba7f6c2 59 return -1;
d2fb8b41 60}
e1c01d61
MH
61
62/*
63 * Helper function for splitting a string into an argv-like array.
25985edc 64 * originally copied from lib/argv_split.c
e1c01d61
MH
65 */
66static const char *skip_sep(const char *cp)
67{
68 while (*cp && isspace(*cp))
69 cp++;
70
71 return cp;
72}
73
74static const char *skip_arg(const char *cp)
75{
76 while (*cp && !isspace(*cp))
77 cp++;
78
79 return cp;
80}
81
82static int count_argc(const char *str)
83{
84 int count = 0;
85
86 while (*str) {
87 str = skip_sep(str);
88 if (*str) {
89 count++;
90 str = skip_arg(str);
91 }
92 }
93
94 return count;
95}
96
97/**
98 * argv_free - free an argv
99 * @argv - the argument vector to be freed
100 *
101 * Frees an argv and the strings it points to.
102 */
103void argv_free(char **argv)
104{
105 char **p;
a067558e
ACM
106 for (p = argv; *p; p++) {
107 free(*p);
108 *p = NULL;
109 }
e1c01d61
MH
110
111 free(argv);
112}
113
114/**
115 * argv_split - split a string at whitespace, returning an argv
116 * @str: the string to be split
117 * @argcp: returned argument count
118 *
119 * Returns an array of pointers to strings which are split out from
120 * @str. This is performed by strictly splitting on white-space; no
121 * quote processing is performed. Multiple whitespace characters are
122 * considered to be a single argument separator. The returned array
123 * is always NULL-terminated. Returns NULL on memory allocation
124 * failure.
125 */
126char **argv_split(const char *str, int *argcp)
127{
128 int argc = count_argc(str);
a067558e 129 char **argv = calloc(argc + 1, sizeof(*argv));
e1c01d61
MH
130 char **argvp;
131
132 if (argv == NULL)
133 goto out;
134
135 if (argcp)
136 *argcp = argc;
137
138 argvp = argv;
139
140 while (*str) {
141 str = skip_sep(str);
142
143 if (*str) {
144 const char *p = str;
145 char *t;
146
147 str = skip_arg(str);
148
149 t = strndup(p, str-p);
150 if (t == NULL)
151 goto fail;
152 *argvp++ = t;
153 }
154 }
155 *argvp = NULL;
156
157out:
158 return argv;
159
160fail:
161 argv_free(argv);
162 return NULL;
163}
bbbb521b 164
6964cd2c
MH
165/* Character class matching */
166static bool __match_charclass(const char *pat, char c, const char **npat)
167{
168 bool complement = false, ret = true;
169
170 if (*pat == '!') {
171 complement = true;
172 pat++;
173 }
174 if (*pat++ == c) /* First character is special */
175 goto end;
176
177 while (*pat && *pat != ']') { /* Matching */
178 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
179 if (*(pat - 1) <= c && c <= *(pat + 1))
180 goto end;
181 if (*(pat - 1) > *(pat + 1))
182 goto error;
183 pat += 2;
184 } else if (*pat++ == c)
185 goto end;
186 }
187 if (!*pat)
188 goto error;
189 ret = false;
190
191end:
192 while (*pat && *pat != ']') /* Searching closing */
193 pat++;
194 if (!*pat)
195 goto error;
196 *npat = pat + 1;
197 return complement ? !ret : ret;
198
199error:
200 return false;
201}
202
2a9c8c36 203/* Glob/lazy pattern matching */
38d14f0c
AK
204static bool __match_glob(const char *str, const char *pat, bool ignore_space,
205 bool case_ins)
bbbb521b
MH
206{
207 while (*str && *pat && *pat != '*') {
2a9c8c36
MH
208 if (ignore_space) {
209 /* Ignore spaces for lazy matching */
210 if (isspace(*str)) {
211 str++;
212 continue;
213 }
214 if (isspace(*pat)) {
215 pat++;
216 continue;
217 }
218 }
6964cd2c 219 if (*pat == '?') { /* Matches any single character */
bbbb521b
MH
220 str++;
221 pat++;
6964cd2c
MH
222 continue;
223 } else if (*pat == '[') /* Character classes/Ranges */
224 if (__match_charclass(pat + 1, *str, &pat)) {
225 str++;
226 continue;
227 } else
bbbb521b 228 return false;
6964cd2c
MH
229 else if (*pat == '\\') /* Escaped char match as normal char */
230 pat++;
38d14f0c
AK
231 if (case_ins) {
232 if (tolower(*str) != tolower(*pat))
233 return false;
234 } else if (*str != *pat)
6964cd2c 235 return false;
38d14f0c
AK
236 str++;
237 pat++;
bbbb521b
MH
238 }
239 /* Check wild card */
240 if (*pat == '*') {
241 while (*pat == '*')
242 pat++;
243 if (!*pat) /* Tail wild card matches all */
244 return true;
245 while (*str)
38d14f0c 246 if (__match_glob(str++, pat, ignore_space, case_ins))
bbbb521b
MH
247 return true;
248 }
249 return !*str && !*pat;
250}
251
2a9c8c36
MH
252/**
253 * strglobmatch - glob expression pattern matching
254 * @str: the target string to match
255 * @pat: the pattern string to match
256 *
257 * This returns true if the @str matches @pat. @pat can includes wildcards
258 * ('*','?') and character classes ([CHARS], complementation and ranges are
259 * also supported). Also, this supports escape character ('\') to use special
260 * characters as normal character.
261 *
262 * Note: if @pat syntax is broken, this always returns false.
263 */
264bool strglobmatch(const char *str, const char *pat)
265{
38d14f0c
AK
266 return __match_glob(str, pat, false, false);
267}
268
269bool strglobmatch_nocase(const char *str, const char *pat)
270{
271 return __match_glob(str, pat, false, true);
2a9c8c36
MH
272}
273
274/**
275 * strlazymatch - matching pattern strings lazily with glob pattern
276 * @str: the target string to match
277 * @pat: the pattern string to match
278 *
279 * This is similar to strglobmatch, except this ignores spaces in
280 * the target string.
281 */
282bool strlazymatch(const char *str, const char *pat)
283{
38d14f0c 284 return __match_glob(str, pat, true, false);
2a9c8c36 285}
bad03ae4
MH
286
287/**
288 * strtailcmp - Compare the tail of two strings
289 * @s1: 1st string to be compared
290 * @s2: 2nd string to be compared
291 *
292 * Return 0 if whole of either string is same as another's tail part.
293 */
294int strtailcmp(const char *s1, const char *s2)
295{
296 int i1 = strlen(s1);
297 int i2 = strlen(s2);
298 while (--i1 >= 0 && --i2 >= 0) {
299 if (s1[i1] != s2[i2])
300 return s1[i1] - s2[i2];
301 }
302 return 0;
303}
304
ea36c46b
JO
305/**
306 * strxfrchar - Locate and replace character in @s
307 * @s: The string to be searched/changed.
308 * @from: Source character to be replaced.
309 * @to: Destination character.
310 *
311 * Return pointer to the changed string.
312 */
313char *strxfrchar(char *s, char from, char to)
314{
315 char *p = s;
316
317 while ((p = strchr(p, from)) != NULL)
318 *p++ = to;
319
320 return s;
321}
322
08aa9cce
NK
323/**
324 * ltrim - Removes leading whitespace from @s.
325 * @s: The string to be stripped.
326 *
327 * Return pointer to the first non-whitespace character in @s.
328 */
329char *ltrim(char *s)
330{
ecbe5e10 331 while (isspace(*s))
08aa9cce 332 s++;
08aa9cce
NK
333
334 return s;
335}
336
cb1a28a0
ACM
337/**
338 * rtrim - Removes trailing whitespace from @s.
339 * @s: The string to be stripped.
340 *
341 * Note that the first trailing whitespace is replaced with a %NUL-terminator
342 * in the given string @s. Returns @s.
343 */
344char *rtrim(char *s)
345{
346 size_t size = strlen(s);
347 char *end;
348
349 if (!size)
350 return s;
351
352 end = s + size - 1;
353 while (end >= s && isspace(*end))
354 end--;
355 *(end + 1) = '\0';
356
357 return s;
358}
b232e073 359
93ec4ce7
ACM
360char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
361{
362 /*
363 * FIXME: replace this with an expression using log10() when we
364 * find a suitable implementation, maybe the one in the dvb drivers...
365 *
366 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
367 */
368 size_t size = nints * 28 + 1; /* \0 */
369 size_t i, printed = 0;
370 char *expr = malloc(size);
371
372 if (expr) {
373 const char *or_and = "||", *eq_neq = "==";
374 char *e = expr;
375
376 if (!in) {
377 or_and = "&&";
378 eq_neq = "!=";
379 }
380
381 for (i = 0; i < nints; ++i) {
382 if (printed == size)
383 goto out_err_overflow;
384
385 if (i > 0)
a067558e 386 printed += scnprintf(e + printed, size - printed, " %s ", or_and);
93ec4ce7
ACM
387 printed += scnprintf(e + printed, size - printed,
388 "%s %s %d", var, eq_neq, ints[i]);
389 }
390 }
391
392 return expr;
393
394out_err_overflow:
395 free(expr);
396 return NULL;
397}