Created
August 21, 2020 16:02
-
-
Save tallpeak/4baf79adcf68e96d4d055c33ef122dc4 to your computer and use it in GitHub Desktop.
Find words like "coffee" (#C0FFEE) which can be written as hexadecimal number.
This file contains 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
// rewrite of Haskell to C | |
// from https://github.com/mitsuji/c0ffee | |
// Find words like "coffee" (#C0FFEE) which can be written as hexadecimal number. | |
// I made a couple of careless pointer errors | |
// Proof I should use Haskell more, and C less? | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define true 1 | |
#define false 0 | |
int hexlike(unsigned char c) | |
{ | |
return ( | |
((unsigned)c-'0') < 10 || | |
((unsigned)(c | 32)-'a') < 6 || | |
c == 'I' || | |
c == 'O' || | |
c == 'S' || | |
c == 'Z' || | |
c == 'l' || | |
c == 'o' || | |
c == 'q' || | |
c == 's' | |
); | |
} | |
int line_is_hexlike_word(char *p) | |
{ | |
while(hexlike(*p++)) { | |
if (!*p) return true; | |
} | |
return false; | |
} | |
// WARNING: MUTATES ARG! | |
char* conv(char *line) | |
{ | |
static char *s = "IOSZloqsz"; | |
static char *r = "105210952"; | |
for (char *pl = line; *pl; pl++) | |
{ | |
char *ps = strchr(s, *pl); | |
if (ps) { | |
*pl = r[ps-s]; // POKE the translated char back into source buffer | |
} | |
} | |
return line; | |
} | |
int main() { | |
// buffer overflow still possible, no matter how big you make this! : | |
char buf[65536]; | |
int c; | |
while(gets(buf)) { | |
if (line_is_hexlike_word(buf)) { | |
printf("%-15s", buf); | |
// must be separate call, due to mutation of buf by conv(): | |
printf(" %s\n", conv(buf)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment