summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
authordefault <nobody@localhost>2023-01-10 08:45:36 +0100
committerdefault <nobody@localhost>2023-01-10 08:45:36 +0100
commit061291cabfe23b5c7742ac5bc4dc7e968278ad00 (patch)
tree15debb64bf1893106a9919d0f1d7b5f6cda59527 /xs.h
parentc37dd39da2d35a7b53c75cf0cc2985f25391b024 (diff)
Backport from xs.
Diffstat (limited to 'xs.h')
-rw-r--r--xs.h18
1 files changed, 13 insertions, 5 deletions
diff --git a/xs.h b/xs.h
index 666f536..68236a1 100644
--- a/xs.h
+++ b/xs.h
@@ -459,13 +459,21 @@ d_char *xs_crop(d_char *str, int start, int end)
d_char *xs_strip_chars(d_char *str, const char *chars)
/* strips the string of chars from the start and the end */
{
- int s, e;
+ int n;
+
+ /* strip first from the end */
+ for (n = strlen(str); n > 0 && strchr(chars, str[n - 1]); n--);
+ str[n] = '\0';
- for (s = 0; strchr(chars, str[s]); s++);
- for (e = strlen(str); e > 0 && strchr(chars, str[e - 1]); e--);
+ if (str[0]) {
+ /* now strip from the beginning */
+ for (n = 0; str[n] && strchr(chars, str[n]); n++);
- str[e] = '\0';
- return xs_collapse(str, 0, s);
+ if (n)
+ str = xs_collapse(str, 0, n);
+ }
+
+ return str;
}