From f59d91f1544cd4fd6142290dffd968560505e448 Mon Sep 17 00:00:00 2001 From: Maximilian Fridrich Date: Thu, 5 Dec 2024 14:50:08 +0100 Subject: [PATCH] fmt/pl.c fix pl_trim, pl_ltrim, and pl_rtrim --- src/fmt/pl.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 47057236f..106631f70 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -784,16 +784,19 @@ const char *pl_strstr(const struct pl *pl, const char *str) */ int pl_ltrim(struct pl *pl) { - if (!pl_isset(pl)) + if (!pl) return EINVAL; - while (!re_regex(pl->p, 1, "[ \t\r\n]")) { - ++pl->p; - --pl->l; - if (!pl->l) - return EINVAL; + if (!pl_isset(pl)) + return 0; + + size_t i = 0; + while (i < pl->l && isspace((unsigned char)pl->p[i])) { + ++i; } + pl_advance(pl, i); + return 0; } @@ -807,13 +810,14 @@ int pl_ltrim(struct pl *pl) */ int pl_rtrim(struct pl *pl) { - if (!pl_isset(pl)) + if (!pl) return EINVAL; - while (!re_regex(pl->p + pl->l - 1, 1, "[ \t\r\n]")) { + if (!pl_isset(pl)) + return 0; + + while (pl->l > 0 && isspace((unsigned char)pl->p[pl->l - 1])) { --pl->l; - if (!pl->l) - return EINVAL; } return 0;