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 630a454 commit 5f1ef8f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,16 +784,20 @@ 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->p += i;
pl->l -= i;

return 0;
}

Expand All @@ -807,13 +811,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 5f1ef8f

Please sign in to comment.