Skip to main content

normalize_payload

Function normalize_payload 

Source
pub fn normalize_payload(s: &str) -> String
Expand description

Normalizes text by collapsing repeated whitespace and trimming edges.

This function performs the following transformations:

  • Trims leading and trailing whitespace
  • Collapses multiple consecutive whitespace characters (spaces, tabs, newlines) into single spaces
  • Preserves Unicode characters (including emojis)
  • Handles all Unicode whitespace as defined by char::is_whitespace()

§Arguments

  • s - The input string to normalize

§Returns

A new String with whitespace normalized. Returns empty string if input is whitespace-only.

§Examples

use ingest::normalize_payload;

// Collapse multiple spaces
let result = normalize_payload("  Hello   world  ");
assert_eq!(result, "Hello world");

// Handle newlines and tabs
let result = normalize_payload("Line1\n\n\t\tLine2");
assert_eq!(result, "Line1 Line2");

// Preserve Unicode
let result = normalize_payload("  Hello 👋  world  ");
assert_eq!(result, "Hello 👋 world");

// Empty result for whitespace-only input
let result = normalize_payload("   \n\t   ");
assert_eq!(result, "");

§Performance

  • Time complexity: O(n) where n is the length of the input string
  • Space complexity: O(n) for the output string
  • Pre-allocates capacity equal to input length to minimize reallocations

§Use Cases

  • Preparing text for fingerprinting (ensures whitespace differences don’t affect matching)
  • Normalizing user input for storage
  • Cleaning scraped web content