From d3d914e66026ba2c13f5185f16c09052b9f5b333 Mon Sep 17 00:00:00 2001 From: Joe Date: Sat, 31 Jan 2026 11:53:44 -0600 Subject: [PATCH] Enhance path encoding logic and add comprehensive tests - Updated the `encode_path_name` function to handle cases of leading, trailing, and entirely empty strings, ensuring correct encoding of spaces. - Introduced new tests for single space, multiple spaces, and empty string scenarios to validate encoding and decoding behavior. --- src/path_encoding.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/path_encoding.rs b/src/path_encoding.rs index 1c8d9871..291da1fa 100644 --- a/src/path_encoding.rs +++ b/src/path_encoding.rs @@ -48,7 +48,14 @@ const SPACE_ENCODING: &str = "%SPACE%"; /// with their encoded representations like `%DOT%`, `%LT%`, `%GT%`, etc. /// Literal `%` is escaped as `%%`. Leading and trailing spaces are encoded as `%SPACE%`. pub fn encode_path_name(name: &str) -> String { - // Count leading and trailing spaces first (before any modifications) + let trimmed = name.trim_matches(' '); + + // Handle the case where the string is entirely spaces (or empty) + if trimmed.is_empty() { + return SPACE_ENCODING.repeat(name.len()); + } + + // Count leading and trailing spaces let leading_spaces = name.len() - name.trim_start_matches(' ').len(); let trailing_spaces = name.len() - name.trim_end_matches(' ').len(); @@ -308,4 +315,28 @@ mod tests { let decoded = decode_path_name(&encoded); assert_eq!(decoded, original); } + + #[test] + fn test_single_space() { + let input = " "; + let encoded = encode_path_name(input); + assert_eq!(encoded, "%SPACE%"); + assert_eq!(decode_path_name(&encoded), input); + } + + #[test] + fn test_multiple_spaces_only() { + let input = " "; + let encoded = encode_path_name(input); + assert_eq!(encoded, "%SPACE%%SPACE%%SPACE%"); + assert_eq!(decode_path_name(&encoded), input); + } + + #[test] + fn test_empty_string() { + let input = ""; + let encoded = encode_path_name(input); + assert_eq!(encoded, ""); + assert_eq!(decode_path_name(&encoded), input); + } }