Layer Comps to Files: a cut part leaves out the parts after it (#13940)

When a part of the file name was cut, the bytes left over (less than one
character of it) could still hold a later, lower-priority part: a cut
comp name could lose its last character while a one-letter comment
stayed. Now a cut prefix leaves out the name and comment, and a cut
name leaves out the comment.
This commit is contained in:
RickiNano committed 2026-11-25 07:24:40 +01:00
1 parent 5bc0153391
commit 3fc42fdab2
2 files changed
+18 -6

No files matched your search

+8 -6
View File
@@ -513,15 +513,17 @@ const MAX_FILE_NAME_BYTES: usize = 255;
/// `<prefix>_<index>_<name>[_<comment>].<ext>`, at most [`MAX_FILE_NAME_BYTES`] long. A name that
/// doesn't fit loses the end of its comment first (the whole comment, with its `_`, when none of it
/// fits), then of the comp name, then of the prefix; the index and extension always stay. Each
/// cut falls between characters.
/// fits), then of the comp name, then of the prefix, and a part that is cut leaves out the parts
/// after it; the index and extension always stay. Each cut falls between characters.
fn comp_file_name(prefix: &str, index: usize, name: &str, comment: &str, ext: &str) -> String {
let index = format!("_{index:04}_");
let room = MAX_FILE_NAME_BYTES.saturating_sub(index.len() + 1 + ext.len());
let prefix = clip_bytes(prefix, room);
let room = room - prefix.len();
let name = clip_bytes(name, room);
let comment = clip_bytes(comment, (room - name.len()).saturating_sub(1));
// 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 kept_name = clip_bytes(name, room);
let room = if kept_name.len() < name.trim_end().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}")
}
@@ -78,3 +78,13 @@ fn a_long_comment_is_shortened_and_the_export_goes_on() {
let _ = std::fs::remove_dir_all(&dir);
}
}
/// When the comp name has to be cut, a short comment doesn't take the bytes the cut leaves free:
/// the comment goes before the name does.
#[test]
fn a_cut_comp_name_leaves_out_the_comment() {
let prefix = "p".repeat(243);
let name = comp_file_name(&prefix, 0, "界", "x", "png");
assert!(name.len() <= MAX_FILE_NAME_BYTES, "{} bytes", name.len());
assert!(name.ends_with("_0000_.png"), "{name}");
}