trim.c 697 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please acknowledge your work.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. char* FAST_FUNC trim(char *s)
  12. {
  13. size_t len = strlen(s);
  14. size_t old = len;
  15. /* trim trailing whitespace */
  16. while (len && isspace(s[len-1]))
  17. --len;
  18. /* trim leading whitespace */
  19. if (len) {
  20. char *nws = skip_whitespace(s);
  21. if ((nws - s) != 0) {
  22. len -= (nws - s);
  23. memmove(s, nws, len);
  24. }
  25. }
  26. s += len;
  27. /* If it was a "const char*" which does not need trimming,
  28. * avoid superfluous store */
  29. if (old != len)
  30. *s = '\0';
  31. return s;
  32. }