mirror of
https://github.com/troglobit/libite.git
synced 2026-10-01 05:22:34 +07:00
Add strtrim(), trims leading and trailing white-space from a string
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
+1
-1
@@ -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 \
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/* Trim a string from whitespace
|
||||
*
|
||||
* Copyright (c) 2014 Mattias Walström <lazzer@gmail.com>
|
||||
* Copyright (c) 2021 Joachim Wiberg <troglobit@gmail.com>
|
||||
*
|
||||
* 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 <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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:
|
||||
*/
|
||||
Reference in New Issue
Block a user