.. | .. |
---|
17 | 17 | #include <string.h> |
---|
18 | 18 | #include <errno.h> |
---|
19 | 19 | #include <linux/string.h> |
---|
| 20 | +#include <linux/ctype.h> |
---|
20 | 21 | #include <linux/compiler.h> |
---|
21 | 22 | |
---|
22 | 23 | /** |
---|
.. | .. |
---|
113 | 114 | #ifdef __clang__ |
---|
114 | 115 | #pragma clang diagnostic pop |
---|
115 | 116 | #endif |
---|
| 117 | + |
---|
| 118 | +/** |
---|
| 119 | + * skip_spaces - Removes leading whitespace from @str. |
---|
| 120 | + * @str: The string to be stripped. |
---|
| 121 | + * |
---|
| 122 | + * Returns a pointer to the first non-whitespace character in @str. |
---|
| 123 | + */ |
---|
| 124 | +char *skip_spaces(const char *str) |
---|
| 125 | +{ |
---|
| 126 | + while (isspace(*str)) |
---|
| 127 | + ++str; |
---|
| 128 | + return (char *)str; |
---|
| 129 | +} |
---|
| 130 | + |
---|
| 131 | +/** |
---|
| 132 | + * strim - Removes leading and trailing whitespace from @s. |
---|
| 133 | + * @s: The string to be stripped. |
---|
| 134 | + * |
---|
| 135 | + * Note that the first trailing whitespace is replaced with a %NUL-terminator |
---|
| 136 | + * in the given string @s. Returns a pointer to the first non-whitespace |
---|
| 137 | + * character in @s. |
---|
| 138 | + */ |
---|
| 139 | +char *strim(char *s) |
---|
| 140 | +{ |
---|
| 141 | + size_t size; |
---|
| 142 | + char *end; |
---|
| 143 | + |
---|
| 144 | + size = strlen(s); |
---|
| 145 | + if (!size) |
---|
| 146 | + return s; |
---|
| 147 | + |
---|
| 148 | + end = s + size - 1; |
---|
| 149 | + while (end >= s && isspace(*end)) |
---|
| 150 | + end--; |
---|
| 151 | + *(end + 1) = '\0'; |
---|
| 152 | + |
---|
| 153 | + return skip_spaces(s); |
---|
| 154 | +} |
---|
| 155 | + |
---|
| 156 | +/** |
---|
| 157 | + * strreplace - Replace all occurrences of character in string. |
---|
| 158 | + * @s: The string to operate on. |
---|
| 159 | + * @old: The character being replaced. |
---|
| 160 | + * @new: The character @old is replaced with. |
---|
| 161 | + * |
---|
| 162 | + * Returns pointer to the nul byte at the end of @s. |
---|
| 163 | + */ |
---|
| 164 | +char *strreplace(char *s, char old, char new) |
---|
| 165 | +{ |
---|
| 166 | + for (; *s; ++s) |
---|
| 167 | + if (*s == old) |
---|
| 168 | + *s = new; |
---|
| 169 | + return s; |
---|
| 170 | +} |
---|
| 171 | + |
---|
| 172 | +static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
---|
| 173 | +{ |
---|
| 174 | + while (bytes) { |
---|
| 175 | + if (*start != value) |
---|
| 176 | + return (void *)start; |
---|
| 177 | + start++; |
---|
| 178 | + bytes--; |
---|
| 179 | + } |
---|
| 180 | + return NULL; |
---|
| 181 | +} |
---|
| 182 | + |
---|
| 183 | +/** |
---|
| 184 | + * memchr_inv - Find an unmatching character in an area of memory. |
---|
| 185 | + * @start: The memory area |
---|
| 186 | + * @c: Find a character other than c |
---|
| 187 | + * @bytes: The size of the area. |
---|
| 188 | + * |
---|
| 189 | + * returns the address of the first character other than @c, or %NULL |
---|
| 190 | + * if the whole buffer contains just @c. |
---|
| 191 | + */ |
---|
| 192 | +void *memchr_inv(const void *start, int c, size_t bytes) |
---|
| 193 | +{ |
---|
| 194 | + u8 value = c; |
---|
| 195 | + u64 value64; |
---|
| 196 | + unsigned int words, prefix; |
---|
| 197 | + |
---|
| 198 | + if (bytes <= 16) |
---|
| 199 | + return check_bytes8(start, value, bytes); |
---|
| 200 | + |
---|
| 201 | + value64 = value; |
---|
| 202 | + value64 |= value64 << 8; |
---|
| 203 | + value64 |= value64 << 16; |
---|
| 204 | + value64 |= value64 << 32; |
---|
| 205 | + |
---|
| 206 | + prefix = (unsigned long)start % 8; |
---|
| 207 | + if (prefix) { |
---|
| 208 | + u8 *r; |
---|
| 209 | + |
---|
| 210 | + prefix = 8 - prefix; |
---|
| 211 | + r = check_bytes8(start, value, prefix); |
---|
| 212 | + if (r) |
---|
| 213 | + return r; |
---|
| 214 | + start += prefix; |
---|
| 215 | + bytes -= prefix; |
---|
| 216 | + } |
---|
| 217 | + |
---|
| 218 | + words = bytes / 8; |
---|
| 219 | + |
---|
| 220 | + while (words) { |
---|
| 221 | + if (*(u64 *)start != value64) |
---|
| 222 | + return check_bytes8(start, value, 8); |
---|
| 223 | + start += 8; |
---|
| 224 | + words--; |
---|
| 225 | + } |
---|
| 226 | + |
---|
| 227 | + return check_bytes8(start, value, bytes % 8); |
---|
| 228 | +} |
---|