blob: 4b92cd923f2e896560d72603ad0112e129bdce2c [file] [log] [blame]
Joe Hershberger32ec63c2012-12-11 22:16:18 -06001/*
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 */
20char *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.
Simon Glass5e31a282025-05-01 05:10:08 -060034 *
35 * Note that if the string consist of only spaces, then the terminator is placed
36 * at the start of the string, with the return value pointing there also.
Joe Hershberger32ec63c2012-12-11 22:16:18 -060037 */
38char *strim(char *s)
39{
40 size_t size;
41 char *end;
42
Joe Hershberger32ec63c2012-12-11 22:16:18 -060043 size = strlen(s);
44 if (!size)
45 return s;
46
47 end = s + size - 1;
48 while (end >= s && isspace(*end))
49 end--;
50 *(end + 1) = '\0';
51
Simon Glass5e31a282025-05-01 05:10:08 -060052 return skip_spaces(s);
Joe Hershberger32ec63c2012-12-11 22:16:18 -060053}