diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-15 19:54:14 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-15 19:54:14 +1300 |
commit | e69fe29958589f90c537737111d1138d52bc3b53 (patch) | |
tree | 64ae5e4b8832ee715c374e16f377a6b355c7d054 | |
parent | Add some comments (diff) | |
download | wtf8-e69fe29958589f90c537737111d1138d52bc3b53.tar.gz wtf8-e69fe29958589f90c537737111d1138d52bc3b53.zip |
Broke up into nice functions, commented, tidied
-rw-r--r-- | wtf8.c | 78 | ||||
-rw-r--r-- | wtf8.h | 2 |
2 files changed, 69 insertions, 11 deletions
@@ -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); } @@ -2,3 +2,5 @@ #include <stdlib.h> int is_utf8_cont(char); +void print_octets(char *); +void print_characters(char *); |