| 304 | // --------------------------------------------------------------------------- |
| 305 | |
| 306 | fn encode_string(table: &SymbolTable, input: &[u8]) -> Vec<u8> { |
| 307 | let mut out = Vec::with_capacity(input.len()); |
| 308 | let mut pos = 0; |
| 309 | |
| 310 | while pos < input.len() { |
| 311 | // Greedy: try to match the longest symbol at current position. |
| 312 | let mut matched = false; |
| 313 | for (idx, sym) in table.symbols.iter().enumerate() { |
| 314 | if input[pos..].starts_with(sym) { |
| 315 | out.push(idx as u8); |
| 316 | pos += sym.len(); |
| 317 | matched = true; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if !matched { |
| 323 | // No symbol matches — emit escape + literal byte. |
| 324 | out.push(ESCAPE); |
| 325 | out.push(input[pos]); |
| 326 | pos += 1; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | out |
| 331 | } |
| 332 | |
| 333 | fn decode_string(symbols: &[Vec<u8>], encoded: &[u8]) -> Result<Vec<u8>, CodecError> { |
| 334 | let mut out = Vec::with_capacity(encoded.len() * 2); |