linux_string.c 929 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * linux/lib/string.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #ifdef USE_HOSTCC
  7. #include <stdio.h>
  8. #endif
  9. #include <linux/ctype.h>
  10. #include <linux/string.h>
  11. /**
  12. * skip_spaces - Removes leading whitespace from @str.
  13. * @str: The string to be stripped.
  14. *
  15. * Returns a pointer to the first non-whitespace character in @str.
  16. */
  17. char *skip_spaces(const char *str)
  18. {
  19. while (isspace(*str))
  20. ++str;
  21. return (char *)str;
  22. }
  23. /**
  24. * strim - Removes leading and trailing whitespace from @s.
  25. * @s: The string to be stripped.
  26. *
  27. * Note that the first trailing whitespace is replaced with a %NUL-terminator
  28. * in the given string @s. Returns a pointer to the first non-whitespace
  29. * character in @s.
  30. */
  31. char *strim(char *s)
  32. {
  33. size_t size;
  34. char *end;
  35. s = skip_spaces(s);
  36. size = strlen(s);
  37. if (!size)
  38. return s;
  39. end = s + size - 1;
  40. while (end >= s && isspace(*end))
  41. end--;
  42. *(end + 1) = '\0';
  43. return s;
  44. }