string.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "string2.h"
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <stdlib.h>
  6. #include <linux/ctype.h>
  7. const char *graph_dotted_line =
  8. "---------------------------------------------------------------------"
  9. "---------------------------------------------------------------------"
  10. "---------------------------------------------------------------------";
  11. const char *dots =
  12. "....................................................................."
  13. "....................................................................."
  14. ".....................................................................";
  15. #define K 1024LL
  16. /*
  17. * perf_atoll()
  18. * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
  19. * and return its numeric value
  20. */
  21. s64 perf_atoll(const char *str)
  22. {
  23. s64 length;
  24. char *p;
  25. char c;
  26. if (!isdigit(str[0]))
  27. goto out_err;
  28. length = strtoll(str, &p, 10);
  29. switch (c = *p++) {
  30. case 'b': case 'B':
  31. if (*p)
  32. goto out_err;
  33. __fallthrough;
  34. case '\0':
  35. return length;
  36. default:
  37. goto out_err;
  38. /* two-letter suffices */
  39. case 'k': case 'K':
  40. length <<= 10;
  41. break;
  42. case 'm': case 'M':
  43. length <<= 20;
  44. break;
  45. case 'g': case 'G':
  46. length <<= 30;
  47. break;
  48. case 't': case 'T':
  49. length <<= 40;
  50. break;
  51. }
  52. /* we want the cases to match */
  53. if (islower(c)) {
  54. if (strcmp(p, "b") != 0)
  55. goto out_err;
  56. } else {
  57. if (strcmp(p, "B") != 0)
  58. goto out_err;
  59. }
  60. return length;
  61. out_err:
  62. return -1;
  63. }
  64. /* Character class matching */
  65. static bool __match_charclass(const char *pat, char c, const char **npat)
  66. {
  67. bool complement = false, ret = true;
  68. if (*pat == '!') {
  69. complement = true;
  70. pat++;
  71. }
  72. if (*pat++ == c) /* First character is special */
  73. goto end;
  74. while (*pat && *pat != ']') { /* Matching */
  75. if (*pat == '-' && *(pat + 1) != ']') { /* Range */
  76. if (*(pat - 1) <= c && c <= *(pat + 1))
  77. goto end;
  78. if (*(pat - 1) > *(pat + 1))
  79. goto error;
  80. pat += 2;
  81. } else if (*pat++ == c)
  82. goto end;
  83. }
  84. if (!*pat)
  85. goto error;
  86. ret = false;
  87. end:
  88. while (*pat && *pat != ']') /* Searching closing */
  89. pat++;
  90. if (!*pat)
  91. goto error;
  92. *npat = pat + 1;
  93. return complement ? !ret : ret;
  94. error:
  95. return false;
  96. }
  97. /* Glob/lazy pattern matching */
  98. static bool __match_glob(const char *str, const char *pat, bool ignore_space,
  99. bool case_ins)
  100. {
  101. while (*str && *pat && *pat != '*') {
  102. if (ignore_space) {
  103. /* Ignore spaces for lazy matching */
  104. if (isspace(*str)) {
  105. str++;
  106. continue;
  107. }
  108. if (isspace(*pat)) {
  109. pat++;
  110. continue;
  111. }
  112. }
  113. if (*pat == '?') { /* Matches any single character */
  114. str++;
  115. pat++;
  116. continue;
  117. } else if (*pat == '[') /* Character classes/Ranges */
  118. if (__match_charclass(pat + 1, *str, &pat)) {
  119. str++;
  120. continue;
  121. } else
  122. return false;
  123. else if (*pat == '\\') /* Escaped char match as normal char */
  124. pat++;
  125. if (case_ins) {
  126. if (tolower(*str) != tolower(*pat))
  127. return false;
  128. } else if (*str != *pat)
  129. return false;
  130. str++;
  131. pat++;
  132. }
  133. /* Check wild card */
  134. if (*pat == '*') {
  135. while (*pat == '*')
  136. pat++;
  137. if (!*pat) /* Tail wild card matches all */
  138. return true;
  139. while (*str)
  140. if (__match_glob(str++, pat, ignore_space, case_ins))
  141. return true;
  142. }
  143. return !*str && !*pat;
  144. }
  145. /**
  146. * strglobmatch - glob expression pattern matching
  147. * @str: the target string to match
  148. * @pat: the pattern string to match
  149. *
  150. * This returns true if the @str matches @pat. @pat can includes wildcards
  151. * ('*','?') and character classes ([CHARS], complementation and ranges are
  152. * also supported). Also, this supports escape character ('\') to use special
  153. * characters as normal character.
  154. *
  155. * Note: if @pat syntax is broken, this always returns false.
  156. */
  157. bool strglobmatch(const char *str, const char *pat)
  158. {
  159. return __match_glob(str, pat, false, false);
  160. }
  161. bool strglobmatch_nocase(const char *str, const char *pat)
  162. {
  163. return __match_glob(str, pat, false, true);
  164. }
  165. /**
  166. * strlazymatch - matching pattern strings lazily with glob pattern
  167. * @str: the target string to match
  168. * @pat: the pattern string to match
  169. *
  170. * This is similar to strglobmatch, except this ignores spaces in
  171. * the target string.
  172. */
  173. bool strlazymatch(const char *str, const char *pat)
  174. {
  175. return __match_glob(str, pat, true, false);
  176. }
  177. /**
  178. * strtailcmp - Compare the tail of two strings
  179. * @s1: 1st string to be compared
  180. * @s2: 2nd string to be compared
  181. *
  182. * Return 0 if whole of either string is same as another's tail part.
  183. */
  184. int strtailcmp(const char *s1, const char *s2)
  185. {
  186. int i1 = strlen(s1);
  187. int i2 = strlen(s2);
  188. while (--i1 >= 0 && --i2 >= 0) {
  189. if (s1[i1] != s2[i2])
  190. return s1[i1] - s2[i2];
  191. }
  192. return 0;
  193. }
  194. char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
  195. {
  196. /*
  197. * FIXME: replace this with an expression using log10() when we
  198. * find a suitable implementation, maybe the one in the dvb drivers...
  199. *
  200. * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
  201. */
  202. size_t size = nints * 28 + 1; /* \0 */
  203. size_t i, printed = 0;
  204. char *expr = malloc(size);
  205. if (expr) {
  206. const char *or_and = "||", *eq_neq = "==";
  207. char *e = expr;
  208. if (!in) {
  209. or_and = "&&";
  210. eq_neq = "!=";
  211. }
  212. for (i = 0; i < nints; ++i) {
  213. if (printed == size)
  214. goto out_err_overflow;
  215. if (i > 0)
  216. printed += scnprintf(e + printed, size - printed, " %s ", or_and);
  217. printed += scnprintf(e + printed, size - printed,
  218. "%s %s %d", var, eq_neq, ints[i]);
  219. }
  220. }
  221. return expr;
  222. out_err_overflow:
  223. free(expr);
  224. return NULL;
  225. }
  226. /* Like strpbrk(), but not break if it is right after a backslash (escaped) */
  227. char *strpbrk_esc(char *str, const char *stopset)
  228. {
  229. char *ptr;
  230. do {
  231. ptr = strpbrk(str, stopset);
  232. if (ptr == str ||
  233. (ptr == str + 1 && *(ptr - 1) != '\\'))
  234. break;
  235. str = ptr + 1;
  236. } while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
  237. return ptr;
  238. }
  239. /* Like strdup, but do not copy a single backslash */
  240. char *strdup_esc(const char *str)
  241. {
  242. char *s, *d, *p, *ret = strdup(str);
  243. if (!ret)
  244. return NULL;
  245. d = strchr(ret, '\\');
  246. if (!d)
  247. return ret;
  248. s = d + 1;
  249. do {
  250. if (*s == '\0') {
  251. *d = '\0';
  252. break;
  253. }
  254. p = strchr(s + 1, '\\');
  255. if (p) {
  256. memmove(d, s, p - s);
  257. d += p - s;
  258. s = p + 1;
  259. } else
  260. memmove(d, s, strlen(s) + 1);
  261. } while (p);
  262. return ret;
  263. }