mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-09 20:09:57 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc1bc1f474 |
@@ -104,19 +104,9 @@ pub(super) fn find(s: &mut Session, p: &Json) -> Result<Json> {
|
|||||||
Ok(json!({"cell": c.a1(), "sheet": si, "value": t, "count": hits.len()}))
|
Ok(json!({"cell": c.a1(), "sheet": si, "value": t, "count": hits.len()}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Most characters a cell holds (Excel's limit).
|
|
||||||
const REPLACE_CELL_MAX_CHARS: usize = 32_767;
|
|
||||||
/// Longest text one replacement may give a cell, in bytes: the character limit, each up to four
|
|
||||||
/// bytes. Every occurrence copies the replacement text, so a short cell full of matches and a
|
|
||||||
/// long replacement would otherwise expand to gigabytes. The character limit is checked on what
|
|
||||||
/// fits in this.
|
|
||||||
const REPLACE_CELL_MAX_BYTES: usize = REPLACE_CELL_MAX_CHARS * 4;
|
|
||||||
/// Most bytes of replaced text one Replace All builds across all its cells.
|
|
||||||
const REPLACE_TOTAL_MAX_BYTES: usize = 256 * 1024 * 1024;
|
|
||||||
|
|
||||||
pub(super) fn replace(s: &mut Session, p: &Json) -> Result<Json> {
|
pub(super) fn replace(s: &mut Session, p: &Json) -> Result<Json> {
|
||||||
let what = str_param(p, "what").unwrap_or("").to_string();
|
let what = str_param(p, "what").unwrap_or("").to_string();
|
||||||
let with = str_param(p, "with").unwrap_or("");
|
let with = str_param(p, "with").unwrap_or("").to_string();
|
||||||
if what.is_empty() {
|
if what.is_empty() {
|
||||||
return Err(bad("edit.replace", "missing `what`"));
|
return Err(bad("edit.replace", "missing `what`"));
|
||||||
}
|
}
|
||||||
@@ -129,7 +119,6 @@ pub(super) fn replace(s: &mut Session, p: &Json) -> Result<Json> {
|
|||||||
let n = edit(s, |cx| {
|
let n = edit(s, |cx| {
|
||||||
let sheets: Vec<usize> = if workbook { (0..cx.wb.sheets.len()).collect() } else { vec![cx.wb.active_sheet()] };
|
let sheets: Vec<usize> = if workbook { (0..cx.wb.sheets.len()).collect() } else { vec![cx.wb.active_sheet()] };
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
let mut total = 0usize;
|
|
||||||
for si in sheets {
|
for si in sheets {
|
||||||
let cells: Vec<(CellRef, String)> =
|
let cells: Vec<(CellRef, String)> =
|
||||||
cx.wb.sheet(si).map(|sh| sh.cells.iter().map(|(c, _)| (c, sh.input_text(c))).collect()).unwrap_or_default();
|
cx.wb.sheet(si).map(|sh| sh.cells.iter().map(|(c, _)| (c, sh.input_text(c))).collect()).unwrap_or_default();
|
||||||
@@ -141,20 +130,12 @@ pub(super) fn replace(s: &mut Session, p: &Json) -> Result<Json> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let new = if whole {
|
let new = if whole {
|
||||||
(with.len() <= REPLACE_CELL_MAX_BYTES).then(|| with.to_string())
|
with.clone()
|
||||||
} else if case {
|
} else if case {
|
||||||
replace_cs_within(&text, &what, with, REPLACE_CELL_MAX_BYTES)
|
text.replace(&what, &with)
|
||||||
} else {
|
} else {
|
||||||
replace_ci_within(&text, &what, with, REPLACE_CELL_MAX_BYTES)
|
replace_ci(&text, &what, &with)
|
||||||
};
|
};
|
||||||
let new = new.filter(|n| n.chars().count() <= REPLACE_CELL_MAX_CHARS);
|
|
||||||
let Some(new) = new else {
|
|
||||||
return Err(EngineError::Other(format!("The replacement makes the text of {} too long.", c.a1())));
|
|
||||||
};
|
|
||||||
total = total.saturating_add(new.len());
|
|
||||||
if total > REPLACE_TOTAL_MAX_BYTES {
|
|
||||||
return Err(EngineError::Other("The replacements make too much text. Replace in a smaller range.".into()));
|
|
||||||
}
|
|
||||||
// A Python cell's text is its code: the replacement edits the code and the cell
|
// A Python cell's text is its code: the replacement edits the code and the cell
|
||||||
// stays a Python cell. Code that would no longer fit in a PY formula is left as it was.
|
// stays a Python cell. Code that would no longer fit in a PY formula is left as it was.
|
||||||
let old_py = cx.wb.sheet(si).and_then(|sh| sh.cell(c)).and_then(|x| x.py.clone());
|
let old_py = cx.wb.sheet(si).and_then(|sh| sh.cell(c)).and_then(|x| x.py.clone());
|
||||||
@@ -209,37 +190,7 @@ fn folds_in_place(s: &str) -> bool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(super) fn replace_ci(text: &str, what: &str, with: &str) -> String {
|
pub(super) fn replace_ci(text: &str, what: &str, with: &str) -> String {
|
||||||
replace_ci_within(text, what, with, usize::MAX).unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `text` with every `what` replaced by `with`, or `None` once the result passes `limit` bytes
|
|
||||||
/// (checked before each copy, so nothing larger is allocated).
|
|
||||||
pub(super) fn replace_cs_within(text: &str, what: &str, with: &str, limit: usize) -> Option<String> {
|
|
||||||
if what.is_empty() {
|
|
||||||
return (text.len() <= limit).then(|| text.to_string());
|
|
||||||
}
|
|
||||||
let mut out = String::new();
|
|
||||||
let mut i = 0;
|
|
||||||
for (pos, m) in text.match_indices(what) {
|
|
||||||
if out.len() + (pos - i) + with.len() > limit {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
out.push_str(text.get(i..pos).unwrap_or(""));
|
|
||||||
out.push_str(with);
|
|
||||||
i = pos + m.len();
|
|
||||||
}
|
|
||||||
let rest = text.get(i..).unwrap_or("");
|
|
||||||
if out.len() + rest.len() > limit {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
out.push_str(rest);
|
|
||||||
Some(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Case-insensitive [`replace_cs_within`].
|
|
||||||
pub(super) fn replace_ci_within(text: &str, what: &str, with: &str, limit: usize) -> Option<String> {
|
|
||||||
let lower = text.to_lowercase();
|
let lower = text.to_lowercase();
|
||||||
let w = what.to_lowercase();
|
let w = what.to_lowercase();
|
||||||
// Slicing `text` at positions found in `lower` is only sound when folding kept every byte
|
// Slicing `text` at positions found in `lower` is only sound when folding kept every byte
|
||||||
@@ -247,22 +198,15 @@ pub(super) fn replace_ci_within(text: &str, what: &str, with: &str, limit: usize
|
|||||||
// boundaries (U+212A KELVIN SIGN shrinks 3→1 bytes, U+0130 grows 2→3), where the tail used
|
// boundaries (U+212A KELVIN SIGN shrinks 3→1 bytes, U+0130 grows 2→3), where the tail used
|
||||||
// to be dropped. Those fall back to the case-sensitive replace, as length changes already do.
|
// to be dropped. Those fall back to the case-sensitive replace, as length changes already do.
|
||||||
if lower.len() != text.len() || w.is_empty() || !folds_in_place(text) || !folds_in_place(what) {
|
if lower.len() != text.len() || w.is_empty() || !folds_in_place(text) || !folds_in_place(what) {
|
||||||
return replace_cs_within(text, what, with, limit);
|
return text.replace(what, with);
|
||||||
}
|
}
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while let Some(pos) = lower.get(i..).and_then(|s| s.find(&w)) {
|
while let Some(pos) = lower.get(i..).and_then(|s| s.find(&w)) {
|
||||||
if out.len() + pos + with.len() > limit {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
out.push_str(text.get(i..i + pos).unwrap_or(""));
|
out.push_str(text.get(i..i + pos).unwrap_or(""));
|
||||||
out.push_str(with);
|
out.push_str(with);
|
||||||
i += pos + w.len();
|
i += pos + w.len();
|
||||||
}
|
}
|
||||||
let rest = text.get(i..).unwrap_or("");
|
out.push_str(text.get(i..).unwrap_or(""));
|
||||||
if out.len() + rest.len() > limit {
|
out
|
||||||
return None;
|
|
||||||
}
|
|
||||||
out.push_str(rest);
|
|
||||||
Some(out)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -353,50 +353,6 @@ fn cut_paste_that_overflows_the_grid_moves_nothing() {
|
|||||||
assert_eq!(get(&mut s, "XFD2")["value"], json!(2.0));
|
assert_eq!(get(&mut s, "XFD2")["value"], json!(2.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn replace_refuses_text_that_would_grow_past_a_cell() {
|
|
||||||
// 10,000 matches × a 1,000-character replacement would make a 10 MB cell (and a few such
|
|
||||||
// cells gigabytes); the replacement is refused before that text is built.
|
|
||||||
let mut s = Session::new();
|
|
||||||
s.new_workbook();
|
|
||||||
let long = "a".repeat(10_000);
|
|
||||||
s.execute("range.setValues", json!({"range": "A1", "values": [[long.clone()], ["abc"]]})).unwrap();
|
|
||||||
for case in [false, true] {
|
|
||||||
let err = s.execute("edit.replace", json!({"what": "a", "with": "x".repeat(1_000), "matchCase": case})).expect_err("too long");
|
|
||||||
assert!(err.to_string().contains("too long"), "{err}");
|
|
||||||
assert_eq!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["value"], json!(long));
|
|
||||||
assert_eq!(s.execute("cell.get", json!({"cell": "A2"})).unwrap()["value"], json!("abc"));
|
|
||||||
}
|
|
||||||
// A replacement that stays within a cell's limit still happens.
|
|
||||||
s.execute("edit.replace", json!({"what": "b", "with": "x".repeat(100)})).unwrap();
|
|
||||||
assert_eq!(s.execute("cell.get", json!({"cell": "A2"})).unwrap()["value"], json!(format!("a{}c", "x".repeat(100))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn replace_refuses_a_result_past_the_character_limit_though_it_fits_the_byte_limit() {
|
|
||||||
let mut s = Session::new();
|
|
||||||
s.new_workbook();
|
|
||||||
s.execute("range.setValues", json!({"range": "A1", "values": [["z"]]})).unwrap();
|
|
||||||
for whole in [false, true] {
|
|
||||||
let err = s.execute("edit.replace", json!({"what": "z", "with": "x".repeat(32_768), "wholeCell": whole})).expect_err("32,768 characters");
|
|
||||||
assert!(err.to_string().contains("too long"), "{err}");
|
|
||||||
assert_eq!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["value"], json!("z"));
|
|
||||||
}
|
|
||||||
s.execute("edit.replace", json!({"what": "z", "with": "x".repeat(32_767)})).unwrap();
|
|
||||||
assert_eq!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["value"], json!("x".repeat(32_767)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bounded_replace_stops_at_its_limit() {
|
|
||||||
use super::find::{replace_ci_within, replace_cs_within};
|
|
||||||
assert_eq!(replace_cs_within("abab", "a", "xy", 6).as_deref(), Some("xybxyb"));
|
|
||||||
assert_eq!(replace_cs_within("abab", "a", "xy", 5), None);
|
|
||||||
assert_eq!(replace_ci_within("AbAb", "a", "xy", 6).as_deref(), Some("xybxyb"));
|
|
||||||
assert_eq!(replace_ci_within("AbAb", "a", "xy", 5), None);
|
|
||||||
assert_eq!(replace_cs_within("abc", "z", "", 3).as_deref(), Some("abc"));
|
|
||||||
assert_eq!(replace_cs_within("abc", "z", "", 2), None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn replace_case_insensitive() {
|
fn replace_case_insensitive() {
|
||||||
assert_eq!(replace_ci("Apple apple APPLE", "apple", "pear"), "pear pear pear");
|
assert_eq!(replace_ci("Apple apple APPLE", "apple", "pear"), "pear pear pear");
|
||||||
|
|||||||
@@ -53,25 +53,41 @@ fn concat(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
|
|||||||
check_len(out)
|
check_len(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The error `v.to_text()` would give, found without building any text.
|
||||||
|
fn text_error(v: &Value) -> Option<CellError> {
|
||||||
|
match v {
|
||||||
|
Value::Error(e) => Some(*e),
|
||||||
|
Value::Array(a) => a.data.first().and_then(text_error),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn textjoin(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
|
fn textjoin(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
|
||||||
let delims: Vec<String> = as_array(&arg(a, 0)?.value).iter().map(|v| v.to_text()).collect::<R<_>>()?;
|
// Errors are found first, in argument order, so they win as before; text is then built one
|
||||||
|
// piece at a time into the capped output, so many references to one long text (or a long
|
||||||
|
// delimiter list) stop at the cap instead of first copying every piece.
|
||||||
|
let delims = as_array(&arg(a, 0)?.value);
|
||||||
|
if let Some(e) = delims.iter().find_map(text_error) {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
let ignore = boolean(a, 1)?;
|
let ignore = boolean(a, 1)?;
|
||||||
let mut items = Vec::new();
|
let items: Vec<_> = a.iter().skip(2).map(|x| as_array(&x.value)).collect();
|
||||||
for x in a.iter().skip(2) {
|
if let Some(e) = items.iter().flat_map(|x| x.iter()).find_map(text_error) {
|
||||||
for v in as_array(&x.value).iter() {
|
return Err(e);
|
||||||
let s = v.to_text()?;
|
|
||||||
if ignore && s.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
items.push(s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
for (i, s) in items.iter().enumerate() {
|
let mut joined = 0usize;
|
||||||
if i > 0 && !delims.is_empty() {
|
for v in items.iter().flat_map(|x| x.iter()) {
|
||||||
push_capped(&mut out, &delims[(i - 1) % delims.len()])?;
|
let s = v.to_text()?;
|
||||||
|
if ignore && s.is_empty() {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
push_capped(&mut out, s)?;
|
if joined > 0 && !delims.data.is_empty() {
|
||||||
|
let d = delims.data.get((joined - 1) % delims.data.len()).map(Value::to_text).transpose()?.unwrap_or_default();
|
||||||
|
push_capped(&mut out, &d)?;
|
||||||
|
}
|
||||||
|
push_capped(&mut out, &s)?;
|
||||||
|
joined += 1;
|
||||||
}
|
}
|
||||||
check_len(out)
|
check_len(out)
|
||||||
}
|
}
|
||||||
@@ -1131,6 +1147,22 @@ mod tests {
|
|||||||
assert_eq!(rows_of(&ev("CONCATENATE", vec![av(row_t(&["a", "b"])), t("x")])), vec![vec![tv("ax"), tv("bx")]]);
|
assert_eq!(rows_of(&ev("CONCATENATE", vec![av(row_t(&["a", "b"])), t("x")])), vec![vec![tv("ax"), tv("bx")]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Security: TEXTJOIN builds its result piece by piece and stops at the text cap, rather
|
||||||
|
/// than first copying every input (100,000 references to one 32,767-character text were
|
||||||
|
/// 3 GB of copies). Errors among the inputs still win, in argument order.
|
||||||
|
#[test]
|
||||||
|
fn textjoin_stops_at_the_text_cap() {
|
||||||
|
use gridcraft_core::Array;
|
||||||
|
let long = tv(&"x".repeat(32_767));
|
||||||
|
let many = || av(Array::filled(100_000, 1, long.clone()).into());
|
||||||
|
is_err(ev("TEXTJOIN", vec![t(","), b(true), many()]), CellError::Value);
|
||||||
|
is_err(ev("TEXTJOIN", vec![av(Array::filled(1, 100_000, long.clone()).into()), b(false), t("a"), t("b")]), CellError::Value);
|
||||||
|
is_err(ev("TEXTJOIN", vec![t(","), b(true), many(), e(CellError::Div0)]), CellError::Div0);
|
||||||
|
is_err(ev("TEXTJOIN", vec![av(arr(vec![vec![tv(","), Value::Error(CellError::NA)]])), b(true), t("a")]), CellError::NA);
|
||||||
|
// Delimiters cycle and are converted only where used.
|
||||||
|
is_text(ev("TEXTJOIN", vec![av(arr(vec![vec![nv(1.0), tv(";")]])), b(false), t("a"), t("b"), t("c"), t("d")]), "a1b;c1d");
|
||||||
|
}
|
||||||
|
|
||||||
fn row_t(xs: &[&str]) -> Value {
|
fn row_t(xs: &[&str]) -> Value {
|
||||||
arr(vec![xs.iter().map(|x| tv(x)).collect()])
|
arr(vec![xs.iter().map(|x| tv(x)).collect()])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user