MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / encode_string

Function encode_string

nodedb-codec/src/fsst.rs:306–331  ·  view source on GitHub ↗
(table: &SymbolTable, input: &[u8])

Source from the content-addressed store, hash-verified

304// ---------------------------------------------------------------------------
305
306fn 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
333fn decode_string(symbols: &[Vec<u8>], encoded: &[u8]) -> Result<Vec<u8>, CodecError> {
334 let mut out = Vec::with_capacity(encoded.len() * 2);

Callers 1

encodeFunction · 0.85

Calls 3

lenMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected