mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
fix: Match OPA behavior for split (#295)
In case of empty delimiter, Rust's split returns leading and trailing empty strings whereas Golang's doesn't. Change behavior to match Golang/OPA. fixes #291 Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
e4a58ad1dc
commit
a488a84969
+10
-3
@@ -146,11 +146,18 @@ fn split(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Re
|
||||
let s = ensure_string(name, ¶ms[0], &args[0])?;
|
||||
let delimiter = ensure_string(name, ¶ms[1], &args[1])?;
|
||||
|
||||
Ok(Value::from_array(
|
||||
// Handle https://github.com/microsoft/regorus/issues/291
|
||||
let parts: Vec<Value> = if delimiter.as_ref() == "" {
|
||||
// If delimiter is "", str::split returns a leading and trailing "" whereas Golang's split doesn't.
|
||||
// Therefore avoid str::split and instead return each char as a Value::String.
|
||||
s.chars().map(|c| Value::from(c.to_string())).collect()
|
||||
} else {
|
||||
s.split(delimiter.as_ref())
|
||||
.map(|s| Value::String(s.into()))
|
||||
.collect(),
|
||||
))
|
||||
.collect()
|
||||
};
|
||||
|
||||
Ok(Value::from(parts))
|
||||
}
|
||||
|
||||
fn to_string(v: &Value, unescape: bool) -> String {
|
||||
|
||||
Reference in New Issue
Block a user