Repository: pola-rs/polars
File: crates/polars-plan/src/dsl/functions/concat.rs (Rust)

Code before:
use super::*;

#[cfg(all(feature = "concat_str", feature = "strings"))]
/// Horizontally concat string columns in linear time
pub fn concat_str<E: AsRef<[Expr]>>(s: E, separator: &str, ignore_nulls: bool) -> Expr {
    let input = s.as_ref().to_vec();
    let separator = separator.into();

    Expr::Function {
        input,
        function: StringFunction::ConcatHorizontal {
            delimiter: separator,
            ignore_nulls,
        }
        .into(),
    }
}

#[cfg(all(feature = "concat_str", feature = "strings"))]
/// Format the results of an array of expressions using a format string
pub fn format_str<E: AsRef<[Expr]>>(format: &str, args: E) -> PolarsResult<Expr> {
    let mut positional_input = std::collections::VecDeque::from_iter(args.as_ref());
    let mut input = Vec::new();
    let mut automatic_used = false;
    let mut index_used = false;

    // Parse format string into constant literal plus insertion locations per
    // input expression.
    let bytes = format.as_bytes();
    let mut s = Vec::with_capacity(format.len());
    let mut insertions = Vec::with_capacity(input.len());
    let mut i = 0;
    while i < format.len() {
        match bytes[i] {
            b'{' => {
                let escaped = bytes.get(i + 1) == Some(&b'{');
                if escaped {
                    s.push(b'{');
                    i += 2;
                    continue;
                }

                let Some(close_offset) = bytes[i..].iter().position(|b| *b == b'}') else {
                    polars_bail!(InvalidOperation: "unmatched '{{' in format string:\n{format}\n\nYou can escape '{{' by writing '{{{{'.");
                };


Review comment (suggestion):
Could we do drive-by to change `args` to just take `&[Expr]`


Code after:
use super::*;

#[cfg(all(feature = "concat_str", feature = "strings"))]
/// Horizontally concat string columns in linear time
pub fn concat_str<E: AsRef<[Expr]>>(s: E, separator: &str, ignore_nulls: bool) -> Expr {
    let input = s.as_ref().to_vec();
    let separator = separator.into();

    Expr::Function {
        input,
        function: StringFunction::ConcatHorizontal {
            delimiter: separator,
            ignore_nulls,
        }
        .into(),
    }
}

#[cfg(all(feature = "concat_str", feature = "strings"))]
/// Format the results of an array of expressions using a format string
pub fn format_str(format: &str, args: &[Expr]) -> PolarsResult<Expr> {
    let mut positional_input = std::collections::VecDeque::from_iter(args);
    let mut input = Vec::new();
    let mut automatic_used = false;
    let mut index_used = false;

    // Parse format string into constant literal plus insertion locations per
    // input expression.
    let bytes = format.as_bytes();
    let mut s = Vec::with_capacity(format.len());
    let mut insertions = Vec::with_capacity(input.len());
    let mut i = 0;
    while i < format.len() {
        match bytes[i] {
            b'{' => {
                let escaped = bytes.get(i + 1) == Some(&b'{');
                if escaped {
                    s.push(b'{');
                    i += 2;
                    continue;
                }

                let Some(close_offset) = bytes[i..].iter().position(|b| *b == b'}') else {
                    polars_bail!(InvalidOperation: "unmatched '{{' in format string:\n{format}\n\nYou can escape '{{' by writing '{{{{'.");
                };