Last active
August 29, 2015 14:12
-
-
Save benwills/d514f2d9b5846c147e71 to your computer and use it in GitHub Desktop.
Quickly Create a Binary Lookup Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| // | |
| // Create entries for a boolean lookup table. | |
| // | |
| // Example output from list[], below: | |
| // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | |
| // 0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1, | |
| // 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1, | |
| // 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0, | |
| // | |
| // Copy that to an array. | |
| // static const uint8_t lu_bool_url[128] = { | |
| // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | |
| // 0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1, | |
| // 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1, | |
| // 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0, | |
| // }; | |
| // | |
| // Call by: | |
| // if ( lu_bool_url[str[i]] ) { ... } | |
| // | |
| // Why? | |
| // A lookup table is even faster than isalpha() | |
| // isalpha : 0.489370 | |
| // lu_bool_alpha : 0.331028 | |
| // | |
| // Especially for more complex checks, e.g. valid url characters, speed improves | |
| // significantly over other methods. | |
| // | |
| // And at 128 bytes per table, it works out well. | |
| // | |
| int main(){ | |
| char list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;="; | |
| int len = strlen(list); | |
| int max = 128; | |
| int fnd = 0; | |
| int splt = 32; // How many chars before a line break | |
| int i, j; | |
| for ( i=0; i<max; ++i ) { | |
| fnd = 0; | |
| for ( j=0; j<len; ++j ) { | |
| if ( list[j] == i ) { | |
| printf("1,"); | |
| fnd = 1; | |
| break; | |
| } | |
| } | |
| if (!fnd) | |
| printf("0,"); | |
| if ( (i % splt) == (splt-1) ) | |
| printf("\n"); | |
| } | |
| printf("\n"); | |
| return 1; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to create a lookup table for converting characters:
https://gist.github.com/benwills/22b707b30e620764220b