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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#if 0
${CC:-gcc} -s -O2 -std=c99 -Wall -o ${SPREED_INSTALL_DIR:-.}/spreed src/spreed.c
exit
#endif
/* TODO
* contractions don't work
* - result of trying to separate a+b
*/
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#define CLR_FG L"\033[31m"
#define CLR_RESET L"\033[0m"
uint8_t get_optimal_recognition_point(size_t len) {
static uint8_t orps[9] = {0,0,0,1,1,2,2,2,2};
if (len >= 10) return 3;
return orps[len-1];
}
int main(int argc, char *argv[])
{
wchar_t word[48];
wchar_t wc;
size_t res;
int c;
char chararr[4] = {0};
uint8_t u, uu;
size_t ichar = 0, iword = 0;
uint8_t b_word_break = 0;
memset(word, 0, 48 * sizeof(wchar_t));
memset(chararr, 0, sizeof(chararr));
setlocale(LC_ALL, "");
fwide(stdout, 1);
fwprintf(stdout, L" V\n");
do {
b_word_break = 0;
c = getchar();
if (c == EOF) {
b_word_break = iword > 0;
goto END_OF_WORD;
}
chararr[ichar++] = (char)c;
res = mbrtowc(&wc, chararr, 4, NULL);
switch(res) {
case 0:
// null char
if (iword == 0 && ichar == 1) {
ichar = 0;
memset(chararr, 0, 4 * sizeof(char));
continue;
} else {
// fwprintf(stderr, L"cowabunga: %X %X, %lX\n", ichar, c, wc);
b_word_break = 1;
goto END_OF_WORD;
}
case (size_t)-2:
// incomplete wchar_t, keep reading bytes
continue;
case (size_t)-1:
if (ichar == 4) {
// emojis return (size_t)-1 until we have all the bytes
fwprintf(stderr, L"\nmbrtowc encoding error\n");
return 1;
}
continue;
}
ichar = 0;
memset(chararr, 0, 4 * sizeof(char));
// not a printable character, swallow
if (!iswprint(wc) && iword == 0) {
continue;
}
if (iswspace(wc)) {
if (iword == 0) {
continue;
}
b_word_break = 1;
}
if (iword > 0) {
if (iswpunct(word[iword-1]) != iswpunct(wc)) {
b_word_break = 1;
}
}
END_OF_WORD:
ichar = 0;
if (b_word_break != 0 || iword == 37) {
if (iword == 0) {
fwprintf(stderr, L"\n0 length word\n");
return 2;
}
ichar = get_optimal_recognition_point(iword);
uu = 0;
putwchar('\r');
for (u = 0; u < 48; ++u) {
if (u < 10-ichar) {
putwchar(L' ');
continue;
}
if (uu < iword) {
if (u == 10) {
fwprintf(stdout, L"%ls", CLR_FG);
}
putwchar(word[uu++]);
if (u == 10) {
fwprintf(stdout, L"%ls", CLR_RESET);
}
continue;
}
putwchar(L' ');
}
putwchar('\n');
iword = 0;
memset(word, 0, 48 * sizeof(wchar_t));
}
ichar = 0;
if (!iswspace(wc)) {
word[iword++] = wc;
}
} while (c != EOF);
putwchar('\n');
return 0;
}
|