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.
This commit is contained in:
Joe 2026-01-31 11:53:44 -06:00
parent 9dd2fd159e
commit d3d914e660

View file

@ -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);
}
}