cellrune 0.1.18

Bounded XLSX/XLSM reading, deterministic calculation, editing, and writing for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::super::ast::Expr;
use super::super::coerce::{to_logical, to_text};
use super::super::eval::{Engine, EvalContext};
use super::super::limits::CalculationLimitKind;
use super::super::value::{ErrorKind, Value};
use super::kernel::TextFunction;
use super::util::{collect_argument_values, required_number, required_text};

pub(super) fn call(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    function: TextFunction,
    args: &[Expr],
) -> Value {
    match function {
        TextFunction::Left => left(engine, context, args),
        TextFunction::Right => right(engine, context, args),
        TextFunction::Mid => mid(engine, context, args),
        TextFunction::Find => find(engine, context, args, true),
        TextFunction::Search => find(engine, context, args, false),
        TextFunction::Substitute => substitute(engine, context, args),
        TextFunction::Len => unary_text(
            engine,
            context,
            args,
            |text| text.chars().count().to_string(),
            true,
        ),
        TextFunction::Trim => unary_text(engine, context, args, trim_excel, false),
        TextFunction::Upper => unary_text(engine, context, args, |text| text.to_uppercase(), false),
        TextFunction::Proper => unary_text(engine, context, args, proper, false),
        TextFunction::Exact => exact(engine, context, args),
        TextFunction::Replace => replace(engine, context, args),
        TextFunction::Rept => rept(engine, context, args),
        TextFunction::Concat => concat(engine, context, args),
        TextFunction::TextJoin => textjoin(engine, context, args),
    }
}

fn right(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.is_empty() || args.len() > 2 {
        return Value::Error(ErrorKind::Value);
    }
    let text = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let count = match args.get(1) {
        Some(expr) => match required_number(engine, context, expr) {
            Ok(number) if number >= 0.0 => number.trunc() as usize,
            Ok(_) => return Value::Error(ErrorKind::Value),
            Err(kind) => return Value::Error(kind),
        },
        None => 1,
    };
    let character_count = text.chars().count();
    engine.bounded_text(
        text.chars()
            .skip(character_count.saturating_sub(count))
            .collect(),
    )
}

fn left(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.is_empty() || args.len() > 2 {
        return Value::Error(ErrorKind::Value);
    }
    let text = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let count = match args.get(1) {
        Some(expr) => match required_number(engine, context, expr) {
            Ok(number) if number >= 0.0 => number.trunc() as usize,
            Ok(_) => return Value::Error(ErrorKind::Value),
            Err(kind) => return Value::Error(kind),
        },
        None => 1,
    };
    engine.bounded_text(text.chars().take(count).collect())
}

fn mid(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.len() != 3 {
        return Value::Error(ErrorKind::Value);
    }
    let text = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let start = match required_number(engine, context, &args[1]) {
        Ok(number) if number >= 1.0 => number.trunc() as usize - 1,
        Ok(_) => return Value::Error(ErrorKind::Value),
        Err(kind) => return Value::Error(kind),
    };
    let count = match required_number(engine, context, &args[2]) {
        Ok(number) if number >= 0.0 => number.trunc() as usize,
        Ok(_) => return Value::Error(ErrorKind::Value),
        Err(kind) => return Value::Error(kind),
    };
    engine.bounded_text(text.chars().skip(start).take(count).collect())
}

fn find(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
    case_sensitive: bool,
) -> Value {
    if args.len() < 2 || args.len() > 3 {
        return Value::Error(ErrorKind::Value);
    }
    let needle = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let haystack = match required_text(engine, context, &args[1]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    if let Err(kind) = engine.ensure_text_bytes(needle.len().max(haystack.len())) {
        return Value::Error(kind);
    }
    let start = match args.get(2) {
        Some(expr) => match required_number(engine, context, expr) {
            Ok(number) if number >= 1.0 => number.trunc() as usize - 1,
            Ok(_) => return Value::Error(ErrorKind::Value),
            Err(kind) => return Value::Error(kind),
        },
        None => 0,
    };
    let (source, origins) = comparison_form(&haystack, case_sensitive);
    let (target, _) = comparison_form(&needle, case_sensitive);
    let source_length = haystack.chars().count();
    if start > source_length {
        return Value::Error(ErrorKind::Value);
    }
    if target.is_empty() {
        return Value::Number((start + 1) as f64);
    }
    // `start` indexes the original text, and case folding may have changed the
    // character count before it, so translate it into the folded sequence.
    let folded_start = origins.partition_point(|origin| *origin < start);
    source[folded_start..]
        .windows(target.len())
        .position(|window| window == target)
        .map_or(Value::Error(ErrorKind::Value), |offset| {
            Value::Number((origins[folded_start + offset] + 1) as f64)
        })
}

/// Returns the characters to compare and, for each of them, the index of the
/// original character it came from.
///
/// Case folding is applied one character at a time so that the mapping stays
/// exact. Folding the whole string at once can emit a different number of
/// characters than it consumed, for example `İ` lowercasing to `i` followed by a
/// combining dot, which would shift every position reported after it.
fn comparison_form(text: &str, case_sensitive: bool) -> (Vec<char>, Vec<usize>) {
    let mut characters = Vec::new();
    let mut origins = Vec::new();
    for (index, character) in text.chars().enumerate() {
        if case_sensitive {
            characters.push(character);
            origins.push(index);
        } else {
            for lowered in character.to_lowercase() {
                characters.push(lowered);
                origins.push(index);
            }
        }
    }
    (characters, origins)
}

fn substitute(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.len() < 3 || args.len() > 4 {
        return Value::Error(ErrorKind::Value);
    }
    let text = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let old = match required_text(engine, context, &args[1]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let new = match required_text(engine, context, &args[2]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    if old.is_empty() {
        return engine.bounded_text(text);
    }
    let Some(instance_expr) = args.get(3) else {
        let replacements = text.matches(&old).count();
        let Some(output_bytes) =
            replacement_output_bytes(text.len(), old.len(), new.len(), replacements)
        else {
            return Value::Error(ErrorKind::ResourceLimit(CalculationLimitKind::TextBytes));
        };
        if let Err(kind) = engine.ensure_text_bytes(output_bytes) {
            return Value::Error(kind);
        }
        return Value::Text(text.replace(&old, &new));
    };
    let instance = match required_number(engine, context, instance_expr) {
        Ok(number) if number >= 1.0 => number.trunc() as usize,
        Ok(_) => return Value::Error(ErrorKind::Value),
        Err(kind) => return Value::Error(kind),
    };
    let Some((start, _)) = text.match_indices(&old).nth(instance - 1) else {
        return engine.bounded_text(text);
    };
    let Some(output_bytes) = replacement_output_bytes(text.len(), old.len(), new.len(), 1) else {
        return Value::Error(ErrorKind::ResourceLimit(CalculationLimitKind::TextBytes));
    };
    if let Err(kind) = engine.ensure_text_bytes(output_bytes) {
        return Value::Error(kind);
    }
    let mut result = text;
    result.replace_range(start..start + old.len(), &new);
    Value::Text(result)
}

fn replacement_output_bytes(
    source_bytes: usize,
    old_bytes: usize,
    new_bytes: usize,
    replacements: usize,
) -> Option<usize> {
    source_bytes
        .checked_sub(old_bytes.checked_mul(replacements)?)?
        .checked_add(new_bytes.checked_mul(replacements)?)
}

fn unary_text(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
    operation: impl FnOnce(&str) -> String,
    numeric_result: bool,
) -> Value {
    if args.len() != 1 {
        return Value::Error(ErrorKind::Value);
    }
    match required_text(engine, context, &args[0]) {
        Ok(text) if numeric_result => operation(&text)
            .parse::<f64>()
            .map(Value::Number)
            .unwrap_or(Value::Error(ErrorKind::Value)),
        Ok(text) => engine.bounded_text(operation(&text)),
        Err(kind) => Value::Error(kind),
    }
}

fn trim_excel(text: &str) -> String {
    text.split(' ')
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

fn proper(text: &str) -> String {
    let mut capitalize = true;
    text.chars()
        .map(|character| {
            if character.is_alphanumeric() {
                let mapped = if capitalize {
                    character.to_uppercase().collect::<String>()
                } else {
                    character.to_lowercase().collect::<String>()
                };
                capitalize = false;
                mapped
            } else {
                capitalize = true;
                character.to_string()
            }
        })
        .collect()
}

fn exact(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.len() != 2 {
        return Value::Error(ErrorKind::Value);
    }
    match (
        required_text(engine, context, &args[0]),
        required_text(engine, context, &args[1]),
    ) {
        (Ok(left), Ok(right)) => Value::Logical(left == right),
        (Err(kind), _) | (_, Err(kind)) => Value::Error(kind),
    }
}

fn replace(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.len() != 4 {
        return Value::Error(ErrorKind::Value);
    }
    let text = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let start = match required_number(engine, context, &args[1]) {
        Ok(number) if number >= 1.0 => number.trunc() as usize - 1,
        Ok(_) => return Value::Error(ErrorKind::Value),
        Err(kind) => return Value::Error(kind),
    };
    let count = match required_number(engine, context, &args[2]) {
        Ok(number) if number >= 0.0 => number.trunc() as usize,
        Ok(_) => return Value::Error(ErrorKind::Value),
        Err(kind) => return Value::Error(kind),
    };
    let replacement = match required_text(engine, context, &args[3]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let char_count = text.chars().count();
    if start > char_count {
        return Value::Error(ErrorKind::Value);
    }
    let end = start.saturating_add(count).min(char_count);
    let byte_start = text
        .char_indices()
        .nth(start)
        .map_or(text.len(), |(index, _)| index);
    let byte_end = text
        .char_indices()
        .nth(end)
        .map_or(text.len(), |(index, _)| index);
    let Some(output_bytes) = text
        .len()
        .checked_sub(byte_end - byte_start)
        .and_then(|bytes| bytes.checked_add(replacement.len()))
    else {
        return Value::Error(ErrorKind::ResourceLimit(CalculationLimitKind::TextBytes));
    };
    if let Err(kind) = engine.ensure_text_bytes(output_bytes) {
        return Value::Error(kind);
    }
    let mut result = text;
    result.replace_range(byte_start..byte_end, &replacement);
    Value::Text(result)
}

fn rept(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.len() != 2 {
        return Value::Error(ErrorKind::Value);
    }
    let text = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let count = match required_number(engine, context, &args[1]) {
        Ok(number) if number >= 0.0 => number.trunc() as usize,
        Ok(_) => return Value::Error(ErrorKind::Value),
        Err(kind) => return Value::Error(kind),
    };
    let Some(output_bytes) = text.len().checked_mul(count) else {
        return Value::Error(ErrorKind::Value);
    };
    if output_bytes > 32_767 {
        return Value::Error(ErrorKind::Value);
    }
    if let Err(kind) = engine.ensure_text_bytes(output_bytes) {
        return Value::Error(kind);
    }
    Value::Text(text.repeat(count))
}

fn concat(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.is_empty() {
        return Value::Error(ErrorKind::Value);
    }
    let values = match collect_argument_values(engine, context, args) {
        Ok(values) => values,
        Err(kind) => return Value::Error(kind),
    };
    let mut result = String::new();
    for item in values {
        match to_text(&item.value) {
            Ok(text) => {
                let Some(output_bytes) = result.len().checked_add(text.len()) else {
                    return Value::Error(ErrorKind::ResourceLimit(CalculationLimitKind::TextBytes));
                };
                if let Err(kind) = engine.ensure_text_bytes(output_bytes) {
                    return Value::Error(kind);
                }
                result.push_str(&text);
            }
            Err(kind) => return Value::Error(kind),
        }
    }
    Value::Text(result)
}

fn textjoin(engine: &Engine<'_>, context: EvalContext<'_>, args: &[Expr]) -> Value {
    if args.len() < 3 {
        return Value::Error(ErrorKind::Value);
    }
    let delimiter = match required_text(engine, context, &args[0]) {
        Ok(text) => text,
        Err(kind) => return Value::Error(kind),
    };
    let ignore_empty = match to_logical(&engine.eval_scalar(context, &args[1])) {
        Ok(logical) => logical,
        Err(kind) => return Value::Error(kind),
    };
    let values = match collect_argument_values(engine, context, &args[2..]) {
        Ok(values) => values,
        Err(kind) => return Value::Error(kind),
    };
    let mut result = String::new();
    let mut has_part = false;
    for item in values {
        match to_text(&item.value) {
            Ok(text) if ignore_empty && text.is_empty() => {}
            Ok(text) => {
                let delimiter_bytes = if has_part { delimiter.len() } else { 0 };
                let Some(output_bytes) = result
                    .len()
                    .checked_add(delimiter_bytes)
                    .and_then(|bytes| bytes.checked_add(text.len()))
                else {
                    return Value::Error(ErrorKind::ResourceLimit(CalculationLimitKind::TextBytes));
                };
                if let Err(kind) = engine.ensure_text_bytes(output_bytes) {
                    return Value::Error(kind);
                }
                if has_part {
                    result.push_str(&delimiter);
                }
                result.push_str(&text);
                has_part = true;
            }
            Err(kind) => return Value::Error(kind),
        }
    }
    Value::Text(result)
}