summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-04 11:52:02 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-04 12:04:52 -0300
commit9814032f6c264102b22fcbfe5a51e230fe6037d1 (patch)
treeaf0b371387ecd06f0dda8f41c6dc598ea034809e
parent2178b6f62737bf78971a9075d96ae66cbb99c993 (diff)
misc: Add `remove_trailing_whitespace()`
This function removes any whitespace found at the end of the supplied string. This can be useful when you have a string with whitespace throughout it, but only want the whitespace at the end removed.
-rw-r--r--misc.c11
-rw-r--r--misc.h1
2 files changed, 12 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 36bbad4..462a3bd 100644
--- a/misc.c
+++ b/misc.c
@@ -89,6 +89,17 @@ char *find_delm(char *str, const char *delm, int skip_delm) {
}
}
+char *remove_trailing_whitespace(char *str) {
+ const char *whitespace = " \t\v\r\n";
+ char *s = str;
+
+ for (char *tmp = s; !is_empty(tmp); s = tmp, tmp = find_delm(tmp, whitespace, 1));
+
+ s = find_delm(s, whitespace, 0);
+ *s = '\0';
+ return str;
+}
+
char *get_str_delm_range(char *str, const char *start_delm, const char *end_delm, char **rhs) {
if (!is_empty(str)) {
char *start = find_delm(str, start_delm, 1);
diff --git a/misc.h b/misc.h
index eb839de..338c546 100644
--- a/misc.h
+++ b/misc.h
@@ -5,6 +5,7 @@
extern char *read_file(const char *filename, long *size);
extern char *find_delm(char *str, const char *delm, int skip_delm);
+extern char *remove_trailing_whitespace(char *str);
extern char *get_str_delm_range(char *str, const char *start_delm, const char *end_delm, char **rhs);
extern char *get_line(char **str);
extern char *make_str(const char *str);