Minor cleanup.

This commit is contained in:
Simon Forman 2023-02-01 18:35:11 -08:00
parent 6b87e46e00
commit 66cbbac1cc
1 changed files with 47 additions and 37 deletions

View File

@ -5,22 +5,17 @@
const char *BLANKS = " \t";
const char *TEXT = "hi there fr [[] ie]nd] [] 23 ";
/* 01234567890123456789
^ ^
*/
const char *TEXT = " 23 [dup *] i hi there fr [[] ie]nd] [] 23 ";
char *
trim_leading_blanks(char *str)
{
size_t offset = strspn(str, BLANKS);
if (offset == strlen(str)) {
/* All blanks. */
return NULL;
}
return str + offset;
return (offset == strlen(str)) ? NULL : (str + offset);
}
int
main(void)
{
@ -28,10 +23,27 @@ main(void)
ptrdiff_t diff;
text = trim_leading_blanks((char *)TEXT);
if (NULL == text) {
/* All blanks. */
return 1;
}
rest = strpbrk(text, " []");
/*
rest now points to a space or '[' or ']' after a term,
-or- it is NULL if the rest of the string is a single term
with no spaces nor brackets.
*/
while (NULL != rest) {
/* How many chars have we got? */
diff = rest - text;
/*
diff can be zero when there is more than one space in
a sequence in the input string. This won't happen on
the first iteration but it can on later iterations.
*/
if (diff) {
/* Allocate space and copy out the substring. */
@ -45,9 +57,7 @@ main(void)
printf("%c\n", rest[0]);
}
/* */
text = trim_leading_blanks(++rest);
/*printf(">>>%s\n\n", text);*/
/* calling strpbrk on NULL caused segfault! */
rest = (NULL != text) ? strpbrk(text, " []") : text;