From e69fe29958589f90c537737111d1138d52bc3b53 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 15 Dec 2016 19:54:14 +1300 Subject: Broke up into nice functions, commented, tidied --- wtf8.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- wtf8.h | 2 ++ 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/wtf8.c b/wtf8.c index 4bad603..e4d3f5e 100644 --- a/wtf8.c +++ b/wtf8.c @@ -9,28 +9,84 @@ int is_utf8_cont(char c) { } /* - * Main function + * Print each octet of a string of characters as lowercase hex followed by a + * trailing space, ending with a newline */ -int main(int argc, char **argv) { - char *p; - int c; +void print_octets(char *s) { - if (argc != 2) - exit(EXIT_FAILURE); + /* + * Iterate through the string, printing each octet + */ + while (*s) + printf("%02x ", (unsigned char) *s++); - for (p = argv[1]; *p; p++) - printf("%02x ", (unsigned char) *p); + /* + * End with a newline + */ putchar('\n'); + return; +} + +/* + * Print each of the UTF-8 characters to align with the output of + * print_octets(), with each character in line with the end of the byte that + * terminates it, ending with a newline + */ +void print_characters(char *s) { + + /* + * We need a short counter to find how long each character is + */ + int c; + + /* + * Iterate through the string + */ + while (*s) { - for (p = argv[1]; *p; ) { - for (c = 1; p[c] && is_utf8_cont(p[c]); c++) + /* + * Print blanks and increment a counter until we find how long this + * character is + */ + for (c = 1; s[c] && is_utf8_cont(s[c]); c++) printf(" "); + + /* + * Print a space, then the full character, then another space + */ putchar(' '); for (; c > 0; c--) - putchar(*p++); + putchar(*s++); putchar(' '); } + + /* + * End with a newline + */ putchar('\n'); + return; +} + +/* + * Main function + */ +int main(int argc, char **argv) { + + /* + * Check we have one and only one argument + */ + if (argc != 2) + exit(EXIT_FAILURE); + + /* + * Print the sole argument first as hex octets, then as characters, spaced + * accordingly + */ + print_octets(argv[1]); + print_characters(argv[1]); + /* + * Done! + */ exit(EXIT_SUCCESS); } diff --git a/wtf8.h b/wtf8.h index 64ac8a1..c4241a8 100644 --- a/wtf8.h +++ b/wtf8.h @@ -2,3 +2,5 @@ #include int is_utf8_cont(char); +void print_octets(char *); +void print_characters(char *); -- cgit v1.2.3