1
0
This commit is contained in:
dtb 2022-10-16 18:44:20 -04:00
parent 4d794c2c6f
commit 5e8b703e67

View File

@ -23,17 +23,10 @@ static char *program_name = "pscat";
/* Test string containing { c, '\0' } without iteration. /* Test string containing { c, '\0' } without iteration.
* Theoretically saves a little bit of time compared to strcmp(3). */ * Theoretically saves a little bit of time compared to strcmp(3). */
int #define SCMPFLAT(a, b) (*(a) != '\0' && *((a)+1) == '\0' && *(a) == (b))
scmpflat(char *s, int c){
return
s[0] != '\0'
&& s[1] == '\0'
&& s[0] == c
;
}
/* Verifies arguments to pscat are sensible. */ /* Verifies arguments to pscat are sensible. */
int static int
check_arg(char **argv){ check_arg(char **argv){
enum { enum {
UNINITIALIZED = 0, UNINITIALIZED = 0,
@ -45,16 +38,16 @@ check_arg(char **argv){
for(s = UNINITIALIZED, terms = 0; *argv != NULL; ++argv) for(s = UNINITIALIZED, terms = 0; *argv != NULL; ++argv)
switch(s){ switch(s){
case UNINITIALIZED: case NORMAL: case UNINITIALIZED: case NORMAL:
if(scmpflat(*argv, L_PAREN)) if(SCMPFLAT(*argv, L_PAREN))
s = INLPAREN; s = INLPAREN;
else else
return 0; /* syntax error */ return 0; /* syntax error */
break; break;
default: /* >= INLPAREN */ default: /* >= INLPAREN */
if(scmpflat(*argv, R_PAREN)){ if(SCMPFLAT(*argv, R_PAREN)){
--s; --s;
terms += s == NORMAL; terms += s == NORMAL;
}else if(scmpflat(*argv, L_PAREN)) /* ineligant */ }else if(SCMPFLAT(*argv, L_PAREN)) /* ineligant */
++s; ++s;
break; break;
} }
@ -89,9 +82,9 @@ int main(int argc, char *argv[]){
/* branching here potentially saves a comparison. /* branching here potentially saves a comparison.
* this seems like the most optimal way to do this, * this seems like the most optimal way to do this,
* maybe it isn't, i don't care too much */ * maybe it isn't, i don't care too much */
if(scmpflat(*argv, L_PAREN)) if(SCMPFLAT(*argv, L_PAREN))
++p; ++p;
else if(scmpflat(*argv, R_PAREN)) else if(SCMPFLAT(*argv, R_PAREN))
--p; --p;
} }
/* *argv -> the corresponding R_PAREN. turn it into NULL to /* *argv -> the corresponding R_PAREN. turn it into NULL to