mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-03 08:51:30 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7db8b5b746 |
@@ -575,9 +575,20 @@ pub fn series(cx: &mut Ctx, sheet: usize, options: SeriesOptions<'_>) -> Result<
|
||||
match (unit, d) {
|
||||
("month", Some(_)) => (step as i64).checked_mul(i as i64).and_then(|m| add_months_clamped(sys, start, m)).unwrap_or(v),
|
||||
("year", Some(_)) => (step as i64).checked_mul(i as i64 * 12).and_then(|m| add_months_clamped(sys, start, m)).unwrap_or(v),
|
||||
// Whole weeks at a time: a huge step costs no more than a small one.
|
||||
("weekday", Some(_)) => add_weekdays(sys, v, step as i64)
|
||||
.ok_or_else(|| crate::EngineError::Other("The weekday series runs past the supported date range.".into()))?,
|
||||
("weekday", Some(_)) => {
|
||||
let mut n = v;
|
||||
let mut left = step.abs() as i64;
|
||||
while left > 0 {
|
||||
n += step.signum();
|
||||
let Some(day) = datetime_from_serial(sys, n) else {
|
||||
return Err(crate::EngineError::Other("The weekday series runs past the supported date range.".into()));
|
||||
};
|
||||
if day.weekday != 0 && day.weekday != 6 {
|
||||
left -= 1;
|
||||
}
|
||||
}
|
||||
n
|
||||
}
|
||||
_ => v + step,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3975,24 +3975,6 @@ fn fill_series_weekday_out_of_range_step_errors() {
|
||||
assert_eq!(v(&s, "A2"), Value::Empty);
|
||||
}
|
||||
|
||||
/// A step of a million weekdays moves whole weeks at a time instead of visiting every day, and
|
||||
/// lands where counting day by day would.
|
||||
#[test]
|
||||
fn fill_series_weekday_large_step_is_arithmetic() {
|
||||
let mut s = s();
|
||||
// 45005 is Monday 20 March 2023; five weekdays on from a Monday is the next Monday.
|
||||
s.execute("cell.set", json!({"cell": "A1", "input": "45005"})).unwrap();
|
||||
s.execute("edit.fillSeries", json!({"range": "A1:A3", "direction": "columns", "type": "date", "dateUnit": "weekday", "step": 1_000_000}))
|
||||
.unwrap();
|
||||
assert_eq!(v(&s, "A2"), Value::Number(45005.0 + 1_400_000.0));
|
||||
assert_eq!(v(&s, "A3"), Value::Number(45005.0 + 2_800_000.0));
|
||||
s.execute("cell.set", json!({"cell": "B1", "input": "45009"})).unwrap();
|
||||
s.execute("edit.fillSeries", json!({"range": "B1:B3", "direction": "columns", "type": "date", "dateUnit": "weekday", "step": -1})).unwrap();
|
||||
// Friday 24 March 2023 back one weekday at a time: Thursday, Wednesday.
|
||||
assert_eq!(v(&s, "B2"), Value::Number(45008.0));
|
||||
assert_eq!(v(&s, "B3"), Value::Number(45007.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_series_month_leap_year() {
|
||||
let s = series_date("2028-01-31", "A1:A3", "month");
|
||||
|
||||
@@ -339,11 +339,11 @@ fn numbervalue(a: &[Arg], c: &mut dyn Ctx) -> R<Value> {
|
||||
if body.is_empty() {
|
||||
return num_val(0.0);
|
||||
}
|
||||
let mut pct = 0;
|
||||
while let Some(b) = body.strip_suffix('%') {
|
||||
body = b.to_string();
|
||||
pct += 1;
|
||||
}
|
||||
// Count the trailing percent signs and cut them off in place: copying the rest once per sign
|
||||
// made a long run of them quadratic.
|
||||
let kept = body.trim_end_matches('%').len();
|
||||
let pct = body.len() - kept;
|
||||
body.truncate(kept);
|
||||
let mut seen_dec = false;
|
||||
let mut out = String::new();
|
||||
for c in body.chars() {
|
||||
@@ -1084,6 +1084,15 @@ mod tests {
|
||||
|
||||
use crate::util::testutil::*;
|
||||
|
||||
/// Trailing percent signs each divide by 100, and a long run of them is cut off in one pass.
|
||||
#[test]
|
||||
fn numbervalue_percent_signs() {
|
||||
close(ev("NUMBERVALUE", vec![t("5%%")]), 0.0005);
|
||||
close(ev("NUMBERVALUE", vec![t("1,5%"), t(","), t(".")]), 0.015);
|
||||
let many = format!("1{}", "%".repeat(32_000));
|
||||
close(ev("NUMBERVALUE", vec![t(&many)]), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bahttext() {
|
||||
let bt = |x: f64| ev("BAHTTEXT", vec![n(x)]);
|
||||
|
||||
Reference in New Issue
Block a user