Add a new function char* gettok_np(char** s)

which finds a token, while scanning over (), like
they were white spaces. It also proceeds with s while
still finding (), (which gettok_noparens does not do).
This commit is contained in:
Holger Vogt 2022-07-06 18:43:48 +02:00
parent 975ea03308
commit 86dedbd1f5
2 changed files with 58 additions and 1 deletions

View File

@ -55,6 +55,7 @@ size_t get_substring_matches(size_t n_char_pattern, const char *p_pattern,
char *gettok(char **s);
char *gettok_char(char **s, char p, bool inc_p, bool nested);
char *gettok_instance(char **);
char *gettok_np(char **);
bool has_escape_or_quote(size_t n, const char *str);
bool is_arith_char(char c);
bool isquote(char ch);

View File

@ -30,6 +30,8 @@ static inline const char *next_substr(
const size_t msb_factor, const size_t h_pattern, size_t *p_h_string);
static bool can_overlap(size_t n_char_pattern, const char * const p_pattern);
static void findtok_np(char** p_str, char** p_token, char** p_token_end);
/* This function returns true if the string s begins with the
* string p and false otherwise. */
@ -454,7 +456,7 @@ nexttok(const char *s)
* nexttok skips over whitespaces and the next token in s
* returns NULL if there is nothing left to skip.
* It replaces constructs like txfree(gettok(&actstring)) by
* actstring = nexttok(actstring). This is derived from the gettok_noparens version.
* actstring = nexttok(actstring). This is derived from the gettok_np version.
* It acts like gettok, except that it treats parens and commas like
* whitespace.
*-------------------------------------------------------------------------*/
@ -606,6 +608,60 @@ char *gettok_noparens(char **s)
} /* end of function gettok_noparens */
/* findtok_np() does the string scanning for gettok_np() but
* does not allocate a token. It skips over all white spaces, ',', '('and ')' */
static
void findtok_np(char** p_str, char** p_token, char** p_token_end)
{
char* str = *p_str;
while (isspace_c(*str) || *str == ',' || *str == '(' || *str == ')')
str++;
if (!*str) {
*p_str = str;
*p_token = (char*)NULL;
return;
}
*p_token = str; /* Token starts after whitespace */
{
char c;
while ((c = *str) != '\0' &&
!isspace_c(c) &&
(c != '(') &&
(c != ')') &&
(c != ',')
) {
str++;
}
}
*p_token_end = str;
while (isspace_c(*str) || *str == ',' || *str == '(' || *str == ')')
str++;
*p_str = str;
} /* end of function findtok_noparen */
/*-------------------------------------------------------------------------*
* gettok_np acts like gettok, except that it treats parens and commas like
* whitespace. That is, it stops parsing and returns when it finds one of
* those chars. It then moves s beyond all white spaces, ',', '('and ')'.
*-------------------------------------------------------------------------*/
char* gettok_np(char** s)
{
char* token, * token_e;
findtok_np(s, &token, &token_e);
if (token == (char*)NULL) {
return (char*)NULL; /* return NULL if we come to end of line */
}
return copy_substring(token, token_e);
} /* end of function gettok_noparens */
/*-------------------------------------------------------------------------*
* gettok_model acts like gettok_noparens, however when it encounters a '{',
* it searches for the corresponding '}' and adds the string to the output