Add ends-with operator and fix dataframe operator behavior (#5395)

* add ends-with operator

* escape needles in dataframe operator regex patterns
This commit is contained in:
panicbit
2022-05-02 10:02:38 +02:00
committed by GitHub
parent 07a7bb14bf
commit 49cbc30974
8 changed files with 68 additions and 2 deletions

View File

@ -35,6 +35,7 @@ impl Expression {
Operator::NotRegexMatch
| Operator::RegexMatch
| Operator::StartsWith
| Operator::EndsWith
| Operator::LessThan
| Operator::LessThanOrEqual
| Operator::GreaterThan

View File

@ -24,6 +24,7 @@ pub enum Operator {
Or,
Pow,
StartsWith,
EndsWith,
}
impl Display for Operator {
@ -48,6 +49,7 @@ impl Display for Operator {
Operator::LessThanOrEqual => write!(f, "<="),
Operator::GreaterThanOrEqual => write!(f, ">="),
Operator::StartsWith => write!(f, "starts-with"),
Operator::EndsWith => write!(f, "ends-with"),
}
}
}

View File

@ -2080,6 +2080,25 @@ impl Value {
}
}
pub fn ends_with(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) {
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
val: lhs.ends_with(rhs),
span,
}),
(Value::CustomValue { val: lhs, span }, rhs) => {
lhs.operation(*span, Operator::EndsWith, op, rhs)
}
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),
lhs_span: self.span()?,
rhs_ty: rhs.get_type(),
rhs_span: rhs.span()?,
}),
}
}
pub fn modulo(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) {
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {