diff --git a/README.md b/README.md index c519066..92456cf 100644 --- a/README.md +++ b/README.md @@ -319,6 +319,11 @@ Generic Functions match, or `-1` on error or not found. `strnmatch()` takes an extra arg to compare only `len` number of characters from `str`. +- `strtrim(str)` + + Trims a string from any leading and trailing white-space, returns the + trimmed result in the same buffer. + - `systemf(fmt, ...)` Like `system()`, but takes a formatted string as argument. This diff --git a/src/Makefile.am b/src/Makefile.am index 630a50f..888a535 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ libite_la_SOURCES = chomp.c copyfile.c \ ifconfig.c lfile.c \ makepath.c progress.c \ pidfile.c pidfilefn.c \ - reallocarray.c rsync.c \ + reallocarray.c rsync.c strtrim.c \ strlcpy.c strlcat.c strtonum.c \ strdupa.h strndupa.h strnlen.h \ strmatch.c systemf.c touchf.c \ diff --git a/src/strlite.h b/src/strlite.h index 726f1b6..19517a1 100644 --- a/src/strlite.h +++ b/src/strlite.h @@ -67,6 +67,8 @@ size_t strlcat (char *dst, const char *src, size_t siz); long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp); #endif +char *strtrim (char *str); + /* Convert string to natural number (0-2147483647), returns -1 on error. */ static inline int atonum(const char *str) { diff --git a/src/strtrim.c b/src/strtrim.c new file mode 100644 index 0000000..5bdb3ee --- /dev/null +++ b/src/strtrim.c @@ -0,0 +1,70 @@ +/* Trim a string from whitespace + * + * Copyright (c) 2014 Mattias Walström + * Copyright (c) 2021 Joachim Wiberg + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "lite.h" + +/** + * strtrim - Strip leading and trailing whitespace from a string + * @str: The string to trim + * + * Trims a string from any leading and trailing white-space, returns the + * trimmed result in the same buffer. + * + * Returns: + * If @str is a valid, non-NULL, string this function returns the same + * string stripped from whitespace. This function only returns %NULL + * if @str itself is %NULL. + */ +char *strtrim(char *str) +{ + char *start, *end; + + if (!str) { + errno = EINVAL; + return NULL; + } + + start = str; + while (isspace(*start)) + start++; + + if (*start == 0) { + str[0] = 0; + return str; + } + + end = start + strlen(start) - 1; + while (end > start && isspace(*end)) + end--; + *(++end) = 0; + + memmove(str, start, end - start + 1); + + return str; +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */