diff options
author | Basile Henry | 2020-11-01 10:21:53 +0100 |
---|---|---|
committer | Basile Henry | 2020-11-01 10:21:53 +0100 |
commit | e12d7fc914a1c97376ed112a92326a138fa2b3f8 (patch) | |
tree | 0c5c96566b35cd2a9f140787fea4fb374e9a8b8c | |
parent | 48037367933085ca9c1c67c8c59f311e2b21be6d (diff) |
Allow `Text/replace ` to support an abstract haystack
m--------- | dhall-lang | 0 | ||||
-rw-r--r-- | dhall/src/builtins.rs | 50 |
2 files changed, 28 insertions, 22 deletions
diff --git a/dhall-lang b/dhall-lang -Subproject e1e726f9e835f0b9ea61f8f193eff93778af2a2 +Subproject ada9a99c12afdf289be9be2971ee043df370028 diff --git a/dhall/src/builtins.rs b/dhall/src/builtins.rs index 41a9f75..cc426dd 100644 --- a/dhall/src/builtins.rs +++ b/dhall/src/builtins.rs @@ -418,30 +418,36 @@ fn apply_builtin(b: Builtin, args: Vec<Nir>, env: NzEnv) -> NirKind { } } - // The needle and the haystack need to be fully - // evaluated as Text otherwise no progress can be made - match (nir_to_string(needle), nir_to_string(haystack)) { - (Some(n), Some(h)) => { - // When the needle is empty the haystack is returned untouched - if n.is_empty() { - Ret::Nir(haystack.clone()) - // Fast case when replacement is fully evaluated - } else if let Some(r) = nir_to_string(replacement) { - Ret::Nir(Nir::from_text(h.replace(&n, &r))) - } else { - use itertools::Itertools; + // The needle needs to be fully evaluated as Text otherwise no + // progress can be made + match nir_to_string(needle) { + // When the needle is empty the haystack is returned untouched + Some(n) if n.is_empty() => Ret::Nir(haystack.clone()), + Some(n) => { + // The haystack needs to be fully evaluated as Text otherwise no + // progress can be made + if let Some(h) = nir_to_string(haystack) { + // Fast case when replacement is fully evaluated + if let Some(r) = nir_to_string(replacement) { + Ret::Nir(Nir::from_text(h.replace(&n, &r))) + } else { + use itertools::Itertools; - let parts = h.split(&n).map(|s| { - InterpolatedTextContents::Text(s.to_string()) - }); - let replacement = - InterpolatedTextContents::Expr(replacement.clone()); + let parts = h.split(&n).map(|s| { + InterpolatedTextContents::Text(s.to_string()) + }); + let replacement = InterpolatedTextContents::Expr( + replacement.clone(), + ); - Ret::Nir(Nir::from_kind(NirKind::TextLit( - nze::nir::TextLit::new( - parts.intersperse(replacement), - ), - ))) + Ret::Nir(Nir::from_kind(NirKind::TextLit( + nze::nir::TextLit::new( + parts.intersperse(replacement), + ), + ))) + } + } else { + Ret::DoneAsIs } } _ => Ret::DoneAsIs, |