Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: fix pl trim methods and add tests #1226

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]")) {
maximilianfridrich marked this conversation as resolved.
Show resolved Hide resolved
++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;
}

Expand All @@ -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;
Expand Down
77 changes: 77 additions & 0 deletions test/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading