aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2021-05-24 18:44:13 -0400
committerEduardo Julian2021-05-24 18:44:13 -0400
commit2afeb97b7c53ed146ec2cb996f61f1a7d56b372d (patch)
tree964999304659a4032759694c5cdc2364f087ead8
parent86538182a50390e7882778cc02e69482e846edd5 (diff)
Using FFI machinery when possible when doing text replacement.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/data/text.lux45
1 files changed, 37 insertions, 8 deletions
diff --git a/stdlib/source/lux/data/text.lux b/stdlib/source/lux/data/text.lux
index b344a2ad9..91d520c3c 100644
--- a/stdlib/source/lux/data/text.lux
+++ b/stdlib/source/lux/data/text.lux
@@ -170,14 +170,43 @@
(def: #export (replace_all pattern replacement template)
(-> Text Text Text Text)
- (loop [left ""
- right template]
- (case (..split_with pattern right)
- (#.Some [pre post])
- (recur ($_ "lux text concat" left pre replacement) post)
-
- #.None
- ("lux text concat" left right))))
+ (for {@.old
+ (:coerce Text
+ ("jvm invokevirtual:java.lang.String:replace:java.lang.CharSequence,java.lang.CharSequence"
+ (:coerce (primitive "java.lang.String") template)
+ (:coerce (primitive "java.lang.CharSequence") pattern)
+ (:coerce (primitive "java.lang.CharSequence") replacement)))
+ @.jvm
+ (:coerce Text
+ ("jvm member invoke virtual" [] "java.lang.String" "replace" []
+ (:coerce (primitive "java.lang.String") template)
+ ["Ljava/lang/CharSequence;" (:coerce (primitive "java.lang.CharSequence") pattern)]
+ ["Ljava/lang/CharSequence;" (:coerce (primitive "java.lang.CharSequence") replacement)]))
+ @.js
+ (:coerce Text
+ ("js object do" "replaceAll" template [pattern replacement]))
+ @.python
+ (:coerce Text
+ ("python object do" "replace" template pattern replacement))
+ ## TODO @.lua
+ @.ruby
+ (:coerce Text
+ ("ruby object do" "gsub" template pattern replacement))
+ @.php
+ (:coerce Text
+ ("php apply" (:assume ("php constant" "str_replace"))
+ pattern replacement template))
+ ## TODO @.scheme
+ }
+ ## Inefficient default
+ (loop [left ""
+ right template]
+ (case (..split_with pattern right)
+ (#.Some [pre post])
+ (recur ($_ "lux text concat" left pre replacement) post)
+
+ #.None
+ ("lux text concat" left right)))))
(structure: #export equivalence
(Equivalence Text)