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