mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-18 16:14:57 +00:00
xlsx: use shared SpreadsheetML string codec in reader and writer
This commit is contained in:
1 parent
abcfc19153
commit
faef1a06c8
4 files changed
+55
-66
No files matched your search
Generated
+24
@@ -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",
|
||||
|
||||
@@ -44,6 +44,8 @@ craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0
|
||||
# The native JSON workbook (`.json`, `.scjson`): read, written, and kept newer macros written back.
|
||||
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.
|
||||
# SpreadsheetML string encoding/decoding; workbook model mapping stays in gridcraft-xlsx.
|
||||
craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
|
||||
craft-xml = { 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" }
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ craft-offcrypto = { workspace = true }
|
||||
craft-opc = { workspace = true }
|
||||
craft-xls = { workspace = true }
|
||||
craft-xml = { workspace = true }
|
||||
craft-xlsx = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
|
||||
+28
-66
@@ -359,8 +359,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.
|
||||
use craft_xml::is_xml_char;
|
||||
// XML escaping and SpreadsheetML strings are shared; phonetic/model mapping stays here.
|
||||
#[cfg(test)]
|
||||
use craft_xml::push_esc;
|
||||
pub use craft_xml::{esc, esc_attr};
|
||||
@@ -402,70 +401,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
|
||||
}
|
||||
// Existing call sites and regression coverage use the production shared codec.
|
||||
pub use craft_xlsx::{decode_xstring, encode_xstring};
|
||||
|
||||
/// `<t>` element with `xml:space="preserve"` when needed.
|
||||
pub fn t_el(s: &str) -> String {
|
||||
@@ -778,3 +715,28 @@ mod shared_escape_parity {
|
||||
assert_eq!(decode_xstring(&encode_xstring("_x000D_\r\0😀")), "_x000D_\r\0😀");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shared_xstring_parity {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn xstring_preserves_controls_unicode_and_literal_escapes() {
|
||||
// XML-invalid scalars, C1 controls and literal escapes survive save/reopen.
|
||||
for c in (0..=0xffff).filter_map(char::from_u32) {
|
||||
let text = format!("left{c}right_x0041_");
|
||||
assert_eq!(decode_xstring(&encode_xstring(&text)), text);
|
||||
}
|
||||
for (encoded, decoded) in [
|
||||
("_x005F_x000D_", "_x000D_"),
|
||||
("_xD83D__xDE00_", "😀"),
|
||||
("_xD83D__x0041_", "_xD83D_A"),
|
||||
("_xD83D__x005F_xDE00_", "_xD83D__xDE00_"),
|
||||
("_X0041_", "_X0041_"),
|
||||
("_xZZZZ_", "_xZZZZ_"),
|
||||
] {
|
||||
assert_eq!(decode_xstring(encoded), decoded);
|
||||
}
|
||||
assert_eq!(encode_xstring("\r\n\t\0"), "_x000D_\n\t_x0000_");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user