apply_str_float

Function apply_str_float 

Source
pub fn apply_str_float<T, F>(
    lhs: StringAVT<'_, T>,
    rhs: &[F],
    op: ArithmeticOperator,
) -> Result<StringArray<T>, KernelError>
where T: Integer, F: Into<f64> + Copy + Float,
Expand description

Applies an element-wise binary operation between a StringArray<T> and a slice of floating-point values, producing a new StringArray<T>. Each operation is performed by interpreting the float as a finite decimal string representation (f64 formatted with ryu), and applying string transformations accordingly.

Supported operations:

  • Add: Appends the stringified float to the string from lhs.
  • Subtract: Removes the first occurrence of the stringified float from lhs, if present. If not found, lhs is returned unchanged.
  • Multiply: Repeats the lhs string N times, where N = abs(round(rhs)) % (STRING_MULTIPLICATION_LIMIT + 1).
  • Divide: Splits the lhs string by the stringified float, and joins the segments using '|'. If the float pattern is not found, the original string is returned unchanged.

Null handling:

  • If the lhs value is null at an index, the result is null at that index.
  • The float operand cannot be null; the caller must guarantee its presence.

Output:

  • A new StringArray<T> with the same length as lhs.
  • The underlying byte storage is preallocated based on a prepass analysis of required capacity.

Errors:

  • Returns KernelError::LengthMismatch if lhs and rhs lengths differ.
  • Returns KernelError::UnsupportedType if the operator is not one of Add, Subtract, Multiply, Divide.

§Features

This function is only available when the str_arithmetic feature is enabled.

This kernel is optional as it pulls in external dependencies, and fits more niche use cases. It’s a good fit for flex-typing scenarios where users are concatenating strings and numbers, or working with semi-structured web content, string formatting pipelines etc.

§Safety

  • Uses unchecked access and raw pointer copies for performance. Invariants around memory safety must hold.
  • Assumes rhs contains only finite floating-point values.