pub fn common_prefix_len(left: &str, right: &str) -> usizeExpand description
How many leading bytes two keys share.
This is what lets a node store a suffix rather than a whole key: entries are sorted, so each one records how much of its predecessor it repeats and then only the rest. Get it wrong and the node encodes to different bytes, which means a different CID, which means a different root — the error does not show up as a lookup that fails, it shows up as a repository nobody else can verify.
The count is in bytes, not characters, and that is the load-bearing detail. Two keys sharing a three-character prefix may share four bytes or nine, and the number a node carries is the one that indexes into the UTF-8. A character count would agree with a byte count on every ASCII key — which is every key upstream’s vectors contain, and every record key atproto permits — and would disagree the first time a tree held a key that was neither.
A returned length can therefore land inside a multi-byte character. That is correct: the tree is keyed on byte strings and the encoding of a node splits on the byte offset this returns.
use didbot_data::mst::common_prefix_len;
assert_eq!(common_prefix_len("abcde", "abc1"), 3);
assert_eq!(common_prefix_len("abc", "abc\0"), 3);
// Two characters shared, four bytes shared.
assert_eq!(common_prefix_len("ααx", "ααy"), 4);