// SPDX-License-Identifier: GPL-2.0 /* * linux/tools/lib/string.c * * Copied from linux/lib/string.c, where it is: * * Copyright (C) 1991, 1992 Linus Torvalds * * More specifically, the first copied function was strtobool, which * was introduced by: * * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents") * Author: Jonathan Cameron <jic23@cam.ac.uk>
*/
/** * memdup - duplicate region of memory * * @src: memory region to duplicate * @len: memory region length
*/ void *memdup(constvoid *src, size_t len)
{ void *p = malloc(len);
if (p)
memcpy(p, src, len);
return p;
}
/** * strtobool - convert common user inputs into boolean values * @s: input string * @res: result * * This routine returns 0 iff the first character is one of 'Yy1Nn0', or * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value * pointed to by res is updated upon finding a match.
*/ int strtobool(constchar *s, bool *res)
{ if (!s) return -EINVAL;
/** * strlcpy - Copy a C-string into a sized buffer * @dest: Where to copy the string to * @src: Where to copy the string from * @size: size of destination buffer * * Compatible with *BSD: the result is always a valid * NUL-terminated string that fits in the buffer (unless, * of course, the buffer size is zero). It does not pad * out the result like strncpy() does. * * If libc has strlcpy() then that version will override this * implementation:
*/ #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wignored-attributes" #endif
size_t __weak strlcpy(char *dest, constchar *src, size_t size)
{
size_t ret = strlen(src);
/** * skip_spaces - Removes leading whitespace from @str. * @str: The string to be stripped. * * Returns a pointer to the first non-whitespace character in @str.
*/ char *skip_spaces(constchar *str)
{ while (isspace(*str))
++str; return (char *)str;
}
/** * strim - Removes leading and trailing whitespace from @s. * @s: The string to be stripped. * * Note that the first trailing whitespace is replaced with a %NUL-terminator * in the given string @s. Returns a pointer to the first non-whitespace * character in @s.
*/ char *strim(char *s)
{
size_t size; char *end;
size = strlen(s); if (!size) return s;
end = s + size - 1; while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';
do { while (*d == ' ')
++d;
} while ((*s++ = *d++));
}
/** * strreplace - Replace all occurrences of character in string. * @s: The string to operate on. * @old: The character being replaced. * @new: The character @old is replaced with. * * Returns pointer to the nul byte at the end of @s.
*/ char *strreplace(char *s, char old, charnew)
{ for (; *s; ++s) if (*s == old)
*s = new; return s;
}
/** * memchr_inv - Find an unmatching character in an area of memory. * @start: The memory area * @c: Find a character other than c * @bytes: The size of the area. * * returns the address of the first character other than @c, or %NULL * if the whole buffer contains just @c.
*/ void *memchr_inv(constvoid *start, int c, size_t bytes)
{
u8 value = c;
u64 value64; unsignedint words, prefix;
if (bytes <= 16) return check_bytes8(start, value, bytes);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.