diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 47057236f..0139614d0 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -784,16 +784,22 @@ 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; } + if (i == pl->l) + pl->l = 0; + else + pl_advance(pl, i); + return 0; } @@ -807,13 +813,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; diff --git a/test/fmt.c b/test/fmt.c index 297346c3f..e6812621d 100644 --- a/test/fmt.c +++ b/test/fmt.c @@ -1185,3 +1185,80 @@ int test_text2pcap(void) mem_deref(mb); return err; } + + +int test_fmt_trim(void) +{ + const struct pl pla = PL(" heisann "); + const struct pl pll = PL("heisann "); + const struct pl plr = PL(" heisann"); + const struct pl plb = PL("heisann"); + + const struct pl pl = PL(" \r \n heisann \n \r "); + const struct pl pl1 = PL("heisann \n \r "); + const struct pl pl2 = PL(" \r \n heisann"); + const struct pl pl3 = PL("heisann"); + + const struct pl pl4 = PL(" \r \n"); + const struct pl pl5 = PL(""); + struct pl pln; + + int err = 0; + + pln = pla; + err = pl_ltrim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pll); + TEST_ERR(err); + + pln = pla; + err = pl_rtrim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &plr); + TEST_ERR(err); + + pln = pla; + err = pl_trim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &plb); + TEST_ERR(err); + + pln = pl1; + err = pl_ltrim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pl1); + TEST_ERR(err); + + pln = pl; + err = pl_rtrim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pl2); + TEST_ERR(err); + + pln = pl; + err = pl_trim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pl3); + TEST_ERR(err); + + pln = pl4; + err = pl_ltrim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pl5); + TEST_ERR(err); + + pln = pl4; + err = pl_rtrim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pl5); + TEST_ERR(err); + + pln = pl4; + err = pl_trim(&pln); + TEST_ERR(err); + err = pl_cmp(&pln, &pl5); + TEST_ERR(err); + +out: + return err; +} diff --git a/test/test.c b/test/test.c index 1ec985d46..cc7594e6c 100644 --- a/test/test.c +++ b/test/test.c @@ -218,6 +218,7 @@ static const struct test tests[] = { TEST(test_tcp), TEST(test_telev), TEST(test_text2pcap), + TEST(test_fmt_trim), #ifdef USE_TLS TEST(test_tls), TEST(test_tls_ec), diff --git a/test/test.h b/test/test.h index eaf278bd4..a5f489b52 100644 --- a/test/test.h +++ b/test/test.h @@ -200,6 +200,7 @@ int test_fmt_str_error(void); int test_fmt_str_itoa(void); int test_fmt_str_wchar(void); int test_fmt_timestamp(void); +int test_fmt_trim(void); int test_fmt_unicode(void); int test_fmt_unicode_decode(void); int test_g711_alaw(void);