Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions packages/apis/src/padding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
/// Pads a string to a specified byte size with null bytes.
///
/// # Arguments
///
/// * `str` - The input string to pad.
/// * `padded_bytes_size` - The target size in bytes after padding.
///
/// # Returns
///
/// A Vec<u8> containing the padded bytes.
///
/// # Performance Notes
///
/// This function is optimized for large strings by pre-allocating the exact
/// capacity needed and avoiding unnecessary memory operations.
pub fn pad_string(str: &str, padded_bytes_size: usize) -> Vec<u8> {
let mut padded_bytes = str.as_bytes().to_vec();
padded_bytes.append(&mut vec![0; padded_bytes_size - padded_bytes.len()]);
let str_bytes = str.as_bytes();
let str_len = str_bytes.len();

if padded_bytes_size <= str_len {
// No padding needed or string is already larger
return str_bytes.to_vec();
}

// Pre-allocate exact capacity to avoid reallocations
let mut padded_bytes = Vec::with_capacity(padded_bytes_size);

// Copy original string bytes
padded_bytes.extend_from_slice(str_bytes);

// Add padding zeros efficiently
let padding_size = padded_bytes_size - str_len;
padded_bytes.resize(padded_bytes_size, 0);

padded_bytes
}