Skip to content

Instantly share code, notes, and snippets.

@benwills
Created December 30, 2014 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwills/22b707b30e620764220b to your computer and use it in GitHub Desktop.
Save benwills/22b707b30e620764220b to your computer and use it in GitHub Desktop.
Create a Lookup Table for ascii Conversions
#include <stdio.h>
#include <string.h>
//
// Create entries for a boolean lookup table to be used for character conversions.
//
// 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 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , '-', '.', 0 ,
// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0 , 0 , 0 , 0 , 0 , 0 ,
// 0 , 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
// 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0 , 0 , 0 , 0 , 0 ,
// 0 , 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
// 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0 , 0 , 0 , 0 , 0 ,
//
// Copy that to an array.
// static const uint8_t cnv_char_host[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 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , '-', '.', 0 ,
// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0 , 0 , 0 , 0 , 0 , 0 ,
// 0 , 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
// 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0 , 0 , 0 , 0 , 0 ,
// 0 , 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
// 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0 , 0 , 0 , 0 , 0 ,
// };
//
// Call by:
// if ( cnv_char_host[str[i]] ) { ... }
//
// Why?
// A lookup table that converts your data is even faster than isalpha()
// isalpha : 0.582884
// cnv_char_host : 0.300070
// With that table, I'll have already tested and converted two characters just as
// isalpha() is finishing figuring out if it's a letter.
//
// 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 listfrm[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
char listto[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789-.";
int len = strlen(listfrm);
int max = 128;
int fnd = 0;
int splt = 16; // 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 ( listfrm[j] == i ) {
printf("'%c', ", listto[j]);
fnd = 1;
break;
}
}
if (!fnd)
printf(" 0 , ");
if ( (i % splt) == (splt-1) )
printf("\n");
}
printf("\n");
return 1;
}
@benwills
Copy link
Author

If you just want to create a lookup table for boolean checks:
https://gist.github.com/benwills/d514f2d9b5846c147e71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment