int main(int argc, char *argv[]){ char *src = "abcdefg"; char *set = "cde"; printf("%s\n", mystrpbrk(src, set)); return 0; }
bool read_expr_impl(const char ** const ptr, char word[256], const char *delims) { bool in_string = false, // "xxx xxx xx" in_escape = false; // \n int in_quote = 0; // 'x' int level = 0; char *word_ptr = word; /* Is there a word left to get? */ // Skip whitespace at the start *ptr = skip_whitespace (*ptr); if (is_end_of_code_line (*ptr) || **ptr == ')') { if (word) *word = '\0'; return false; } while (**ptr != '\0' && !((mystrpbrk (*ptr, delims) == *ptr || is_end_of_code_line (*ptr)) && !in_escape && !in_string && in_quote == 0 && level == 0)) { if (!in_escape) { switch( **ptr ) { case '"': if (in_quote == 0) in_string = !in_string; break; case '\'': if (!in_string && in_quote == 0) in_quote = 3; break; case '\\': in_escape = true; break; case '(': if (!in_string && in_quote == 0) level++; break; case ')': if (!in_string && in_quote == 0) level--; if (level < 0) { //(*ptr)--; //12/29/2010 stp not sure goto finish_read_expr; } break; default: /* If this char wasn't ', redo the inside */ if (in_quote == 1) { *ptr -= 2; word_ptr -= 2; } } if (in_quote) in_quote--; } else { in_escape = false; } if (word_ptr - word >= 254) { show_fatal_error ("Expression is too long - must be 255 chars or less"); if (word) strcpy (word, "0"); return true; } if (word) *word_ptr++ = **ptr; (*ptr)++; } finish_read_expr: // Remove whitespace at the end if (word) { while (word_ptr > word && isspace((unsigned char) word_ptr[-1])) *(--word_ptr) = '\0'; *word_ptr = '\0'; } /* input is either the delimiter or null */ if (is_end_of_code_line (*ptr)) return true; return true; }
char *next_expr (const char *ptr, const char *delims) { bool in_string = false, // "xxx xxx xx" in_escape = false; // \n int in_quote = 0; // 'x' /* Is there a word left to get? */ // Skip whitespace at the start ptr = skip_whitespace (ptr); if (is_end_of_code_line (ptr) || *ptr == ')') return (char *) ptr; if (strlen(delims) == 1) { while (*ptr != '\0' && !((strchr (ptr, delims[0]) == ptr || is_end_of_code_line (ptr)) && !in_escape && !in_string && in_quote == 0)) { if (!in_escape) { switch( *ptr ) { case '"': if (in_quote == 0) in_string = !in_string; break; case '\'': if (!in_string && in_quote == 0) in_quote = 3; break; case '\\': in_escape = true; break; default: /* If this char wasn't ', redo the inside */ if (in_quote == 1) { ptr -= 2; } } if (in_quote) in_quote--; } else { in_escape = false; } ptr++; } } else { while (*ptr != '\0' && !((mystrpbrk (ptr, delims) == ptr || is_end_of_code_line (ptr)) && !in_escape && !in_string && in_quote == 0)) { if (!in_escape) { switch( *ptr ) { case '"': if (in_quote == 0) in_string = !in_string; break; case '\'': if (!in_string && in_quote == 0) in_quote = 3; break; case '\\': in_escape = true; break; default: /* If this char wasn't ', redo the inside */ if (in_quote == 1) { ptr -= 2; } } if (in_quote) in_quote--; } else { in_escape = false; } ptr++; } } return (char *) ptr; }