Skip to main content

expand

Function expand 

Source
pub fn expand(src: &[u8]) -> Vec<u8> ⓘ
Expand description

Expands a byte slice by inserting ‘X’ characters between consecutive duplicate characters.

This is a convenience wrapper around expand_with that uses the standard ‘X’ filler character (FillX). It processes the input in pairs (digrams), inserting an ‘X’ between any two consecutive identical characters. This is the most commonly used expansion function for classical ciphers like Playfair.

This function is equivalent to calling expand_with::<FillX>(src).

§Algorithm

See expand_with for detailed algorithm description. This function simply delegates to expand_with with the FillX type parameter.

§Arguments

  • src - A byte slice to be expanded

§Returns

A new Vec<u8> with ‘X’ characters inserted between consecutive duplicates

§Examples

Basic usage with simple duplicates:

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");

More complex examples:

use old_crypto_rs::helpers::expand;

// 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");

// Mixed duplicates
let input = b"PJRJJJJJJS";
let result = expand(input);
assert_eq!(result, b"PJRJJXJXJXJXJS");

§Use Cases

This function is primarily used in:

  • Playfair cipher preprocessing
  • Two-square cipher preprocessing
  • Four-square cipher preprocessing
  • Any bi-grammatic classical cipher requiring duplicate handling

§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

  • expand_with - Generic version allowing custom filler characters
  • FillX - The ‘X’ filler character policy used by this function
  • FillQ - Alternative ‘Q’ filler character policy
  • FillWith - Trait defining filler character policies