Skip to content

Commit

Permalink
in split, filter out any result that equals the delimiter (9)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtrilling authored and evinism committed Dec 30, 2022
1 parent 0231cd8 commit 611e216
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions rust/src/function/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ pub fn match_op(left: Value, right: Value) -> Result<Value> {
}

pub fn split(arg_parser: ArgParser) -> Result<Value> {
let (pattern_val, target_val) = arg_parser.two_args()?;
let (pattern_arg, target_arg) = arg_parser.two_args()?;

let pattern = match pattern_val.to_value(arg_parser.data)? {
let pattern_val = pattern_arg.to_value(arg_parser.data)?;
let pattern = match &pattern_val {
Value::Regex(s, _) | Value::String(s) => match Regex::new(&s) {
Ok(pat) => Ok(pat),
Err(err) => Err(Error::regex(err)),
Expand All @@ -67,14 +68,15 @@ pub fn split(arg_parser: ArgParser) -> Result<Value> {
)),
}?;

let target = match target_val.to_value(arg_parser.data)? {
let target = match target_arg.to_value(arg_parser.data)? {
Value::String(s) => Ok(s),
_ => Err(Error::eval("split target must be a string".to_string())),
}?;

Ok(Value::Array(
pattern
.split(&target)
.filter(|elt| *elt != pattern_val.to_string().as_str())
.map(|elt| Value::String(elt.to_string()))
.collect(),
))
Expand Down
2 changes: 1 addition & 1 deletion rust/src/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::fmt;
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Regex(_, _) => Err(fmt::Error),
Value::Null => write!(f, "null"),
Value::Boolean(true) => write!(f, "true"),
Value::Boolean(false) => write!(f, "false"),
Value::Number(Number::Float(num)) => write!(f, "{}", from_number(*num)),
Value::Number(Number::Int(num)) => write!(f, "{}", from_number(*num as f64)),
Value::String(s) => write!(f, "{}", s),
Value::Ident(s) => write!(f, "{}", s),
Value::Regex(s, _) => write!(f, "{}", s),
Value::Array(a) => {
write!(
f,
Expand Down

0 comments on commit 611e216

Please sign in to comment.