Debian Codegraph @

src/util/string.h

raw
1 #pragma once
2
3 /*
4 * String Helpers
5 */
6
7 #include <c-stdaux.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 enum {
12 _UTIL_STRING_E_SUCCESS,
13
14 UTIL_STRING_E_INVALID,
15 UTIL_STRING_E_RANGE,
16 };
17
18 int util_strtou32(uint32_t *valp, const char *string);
19 int util_strtou64(uint64_t *valp, const char *string);
20 int util_strtoint(int *valp, const char *string);
21
22 /**
23 * string_compare() - compare two strings
24 * @a: first string to compare, or NULL
25 * @b: second string to compare, or NULL
26 *
27 * Compare two strings, the same way strcmp() does it.
28 * Additionally, NULL is allowed as input, which compares equal to itself
29 * and smaller than any other string.
30 *
31 * Return: Less than, greater than or equal to zero, as strcmp().
32 */
33 _c_pure_ static inline int string_compare(const char *a, const char *b) {
34 if (a == b)
35 return 0;
36
37 return (!a || !b) ? (a ? 1 : -1) : strcmp(a, b);
38 }
39
40 /**
41 * string_equal() - compare strings for equality
42 * @a: first string to compare, or NULL
43 * @b: second string to compare, or NULL
44 *
45 * Compare two strings for equality, the same way strcmp() does it.
46 * Additionally, NULL is allowed as input and compares equal to itself only.
47 * Unlike strcmp(), this returns a boolean.
48 *
49 * Return: True if both are equal, false if not.
50 */
51 _c_pure_ static inline bool string_equal(const char *a, const char *b) {
52 return (!a || !b) ? (a == b) : !strcmp(a, b);
53 }
54
55 /**
56 * string_prefix() - check prefix of a string
57 * @str: string to check
58 * @prefix: prefix to look for
59 *
60 * This checks whether @str starts with @prefix. If it does, a pointer to the
61 * first character in @str after the prefix is returned, if not, NULL is
62 * returned.
63 *
64 * Return: Pointer directly behind the prefix in @str, or NULL if not found.
65 */
66 _c_pure_ static inline char *string_prefix(const char *str, const char *prefix) {
67 size_t l = strlen(prefix);
68 return !strncmp(str, prefix, l) ? (char *)str + l : NULL;
69 }
70
71 /**
72 * string_to_hex() - encode string as ascii-hex
73 * @str: string to encode from
74 * @n: length of @str in bytes
75 * @hex: destination buffer
76 *
77 * This hex-encodes the source string into the destination buffer. The
78 * destination buffer must be at least twice as big as the source.
79 */
80 static inline void string_to_hex(const char *str, size_t n, char *hex) {
81 // Include terminating NUL to silence warnings about truncated strings.
82 static const char table[17] = "0123456789abcdef";
83 size_t i;
84
85 for (i = 0; i < n; ++i) {
86 *hex++ = table[(*str >> 4) & 0x0f];
87 *hex++ = table[(*str++) & 0x0f];
88 }
89 }
90
91 /**
92 * string_from_hex() - decode ascii-hex string
93 * @str: string buffer to write into
94 * @n: length of @str in bytes
95 * @hex: hex encoded buffer to decode
96 *
97 * This hex-decodes @hex into the string buffer @str. Be aware that @hex must
98 * be twice the size as @str / @n.
99 *
100 * Return: True if successful, false if invalid.
101 */
102 static inline bool string_from_hex(char *str, size_t n, const char *hex) {
103 static const uint8_t table[128] = {
104 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
105 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
106 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
107 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, -1, -1, -1, -1, -1, -1,
108 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
109 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
110 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
112 };
113 uint8_t v1, v2;
114
115 for ( ; n; --n, hex += 2) {
116 v1 = table[hex[0] & 0x7f];
117 v2 = table[hex[1] & 0x7f];
118 if (_c_unlikely_((hex[0] | hex[1] | v1 | v2) & 0x80))
119 return false;
120
121 *str++ = (v1 << 4) | v2;
122 }
123
124 return true;
125 }