Skip to content

Instantly share code, notes, and snippets.

@theikkila
Created September 21, 2018 12:23
Show Gist options
  • Save theikkila/f22cdd973b68554d040a6dfbc88846e4 to your computer and use it in GitHub Desktop.
Save theikkila/f22cdd973b68554d040a6dfbc88846e4 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
// How does this work:
// https://github.com/nginx/nginx/blob/master/src/http/ngx_http_parse.c#L634
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
static uint32_t usual[] = {
0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x7fff37d6, /* 0111 1111 1111 1111 0011 0111 1101 0110 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
int main(int argc, char const *argv[]) {
uint8_t ch = '';
for (size_t i = 0; i < 126; i++) {
ch = i;
printf("-> [%d] "BYTE_TO_BINARY_PATTERN"\n", ch >> 5, BYTE_TO_BINARY((1U << (ch & 0x1f))));
if (usual[ch >> 5] & (1U << (ch & 0x1f))) {
printf("BREAK: %c\n", ch);
} else {
printf("OK: %c\n", ch);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment