Skip to content

Commit

Permalink
fmt/pl.c fix pl_trim, pl_ltrim, and pl_rtrim
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianfridrich committed Dec 9, 2024
1 parent 630a454 commit 2b7d161
Showing 1 changed file with 17 additions and 10 deletions.
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]")) {
++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

0 comments on commit 2b7d161

Please sign in to comment.