Encode trailing space as _x20_ in XML ids to keep round trip

Found while testing crates/doc/src/xml_id.rs:80 (strip_unique), triggered by from_object_name at :32

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-21 15:20:11 +07:00
committed by bflatastic
co-authored by Claude Haiku 5.5
parent 7c8e772b46
commit 781d68a971
+9 -1
View File
@@ -31,8 +31,12 @@ pub fn to_object_name(id: &str) -> String {
/// since a bare `_` would read back as a space.
pub fn from_object_name(name: &str) -> String {
let mut out = String::with_capacity(name.len().saturating_mul(2));
let last = name.chars().count().saturating_sub(1);
for (i, c) in name.chars().enumerate() {
if c == ' ' {
if c == ' ' && i == last {
// A trailing bare `_` could read back as the end of a uniqueness suffix.
out.push_str("_x20_");
} else if c == ' ' {
out.push('_');
} else if c == '_' {
out.push_str("_x5F_");
@@ -115,6 +119,10 @@ mod tests {
let id = from_object_name(name);
assert_eq!(to_object_name(&id), name, "name {name:?} -> id {id:?}");
}
let name = "Scan 12345678901234567890 ";
let id = from_object_name(name);
assert!(id.ends_with("_x20_"), "{id:?}");
assert_eq!(to_object_name(&id), name);
assert_eq!(from_object_name("-abc"), "_x2D_abc");
assert_eq!(from_object_name(".5 mm"), "_x2E_5_mm");
assert_eq!(from_object_name("²x"), "_xB2_x");