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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use anyhow::Result;
// MQ2-UNI: UTF-8 safe encoding with ASCII escape sequences
// FIXED: No more collision with UTF-8 continuation bytes!
pub const MQ2_UNI_DICT_ID: &str = "mq2-uni-v2-utf8safe";
const ESC: u8 = b'~'; // The marqant sigil for escape sequences
/// ASCII-safe token mappings using escape sequences
fn get_token_map() -> Vec<(&'static [u8], &'static [u8])> {
vec![
// Common markdown patterns -> ASCII escape codes
(b"\n\n", b"~PP"), // Paragraph break
(b" ", b"~SP"), // Double space
(b"\n- ", b"~LI"), // List item
(b"## ", b"~H2"), // Header 2
(b"# ", b"~H1"), // Header 1
(b"```\n", b"~CB"), // Code block start
(b"```", b"~CE"), // Code block end
(b"{\n", b"~OB"), // Open brace newline
(b"}\n", b"~CL"), // Close brace newline
(b"[\n", b"~OS"), // Open square newline
(b"\n]", b"~CS"), // Close square
(b": ", b"~CO"), // Colon space
(b", ", b"~CM"), // Comma space
(b" ", b"~IN"), // Indent (4 spaces)
(b"\n\n\n", b"~TB"), // Triple break
]
}
/// Skip to the next UTF-8 character boundary
fn skip_utf8_char(bytes: &[u8], i: usize) -> usize {
if i >= bytes.len() {
return i;
}
let b = bytes[i];
if b < 0x80 {
return i + 1; // ASCII - single byte
}
// Multi-byte UTF-8 sequence
let len = if b & 0b1111_0000 == 0b1111_0000 {
4
}
// 4-byte char
else if b & 0b1110_0000 == 0b1110_0000 {
3
}
// 3-byte char
else if b & 0b1100_0000 == 0b1100_0000 {
2
}
// 2-byte char
else {
1
}; // Continuation byte or invalid - skip conservatively
(i + len).min(bytes.len())
}
pub fn mq2_uni_encode(input: &[u8]) -> Result<Vec<u8>> {
let token_map = get_token_map();
let mut out = Vec::with_capacity(input.len());
let mut i = 0;
while i < input.len() {
// Try to match patterns (only in ASCII range)
if input[i] < 0x80 {
let mut matched = false;
for (pattern, token) in &token_map {
if i + pattern.len() <= input.len() && &input[i..i + pattern.len()] == *pattern {
out.extend_from_slice(token);
i += pattern.len();
matched = true;
break;
}
}
if matched {
continue;
}
}
// Handle UTF-8 properly - copy entire character
let next_i = skip_utf8_char(input, i);
out.extend_from_slice(&input[i..next_i]);
i = next_i;
}
Ok(out)
}
pub fn mq2_uni_decode(input: &[u8]) -> Result<Vec<u8>> {
let token_map = get_token_map();
let mut out = Vec::with_capacity(input.len() * 2);
let mut i = 0;
while i < input.len() {
// Check for escape sequences
if i + 2 < input.len() && input[i] == ESC {
let mut decoded = false;
// Try 3-byte tokens first (~XX)
if i + 3 <= input.len() {
let token = &input[i..i + 3];
for (pattern, tok) in &token_map {
if *tok == token {
out.extend_from_slice(pattern);
i += 3;
decoded = true;
break;
}
}
}
if decoded {
continue;
}
}
// Not a token - copy UTF-8 character
let next_i = skip_utf8_char(input, i);
out.extend_from_slice(&input[i..next_i]);
i = next_i;
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_emoji_preservation() {
let test_cases = vec![
"Hello π World! π",
"Rust π¦ is awesome! π",
"ππ’π‘π₯° emotions",
"Complex: δ½ ε₯½δΈη π― Ω
Ψ±ΨΨ¨Ψ§ π",
];
for original in test_cases {
let bytes = original.as_bytes();
let encoded = mq2_uni_encode(bytes).unwrap();
let decoded = mq2_uni_decode(&encoded).unwrap();
assert_eq!(
bytes,
decoded.as_slice(),
"Failed to preserve: {}",
original
);
// Verify we can reconstruct the string
let reconstructed = String::from_utf8(decoded).unwrap();
assert_eq!(original, reconstructed);
}
}
#[test]
fn test_markdown_patterns() {
let markdown = "# Title\n\n## Subtitle\n\n- Item 1\n- Item 2";
let bytes = markdown.as_bytes();
let encoded = mq2_uni_encode(bytes).unwrap();
let decoded = mq2_uni_decode(&encoded).unwrap();
assert_eq!(bytes, decoded.as_slice());
// Note: ASCII escapes may be longer than originals for short strings
// What matters is correctness, not size for small test cases
}
#[test]
fn test_utf8_boundaries() {
// Test that we never split multi-byte sequences
let text = "UTF-8: β¬Β£Β₯ Emoji: π¨βπ©βπ§βπ¦ Chinese: δΈζ";
let bytes = text.as_bytes();
let encoded = mq2_uni_encode(bytes).unwrap();
let decoded = mq2_uni_decode(&encoded).unwrap();
assert_eq!(bytes, decoded.as_slice());
}
}