Skip to content

Instantly share code, notes, and snippets.

@yshui
Last active August 29, 2015 14:00
Show Gist options
  • Save yshui/11348787 to your computer and use it in GitHub Desktop.
Save yshui/11348787 to your computer and use it in GitHub Desktop.
UTF8 to Unicode, not-so-IOCCC style
#define s "U+%04llX\n"
#define G(x) goto x
unsigned long long cp;
main(a,_,c){if(c)G(y);
if(~_&0x40)G(e);if(_&(1<<a))G(x);
return printf(s,cp+((_&~(-1<<a))<<6*(6-a)));
x:if((c=getchar())&0x40)G(e);
cp=(cp<<6)+(c&0x3f);return main(a-1,_,0);
y:if(!~(_=getchar()))return 0;
_&0x80?main(6,_,cp=0):printf(s,_);return main(a,_,c);
e:exit(puts("Broken UTF8"));}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment