libite 2.6.1
strtrim.c
Go to the documentation of this file.
1/* Trim a string from whitespace
2 *
3 * Copyright (c) 2014 Mattias Walström <lazzer@gmail.com>
4 * Copyright (c) 2021 Joachim Wiberg <troglobit@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
26
27#include <ctype.h>
28#include <errno.h>
29#include <stdlib.h>
30
31#include "lite.h"
32
44char *strtrim(char *str)
45{
46 char *start, *end;
47
48 if (!str) {
49 errno = EINVAL;
50 return NULL;
51 }
52
53 start = str;
54 while (isspace(*start))
55 start++;
56
57 if (*start == 0) {
58 str[0] = 0;
59 return str;
60 }
61
62 end = start + strlen(start) - 1;
63 while (end > start && isspace(*end))
64 end--;
65 *(++end) = 0;
66
67 memmove(str, start, end - start + 1);
68
69 return str;
70}
71
char * strtrim(char *str)
Definition strtrim.c:44