diff options
author | Basile Henry | 2020-11-03 23:58:28 +0100 |
---|---|---|
committer | GitHub | 2020-11-03 23:58:28 +0100 |
commit | 71c8e889610b8b9bb6155c20ca91bac4ebc9daee (patch) | |
tree | 8cd7429e654d31b9fbc68a97d71db92e7571c08a | |
parent | f87ffe7a590c9e0a3e0a57854c739a0f89c3784d (diff) | |
parent | a56cd6021705ebfd310bc902c4f549bea9b06a5b (diff) |
Merge pull request #191 from basile-henry/text-replace
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 8e0674eb419876e81d9e2218ddefc503a85fd58 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, |