From f112c4e13009e5ddfe3cf5c4cbe7f29f832b8553 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 12 Oct 2025 21:51:21 +0200 Subject: solve double delimiters with empty fields --- src/bibiman/citekeys.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/bibiman/citekeys.rs b/src/bibiman/citekeys.rs index 065d57f..9d17403 100644 --- a/src/bibiman/citekeys.rs +++ b/src/bibiman/citekeys.rs @@ -317,13 +317,37 @@ fn build_citekey(entry: &Entry, pattern_fields: &[String], case: Option<&Citekey // process the single slices and add correct delimiter if let Some(field_slice) = split_field.next() { - formatted_str = formatted_str + &format_word(field_slice, char_count); - words_passed += 1; - if word_count == words_passed { - formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); - break; + // Create word slice char by char. We need to loop over chars + // instead of a simple bytes index to also catch chars which + // consist of more than one byte (äöüøæ etc...) + let mut word_slice = String::new(); + let word_chars = field_slice.chars(); + let mut counter = 0; + for c in word_chars { + if let Some(len) = char_count + && counter == len + { + break; + } + // if a word slice contains a special char, skip it + if IGNORED_SPECIAL_CHARS.contains(&c) { + continue; + } + word_slice.push(c); + counter += 1; + } + // Don't count empty slices and don't add delimiter to those + if !word_slice.is_empty() { + formatted_str = formatted_str + &word_slice; + words_passed += 1; + if word_count == words_passed { + formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); + break; + } else { + formatted_str = formatted_str + inner_delimiter.unwrap_or(""); + } } else { - formatted_str = formatted_str + inner_delimiter.unwrap_or("") + continue; } } else { formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); -- cgit v1.2.3