Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C problem

Name: stallman 2011-11-06 9:12

Sup prog
i'm using C
i have a problem

i have a dvi file where each line is a command and its parameters
i have to output a file and write what the commands do

i have been able to parse the lines into strings and get the first string which is the command itself

my goal is to compare that first string (the command) with strings contained in a string array (which are all the commands that exist in a dvi file)

that way i can return the corresponding function which will do the calculation using the parameters from the line

problem is that there is a command named set_char_i where i can go from 0 to 127

my command string array looks like that:
char *commandes[40]={"pre", "xxx1", "set", "set_char_i", "set_rule", "put", "put_rule", "down", "fnt_num", "w0", "w", "x0", "x", "y0", "y", "z0", "z", "fnt_def", "post", "post_post", "eop", "bop", "push", "pop"};

how can i make it so that "set_char_i" has i between 0 and 127?

Name: Anonymous 2011-11-06 14:55

>>17
There is no such thing as a "string array" you fucking dumb jew. Psstt... a string in C is an array of characters that get terminated with '\0'.

Name: Anonymous 2011-11-06 15:29

>>19

but...but...but..this way I can strcmp the buffer with a finite set of strings, determined at compile time, with the efficiency of a single strcmp!

for example, if the strings were: hi me how are you

I could do:


#define NOTHING -1
#define HI 2
#define ME 4
#define HOW 7
#define ARE 11
#define YOU 16

char *start = buffer;
char *position;
int state = 0;
int token = NOTHING;

for(;;) {
  switch(*position) {
    case 'h': switch(state) { case 0: state = 1; break;
                              default: state = 0; break;
                            }
    case 'i': switch(state) { case 1: state = 2; break;
                              default: state = 0; break;
                            }

    case 'm': switch(state) { case 0: state = 3; break;
                              default: state = 0; break;
                            }
    case 'e': switch(state) { case 3: state = 4; break;
                              default: state = 0; break;
                            }

    case 'h': switch(state) { case 0: state = 5; break;
                              default: state = 0; break;
                            }
    case 'o': switch(state) { case 5: state = 6; break;
                              default: state = 0; break;
                            }
    case 'w': switch(state) { case 6: state = 7; break;
                              default: state = 0; break;
                            }

    case 'a': switch(state) { case 8: state = 9; break;
                              default: state = 0; break;
                            }
    case 'r': switch(state) { case 9: state = 10; break;
                              default: state = 0; break;
                            }
    case 'e': switch(state) { case 10: state = 11; break;
                              default: state = 0; break;
                            }

    case 'y': switch(state) { case 0: state = 12; break;
                              default: state = 0; break;
                            }
    case 'o': switch(state) { case 13: state = 14; break;
                              default: state = 0; break;
                            }
    case 'u': switch(state) { case 15: state = 16; break;
                              default: state = 0; break;
                            }

    case '\0': state = NOTHING; break;
  }
  switch(state) {
    case NOTHING: return NULL; break;
    case HI:
    case ME:
    case HOW:
    case ARE:
    case YOU:
      return state;
      break;
  }
}


And this then reduces to:


#define NOTHING -1
#define HI 2
#define ME 4
#define HOW 7
#define ARE 11
#define YOU 16

char *start = buffer;
char *position;
int state = 0;
int token = NOTHING;

for(;;) {
  switch(*position) {
    case 'h': switch(state) { case 0: state = 1; break;
                              default: state = 0; break;
                            }
    case 'i': switch(state) { case 1: state = 2; break;
                              default: state = 0; break;
                            }

    case 'm': switch(state) { case 0: state = 3; break;
                              default: state = 0; break;
                            }
    case 'e': switch(state) { case 3: state = 4; break;
                              case 10: state = 11; break;
                              default: state = 0; break;
                            }

    case 'o': switch(state) { case 1: state = 6; break;
                              case 13: state = 14; break;
                              default: state = 0; break;
                            }
    case 'w': switch(state) { case 6: state = 7; break;
                              default: state = 0; break;
                            }

    case 'a': switch(state) { case 8: state = 9; break;
                              default: state = 0; break;
                            }
    case 'r': switch(state) { case 9: state = 10; break;
                              default: state = 0; break;
                            }

    case 'y': switch(state) { case 0: state = 12; break;
                              default: state = 0; break;
                            }
    case 'u': switch(state) { case 15: state = 16; break;
                              default: state = 0; break;
                            }

    case '\0': state = NOTHING; break;
  }
  switch(state) {
    case NOTHING: return NULL; break;
    case HI:
    case ME:
    case HOW:
    case ARE:
    case YOU:
      return state;
      break;
  }
}

Name: Anonymous 2011-11-06 15:34

>>19
Using strcmp() would make it into linear time you fucking idiot.

Name: Anonymous 2011-11-06 15:36

>>21

>>19 is a fucking stupid shit. Most, if not all C compilers can optimize a switch/case statement to a jump table. This in turn makes it constant time. However using strcmp() will result in linear time.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List