Skip to content

Commit

Permalink
Trim leading and trailing whitespaces from strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed Apr 12, 2014
1 parent a509d69 commit 25a64a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 25 additions & 9 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,33 @@ verify_status_code (char *str)
}

char *
trim_str (char *str)
ltrim (char *s)
{
char *p;
if (!str)
return NULL;
if (!*str)
return str;
for (p = str + strlen (str) - 1; (p >= str) && isspace (*p); --p);
p[1] = '\0';
char *begin = s;

return str;
while (isspace (*begin))
++begin;
memmove (s, begin, strlen (begin) + 1);

return s;
}

char *
rtrim (char *s)
{
char *end = s + strlen (s);

while ((end != s) && isspace (*(end - 1)))
--end;
*end = '\0';

return s;
}

char *
trim_str (char *str)
{
return rtrim (ltrim (str));
}

char *
Expand Down
2 changes: 2 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ char *filesize_str (unsigned long long log_size);
char *float_to_str (float num);
char *int_to_str (int d);
char *left_pad_str (const char *s, int indent);
char *ltrim (char *s);
char *replace_str (const char *str, const char *old, const char *new);
char *rtrim (char *s);
char *secs_to_str (int secs);
char *substring (const char *str, int begin, int len);
char *trim_str (char *str);
Expand Down

0 comments on commit 25a64a6

Please sign in to comment.