-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchar.c
57 lines (53 loc) · 1.27 KB
/
char.c
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
#include <stdio.h>
#include <inttypes.h>
#include "types.h"
void print_codepoint(int64_t);
void print_char (int64_t v) {
int64_t codepoint = v >> char_shift;
printf("#\\");
switch (codepoint) {
case 0:
printf("nul"); break;
case 8:
printf("backspace"); break;
case 9:
printf("tab"); break;
case 10:
printf("newline"); break;
case 11:
printf("vtab"); break;
case 12:
printf("page"); break;
case 13:
printf("return"); break;
case 32:
printf("space"); break;
case 127:
printf("rubout"); break;
default:
print_codepoint(v);
}
}
void print_codepoint(int64_t v) {
int64_t codepoint = v >> char_shift;
// Print using UTF-8 encoding of codepoint
// https://en.wikipedia.org/wiki/UTF-8
if (codepoint < 128) {
printf("%c", (char) codepoint);
} else if (codepoint < 2048) {
printf("%c%c",
(char)(codepoint >> 6) | 192,
((char)codepoint & 63) | 128);
} else if (codepoint < 65536) {
printf("%c%c%c",
(char)(codepoint >> 12) | 224,
((char)(codepoint >> 6) & 63) | 128,
((char)codepoint & 63) | 128);
} else {
printf("%c%c%c%c",
(char)(codepoint >> 18) | 240,
((char)(codepoint >> 12) & 63) | 128,
((char)(codepoint >> 6) & 63) | 128,
((char)codepoint & 63) | 128);
}
}