diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/library/lux/data/text.lux | 147 |
1 files changed, 88 insertions, 59 deletions
diff --git a/stdlib/source/library/lux/data/text.lux b/stdlib/source/library/lux/data/text.lux index 177192e95..01e39705f 100644 --- a/stdlib/source/library/lux/data/text.lux +++ b/stdlib/source/library/lux/data/text.lux @@ -1,23 +1,23 @@ (.using - [library - [lux {"-" char} - ["@" target] - [abstract - [hash {"+" Hash}] - [monoid {"+" Monoid}] - [equivalence {"+" Equivalence}] - [order {"+" Order}] - [monad {"+" do}] - [codec {"+" Codec}]] - [control - ["[0]" maybe]] - [data - [collection - ["[0]" list ("[1]#[0]" mix)]]] - [math - [number - ["n" nat] - ["[0]" i64]]]]]) + [library + [lux {"-" char} + ["@" target] + [abstract + [hash {"+" Hash}] + [monoid {"+" Monoid}] + [equivalence {"+" Equivalence}] + [order {"+" Order}] + [monad {"+" do}] + [codec {"+" Codec}]] + [control + ["[0]" maybe]] + [data + [collection + ["[0]" list ("[1]#[0]" mix)]]] + [math + [number + ["n" nat] + ["[0]" i64]]]]]) (type: .public Char Nat) @@ -182,48 +182,77 @@ [[pre post] (..split_by pattern template)] (in ($_ "lux text concat" pre replacement post))))) +(for [@.js (as_is (macro: (defined? tokens lux) + (case tokens + (^ (list it)) + {.#Right [lux (list (` (.case ("js type-of" ("js constant" (~ it))) + "undefined" + .false + + (~' _) + .true)))]} + + _ + {.#Left ""})) + (macro: (if_nashorn tokens lux) + (case tokens + (^ (list then else)) + {.#Right [lux (list (if (and (..defined? "java") + (..defined? "java.lang") + (..defined? "java.lang.Object")) + then + else))]} + + _ + {.#Left ""})))] + (as_is)) + (def: .public (replaced pattern replacement template) (-> Text Text Text Text) - (for [@.old - (:as Text - ("jvm invokevirtual:java.lang.String:replace:java.lang.CharSequence,java.lang.CharSequence" - (:as (Primitive "java.lang.String") template) - (:as (Primitive "java.lang.CharSequence") pattern) - (:as (Primitive "java.lang.CharSequence") replacement))) - @.jvm - (:as Text - ("jvm member invoke virtual" [] "java.lang.String" "replace" [] - (:as (Primitive "java.lang.String") template) - ["Ljava/lang/CharSequence;" (:as (Primitive "java.lang.CharSequence") pattern)] - ["Ljava/lang/CharSequence;" (:as (Primitive "java.lang.CharSequence") replacement)])) - ... TODO: Comment/turn-off when generating a JS compiler using a JVM-based compiler because Nashorn's implementation of "replaceAll" is incorrect. - @.js - (:as Text - ("js object do" "replaceAll" template [pattern replacement])) - @.python - (:as Text - ("python object do" "replace" template pattern replacement)) - ... TODO @.lua - @.ruby - (:as Text - ("ruby object do" "gsub" template pattern replacement)) - @.php - (:as Text - ("php apply" (:expected ("php constant" "str_replace")) - pattern replacement template)) - ... TODO @.scheme - ... TODO @.common_lisp - ... TODO @.r - ] - ... Inefficient default - (loop [left "" - right template] - (case (..split_by pattern right) - {.#Some [pre post]} - (again ($_ "lux text concat" left pre replacement) post) - - {.#None} - ("lux text concat" left right))))) + (with_expansions [... Inefficient default + <default> (loop [left "" + right template] + (case (..split_by pattern right) + {.#Some [pre post]} + (again ($_ "lux text concat" left pre replacement) post) + + {.#None} + ("lux text concat" left right)))] + (for [@.old + (:as Text + ("jvm invokevirtual:java.lang.String:replace:java.lang.CharSequence,java.lang.CharSequence" + (:as (Primitive "java.lang.String") template) + (:as (Primitive "java.lang.CharSequence") pattern) + (:as (Primitive "java.lang.CharSequence") replacement))) + @.jvm + (:as Text + ("jvm member invoke virtual" [] "java.lang.String" "replace" [] + (:as (Primitive "java.lang.String") template) + ["Ljava/lang/CharSequence;" (:as (Primitive "java.lang.CharSequence") pattern)] + ["Ljava/lang/CharSequence;" (:as (Primitive "java.lang.CharSequence") replacement)])) + @.js + ... TODO: Remove this when Nashorn is no longer being used. + (..if_nashorn + <default> + (:as Text + ("js object do" "replaceAll" template [pattern replacement]))) + @.python + (:as Text + ("python object do" "replace" template pattern replacement)) + ... TODO @.lua + @.ruby + (:as Text + ("ruby object do" "gsub" template pattern replacement)) + @.php + (:as Text + ("php apply" (:expected ("php constant" "str_replace")) + pattern replacement template)) + ... TODO @.scheme + ... TODO @.common_lisp + ... TODO @.r + ] + ... Inefficient default + <default>))) (implementation: .public equivalence (Equivalence Text) |