-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrytype.myr
128 lines (118 loc) · 2.82 KB
/
crytype.myr
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
use regex
use std
use "misc"
pkg fltr =
const crytype : (line : byte[:] -> byte[:])
;;
type state = struct
mangle : union
`Init
`Count uint
`Swap char
`Mangle
;;
word : bool
;;
const step = {state : state#
const odds = [24, 16, 12, 6, 3, 2, 1]
match state.mangle
| `Init: state.mangle = `Count 0
| `Count n: state.mangle = std.rand(0, odds[n]) == 0 ? `Mangle : `Count (n + 1)
| _:
;;
-> state.mangle
}
const blank = {state, sb, c
if std.rand(0, 2) == 0
std.sbputs(sb, [",,", ",,,", ",, ,"][std.rand(0, 3)])
state.mangle = `Init
;;
std.sbputc(sb, c)
state.word = false
}
const firstchar = {state, sb, c
if std.rand(0, 4) == 0
std.sbputs(sb, [";", ",,", ",,,", ", ,,"][std.rand(0, 4)])
state.mangle = `Init
;;
std.sbputc(sb, c)
state.word = true
}
const mangle = {state, sb, c, line, off
match c
| '\'': std.sbputs(sb, [";", ";", ",", ",,", ",,,"][std.rand(0, 5)])
| '.': std.sbputs(sb, [";", ",,", ",,", ",,,"][std.rand(0, 4)])
| '?': std.sbputs(sb, ["?,", "?,,"][std.rand(0, 2)])
| _:
match std.rand(0, off < line.len && std.isblank(std.decode(line[off:])) ? 5 : 9)
| 0: std.sbputc(sb, std.isupper(c) ? std.tolower(c) : std.toupper(c))
| 1:
std.sbputc(sb, c)
std.sbputc(sb, c)
| 2:
std.sbputc(sb, c)
std.sbputc(sb, c)
| 3:
std.sbputs(sb, [";", ",", ",,", "/"][std.rand(0, 4)])
std.sbputc(sb, c)
| 4:
std.sbputs(sb, [";", ".", ",", ",,", "/"][std.rand(0, 5)])
std.sbputc(sb, c)
| 5: /* drop this char */
| 6:
| 7: std.sbputs(sb, [";", ".", ",", ",,", "/"][std.rand(0, 5)])
| 8:
std.sbputc(sb, std.decode(line[off:]))
state.mangle = `Swap c
-> void
| _: std.assert(false, "unreachable\n")
;;
;;
state.mangle = `Init
}
const dont = {state, sb, c
/* always mangle apostrophes */
match c
| '\'':
std.sbputs(sb, [";", ";", ",", ",,", ",,,"][std.rand(0, 5)])
state.mangle = `Init
| _: std.sbputc(sb, c)
;;
state.word = !std.isblank(c)
}
const crytypesubs = [
"so sorry", "So sorry", "SO SORRY",
"i'm so sorry", "I'm so sorry", "I'M SO SORRY",
"i'm so so sorry", "I'm so so sorry", "I'M SO SO SORRY",
][:]
/* this whole file is a disaster tbh */
const crytype = {line
if re.crytype == Zptr
re.crytype = std.try(regex.compile( \
"(sorry)|(Sorry)|(SORRY)|" \
"(notice|warning)|(Notice|Warning)|(NOTICE|WARNING)|" \
"(error|oops)|(Error|Oops)|(ERROR|OOPS)"))
;;
var l = regex.suball(re.crytype, line, crytypesubs)
var sb = std.mksb()
var state = [.mangle = `Init]
/* TODO: iterate by graphemes instead of chars */
for (c, off) : std.bycharoff(l)
match step(&state)
| `Swap p:
std.sbputc(sb, p)
state.mangle = `Init
| `Mangle:
if std.isblank(c)
blank(&state, sb, c)
elif !state.word
firstchar(&state, sb, c)
else
mangle(&state, sb, c, l, off)
;;
| _: dont(&state, sb, c)
;;
;;
std.slfree(l)
-> std.sbfin(sb)
}