yakra / DataProcessing

Data Processing Scripts and Programs for Travel Mapping Project
0 stars 0 forks source link

strcspn -> strchr when only 1 delimiter #192

Open yakra opened 3 years ago

yakra commented 3 years ago

strcspn takes up time, however little, looping thru an array and checking whether within bounds. Why not skip the looping when we won't need an array?

yakra commented 3 years ago

Won't work. If delimiter (space in this example) is not found, d is set to 0. And then we try to dereference it. Having to check whether d is valid (and then potentially do whatever) outweighs the benefit of losing the bounds checking.

yakra commented 3 years ago

Wait, how about something like this?

// label
char* c = strchr(line, ' ');
if (c)  do *c = 0; while (*++c == ' ');
else    /* not important in this example */ ;
label = line;