pub fn expand(src: &[u8]) -> Vec<u8> ⓘExpand description
Expands a byte slice by inserting ‘X’ between consecutive duplicate characters.
This function processes the input in pairs (digrams), inserting the byte b'X'
between any two consecutive identical characters. This is commonly used in
classical ciphers like Playfair to ensure all digrams consist of different characters.
§Algorithm
- Copy the source slice into a mutable vector
- Iterate through the vector in steps of 2 (processing digrams)
- If two consecutive characters are identical, insert
b'X'between them - Continue processing, which causes the inserted ‘X’ to become part of the next digram
§Arguments
src- A byte slice to be expanded
§Returns
A new Vec<u8> with ‘X’ characters inserted between consecutive duplicates
§Examples
use old_crypto_rs::helpers::expand;
// Simple duplicate
let input = b"AAA";
let result = expand(input);
assert_eq!(result, b"AXAXA");
// No duplicates
let input = b"ARABESQUE";
let result = expand(input);
assert_eq!(result, b"ARABESQUE");
// Multiple consecutive duplicates
let input = b"AAAA";
let result = expand(input);
assert_eq!(result, b"AXAXAXA");
// Duplicates in different positions
let input = b"LANNONCE";
let result = expand(input);
assert_eq!(result, b"LANXNONCE");§Performance
Time complexity: O(n) where n is the length of the input, though insertions may cause reallocation in worst case scenarios with many consecutive duplicates. Space complexity: O(n + k) where k is the number of ‘X’ characters inserted.
§See Also
insert- The helper function used to insert characters