blob: 714a7cc4c3782541b4554009e988d91ba89aa377 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include "wtf8.h"
/*
* Check if first two bits of the character are "10", meaning it's a UTF-8
* continuation character
*/
int is_utf8_cont(unsigned char c) {
return (c & 0xC0) == 0x80;
}
/*
* Print each octet of a string of characters as lowercase hex followed by a
* trailing space, ending with a newline
*/
void print_octets(char *s) {
unsigned char c;
/*
* Iterate through the string, printing each octet, ending with a newline
*/
while ((c = *s++))
printf("%c%02x", (is_utf8_cont(c) ? '-' : ' '), c);
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 octet that
* terminates it, ending with a newline
*/
void print_characters(char *s) {
/*
* We need a short counter to find how long each character is
*/
unsigned char c;
/*
* Iterate through the string
*/
while (*s) {
/*
* Print blanks and increment a counter until we find how long this
* character is
*/
for (c = 1; is_utf8_cont(s[c]) && c <= UCHAR_MAX; c++)
printf(" ");
/*
* Print two spaces, and then the full character
*/
printf(" ");
while (c--)
putchar(*s++);
}
/*
* 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);
}
|