From baea3f6ef803d309efe62f6605ef233e7d6facbf Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:28:31 +0100 Subject: [PATCH] Layer Comps to Files: a part that fits keeps its trailing space (#13940) `clip_bytes` trimmed trailing spaces even when it didn't cut anything, so a sanitized prefix such as `A ` (from `A .`) lost its space in a short file name. It now returns a part that fits unchanged and trims only the end a cut leaves. --- crates/engine-vector/src/cmd/comps_cmds.rs | 11 +++++++---- .../comps_cmds/layer_comps_to_files_names_tests.rs | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/engine-vector/src/cmd/comps_cmds.rs b/crates/engine-vector/src/cmd/comps_cmds.rs index 68215a0f6d..c7127c885e 100644 --- a/crates/engine-vector/src/cmd/comps_cmds.rs +++ b/crates/engine-vector/src/cmd/comps_cmds.rs @@ -520,17 +520,20 @@ fn comp_file_name(prefix: &str, index: usize, name: &str, comment: &str, ext: &s let room = MAX_FILE_NAME_BYTES.saturating_sub(index.len() + 1 + ext.len()); // Once a part is cut, the parts after it are left out, even where a few bytes are still free. let kept_prefix = clip_bytes(prefix, room); - let room = if kept_prefix.len() < prefix.trim_end().len() { 0 } else { room - kept_prefix.len() }; + let room = if kept_prefix.len() < prefix.len() { 0 } else { room - kept_prefix.len() }; let kept_name = clip_bytes(name, room); - let room = if kept_name.len() < name.trim_end().len() { 0 } else { room - kept_name.len() }; + let room = if kept_name.len() < name.len() { 0 } else { room - kept_name.len() }; let (prefix, name, comment) = (kept_prefix, kept_name, clip_bytes(comment, room.saturating_sub(1))); let comment = if comment.is_empty() { String::new() } else { format!("_{comment}") }; format!("{prefix}{index}{name}{comment}.{ext}") } -/// The longest start of `text` that fits in `max` bytes without splitting a character, with any -/// spaces the cut leaves at its end trimmed. +/// `text` when it fits in `max` bytes, else its longest start that does without splitting a +/// character, with any spaces the cut leaves at its end trimmed. fn clip_bytes(text: &str, max: usize) -> &str { + if text.len() <= max { + return text; + } let end = text.char_indices().map(|(i, c)| i + c.len_utf8()).take_while(|&end| end <= max).last().unwrap_or(0); text.get(..end).unwrap_or_default().trim_end() } diff --git a/crates/engine-vector/src/cmd/comps_cmds/layer_comps_to_files_names_tests.rs b/crates/engine-vector/src/cmd/comps_cmds/layer_comps_to_files_names_tests.rs index 27e3fe7f74..96f58646e5 100644 --- a/crates/engine-vector/src/cmd/comps_cmds/layer_comps_to_files_names_tests.rs +++ b/crates/engine-vector/src/cmd/comps_cmds/layer_comps_to_files_names_tests.rs @@ -88,3 +88,10 @@ fn a_cut_comp_name_leaves_out_the_comment() { assert!(name.len() <= MAX_FILE_NAME_BYTES, "{} bytes", name.len()); assert!(name.ends_with("_0000_.png"), "{name}"); } + +/// A part that fits stays exactly as sanitized: only a cut trims the spaces it leaves. +#[test] +fn a_part_that_fits_keeps_its_trailing_space() { + let prefix = sanitize("A ."); + assert!(comp_file_name(&prefix, 0, "Name", "x", "png").starts_with(&format!("{prefix}_0000_Name")), "{prefix:?}"); +}