Compare commits

...
Author SHA1 Message Date
Dylan M. Taylor 623c30efa9 xlsx: use shared ST_Xstring encoding and decoding 2026-11-16 19:42:17 -05:00
FredApps abcfc19153 Merge pull request #4364 from storytold/docs/shared-library-guidance
docs: prefer shared Craft libraries for reusable features
2026-11-16 13:51:45 +01:00
4 changed files with 29 additions and 64 deletions
Generated
+24
View File
@@ -932,6 +932,17 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-drawingml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4"
dependencies = [
"craft-xml",
"kurbo",
"log",
"serde",
]
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
@@ -1127,6 +1138,18 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-xlsx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4"
dependencies = [
"craft-drawingml",
"craft-opc",
"craft-sheet",
"craft-xml",
"thiserror 2.0.21",
]
[[package]]
name = "craft-xml"
version = "0.1.0"
@@ -2379,6 +2402,7 @@ dependencies = [
"craft-offcrypto",
"craft-opc",
"craft-xls",
"craft-xlsx",
"craft-xml",
"gridcraft-core",
"gridcraft-formula",
+1
View File
@@ -45,6 +45,7 @@ craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0
craft-gridcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "9a6d894200221fcfedc274870c30394d404f859d", version = "0.1.0" }
# Excel 97-2003 (.xls, BIFF8) workbooks; `crates/xlsx/src/xls.rs` maps them onto the model.
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
# foundation
+1
View File
@@ -14,6 +14,7 @@ workspace = true
test-support = []
[dependencies]
craft-xlsx.workspace = true
gridcraft-core = { workspace = true }
gridcraft-formula = { workspace = true }
gridcraft-math = { workspace = true }
+3 -64
View File
@@ -360,6 +360,7 @@ pub fn parse_streaming(xml: &[u8], split: &[&str], cb: &mut dyn FnMut(El) -> Res
// ---------------------------------------------------------------- writing helpers
// XML 1.0 byte escaping is shared; spreadsheet string and phonetic policy stays here.
#[cfg(test)]
use craft_xml::is_xml_char;
#[cfg(test)]
use craft_xml::push_esc;
@@ -402,70 +403,8 @@ pub fn push_a1(o: &mut String, c: gridcraft_core::CellRef) {
push_u64(o, u64::from(c.row) + 1);
}
/// ST_Xstring encoding for cell text: characters XML can't carry and carriage returns become
/// `_xHHHH_`, and literal `_xHHHH_` sequences are protected with `_x005F_`. The result still needs
/// [`esc`].
pub fn encode_xstring(s: &str) -> String {
let mut o = String::with_capacity(s.len());
let chars: Vec<char> = s.chars().collect();
for (i, &c) in chars.iter().enumerate() {
if c == '_' && looks_like_escape(&chars, i) {
o.push_str("_x005F_");
} else if !is_xml_char(c) || (c.is_control() && !matches!(c, '\t' | '\n')) {
o.push_str(&format!("_x{:04X}_", c as u32 & 0xFFFF));
} else {
o.push(c);
}
}
o
}
fn looks_like_escape(chars: &[char], i: usize) -> bool {
chars.get(i + 1) == Some(&'x') && chars.get(i + 6) == Some(&'_') && (i + 2..i + 6).all(|k| chars.get(k).is_some_and(char::is_ascii_hexdigit))
}
fn escape_unit(chars: &[char], i: usize) -> Option<u32> {
let hex: String = chars.get(i + 2..i + 6)?.iter().collect();
u32::from_str_radix(&hex, 16).ok()
}
/// The character for a high surrogate escape at `i` (value `high`) followed directly by a low surrogate escape.
fn surrogate_pair(chars: &[char], i: usize, high: u32) -> Option<char> {
if !(0xD800..=0xDBFF).contains(&high) || chars.get(i + 7) != Some(&'_') || !looks_like_escape(chars, i + 7) {
return None;
}
let low = escape_unit(chars, i + 7).filter(|l| (0xDC00..=0xDFFF).contains(l))?;
char::from_u32(0x10000 + ((high - 0xD800) << 10) + (low - 0xDC00))
}
/// Inverse of [`encode_xstring`].
pub fn decode_xstring(s: &str) -> String {
if !s.contains("_x") {
return s.to_string();
}
let chars: Vec<char> = s.chars().collect();
let mut o = String::with_capacity(s.len());
let mut i = 0;
while i < chars.len() {
if chars[i] == '_' && looks_like_escape(&chars, i) {
let unit = escape_unit(&chars, i);
if let Some(ch) = unit.and_then(char::from_u32) {
o.push(ch);
i += 7;
continue;
}
// A high surrogate escape followed by a low surrogate escape is one UTF-16 pair.
if let Some(ch) = unit.and_then(|high| surrogate_pair(&chars, i, high)) {
o.push(ch);
i += 14;
continue;
}
}
o.push(chars[i]);
i += 1;
}
o
}
/// Shared ST_Xstring encoding/decoding; XML escaping remains a separate operation.
pub use craft_xlsx::{decode_xstring, encode_xstring};
/// `<t>` element with `xml:space="preserve"` when needed.
pub fn t_el(s: &str) -> String {