commit 8985f9026c19c1a92a4678ce934eee8d0041cf5e
parent a960ec7c8e4119db5e9f45c7be97dd36d4b9181b
Author: Antoine A <>
Date: Wed, 24 Jun 2026 12:33:00 +0200
ebics: clean code
Diffstat:
1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/libeufin-ebics/src/xml_sign.rs b/libeufin-ebics/src/xml_sign.rs
@@ -33,22 +33,31 @@ use thiserror::Error;
use crate::xml::{self, Xml, XmlAccess as _};
fn escape<'a>(
- text: &'a str,
+ mut text: &'a str,
replacements: &[(char, &str)],
out: &mut impl Write,
) -> std::io::Result<()> {
- // Find the first character that needs escaping
- let Some(first_pos) = text.find(|c| replacements.iter().any(|(r, _)| *r == c)) else {
- return write!(out, "{text}"); // No escaping needed
- };
+ // Find the next character that needs escaping
+ while let Some(pos) = text.find(|c| replacements.iter().any(|&(r, _)| r == c)) {
+ // Write the safe chunk of text before the escaped character
+ if pos > 0 {
+ out.write_all(text[..pos].as_bytes())?;
+ }
- write!(out, "{}", &text[..first_pos])?;
+ // Get the character that triggered the match
+ let ch = text[pos..].chars().next().unwrap();
- for ch in text[first_pos..].chars() {
- match replacements.iter().find(|(r, _)| *r == ch) {
- Some((_, escaped)) => write!(out, "{escaped}")?,
- None => write!(out, "{ch}")?,
- }
+ // Find and write its replacement
+ let escaped = replacements.iter().find(|&&(r, _)| r == ch).unwrap().1;
+ out.write_all(escaped.as_bytes())?;
+
+ // Advance the text slice past the character we just escaped
+ text = &text[pos + ch.len_utf8()..];
+ }
+
+ // Write any remaining unescaped tail
+ if !text.is_empty() {
+ out.write_all(text.as_bytes())?;
}
Ok(())