Skip to main content

ganit_core/eval/functions/text/replaceb/
mod.rs

1use crate::eval::coercion::{to_number, to_string_val};
2use crate::eval::functions::check_arity;
3use crate::types::{ErrorKind, Value};
4
5/// `REPLACEB(old_text, start_byte, num_bytes, new_text)` — replaces N bytes starting at byte position.
6/// start_byte is 1-based. For ASCII text this is identical to REPLACE.
7/// Returns `#VALUE!` if start_byte < 1 or num_bytes < 0.
8pub fn replaceb_fn(args: &[Value]) -> Value {
9    if let Some(err) = check_arity(args, 4, 4) {
10        return err;
11    }
12    let text = match to_string_val(args[0].clone()) {
13        Ok(s) => s,
14        Err(e) => return e,
15    };
16    let start_num = match to_number(args[1].clone()) {
17        Ok(n) => n,
18        Err(e) => return e,
19    };
20    let num_bytes = match to_number(args[2].clone()) {
21        Ok(n) => n,
22        Err(e) => return e,
23    };
24    let new_text = match to_string_val(args[3].clone()) {
25        Ok(s) => s,
26        Err(e) => return e,
27    };
28    if start_num < 1.0 || num_bytes < 0.0 {
29        return Value::Error(ErrorKind::Value);
30    }
31    let start_byte = (start_num as usize) - 1;
32    let num_bytes = num_bytes as usize;
33    let total = text.len();
34    let start_byte = start_byte.min(total);
35    // Snap to char boundary
36    let start_byte = (start_byte..=total)
37        .find(|&i| text.is_char_boundary(i))
38        .unwrap_or(total);
39    let end_byte = (start_byte + num_bytes).min(total);
40    let end_byte = (end_byte..=total)
41        .find(|&i| text.is_char_boundary(i))
42        .unwrap_or(total);
43    Value::Text(format!("{}{}{}", &text[..start_byte], new_text, &text[end_byte..]))
44}
45
46#[cfg(test)]
47mod tests;