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 8, 2024
1 parent 6e592db commit c9e1a14
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,16 +784,23 @@ 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)
i = pl->l;

pl->p += i;
pl->l -= i;

return 0;
}

Expand All @@ -807,13 +814,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 c9e1a14

Please sign in to comment.