diff options
Diffstat (limited to '')
-rw-r--r-- | source/lux/data/text.lux | 195 |
1 files changed, 0 insertions, 195 deletions
diff --git a/source/lux/data/text.lux b/source/lux/data/text.lux deleted file mode 100644 index af2de51ff..000000000 --- a/source/lux/data/text.lux +++ /dev/null @@ -1,195 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;import lux - (lux (control (monoid #as m) - (eq #as E) - (ord #as O) - (show #as S) - (monad #as M #refer #all)) - (data (number (int #open ("i" Int/Number Int/Ord))) - maybe))) - -## [Functions] -(def #export (size x) - (-> Text Int) - (_jvm_i2l (_jvm_invokevirtual "java.lang.String" "length" [] - x []))) - -(def #export (@ idx x) - (-> Int Text (Maybe Char)) - (if (and (i< idx (size x)) - (i>= idx 0)) - (#;Some (_jvm_invokevirtual "java.lang.String" "charAt" ["int"] - x [(_jvm_l2i idx)])) - #;None)) - -(def #export (contains? x y) - (-> Text Text Bool) - (_jvm_invokevirtual "java.lang.String" "contains" ["java.lang.CharSequence"] - x [y])) - -(do-template [<name> <method>] - [(def #export (<name> x) - (-> Text Text) - (_jvm_invokevirtual "java.lang.String" <method> [] - x []))] - [lower-case "toLowerCase"] - [upper-case "toUpperCase"] - [trim "trim"] - ) - -(def #export (sub' from to x) - (-> Int Int Text (Maybe Text)) - (if (and (i< from to) - (i>= from 0) - (i<= to (size x))) - (#;Some (_jvm_invokevirtual "java.lang.String" "substring" ["int" "int"] - x [(_jvm_l2i from) (_jvm_l2i to)])) - #;None)) - -(def #export (sub from x) - (-> Int Text (Maybe Text)) - (sub' from (size x) x)) - -(def #export (split at x) - (-> Int Text (Maybe (, Text Text))) - (if (and (i< at (size x)) - (i>= at 0)) - (let [pre (_jvm_invokevirtual "java.lang.String" "substring" ["int" "int"] - x [(_jvm_l2i 0) (_jvm_l2i at)]) - post (_jvm_invokevirtual "java.lang.String" "substring" ["int"] - x [(_jvm_l2i at)])] - (#;Some [pre post])) - #;None)) - -(def #export (replace pattern value template) - (-> Text Text Text Text) - (_jvm_invokevirtual "java.lang.String" "replace" ["java.lang.CharSequence" "java.lang.CharSequence"] - template [pattern value])) - -(do-template [<common> <general> <method>] - [(def #export (<general> pattern from x) - (-> Text Int Text (Maybe Int)) - (if (and (i< from (size x)) (i>= from 0)) - (case (_jvm_i2l (_jvm_invokevirtual "java.lang.String" <method> ["java.lang.String" "int"] - x [pattern (_jvm_l2i from)])) - -1 #;None - idx (#;Some idx)) - #;None)) - - (def #export (<common> pattern x) - (-> Text Text (Maybe Int)) - (case (_jvm_i2l (_jvm_invokevirtual "java.lang.String" <method> ["java.lang.String"] - x [pattern])) - -1 #;None - idx (#;Some idx)))] - - [index-of index-of' "indexOf"] - [last-index-of last-index-of' "lastIndexOf"] - ) - -(def #export (starts-with? prefix x) - (-> Text Text Bool) - (case (index-of prefix x) - (#;Some 0) - true - - _ - false)) - -(def #export (ends-with? postfix x) - (-> Text Text Bool) - (case (last-index-of postfix x) - (#;Some n) - (i= (i+ n (size postfix)) - (size x)) - - _ - false)) - -## [Structures] -(defstruct #export Text/Eq (E;Eq Text) - (def (= x y) - (_jvm_invokevirtual "java.lang.Object" "equals" ["java.lang.Object"] - x [y]))) - -(defstruct #export Text/Ord (O;Ord Text) - (def _eq Text/Eq) - - (do-template [<name> <op>] - [(def (<name> x y) - (<op> (_jvm_i2l (_jvm_invokevirtual "java.lang.String" "compareTo" ["java.lang.String"] - x [y])) - 0))] - - [< i<] - [<= i<=] - [> i>] - [>= i>=])) - -(defstruct #export Text/Show (S;Show Text) - (def show id)) - -(defstruct #export Text/Monoid (m;Monoid Text) - (def unit "") - (def (++ x y) - (_jvm_invokevirtual "java.lang.String" "concat" ["java.lang.String"] - x [y]))) - -## [Syntax] -(def (extract-var template) - (-> Text (Maybe (, Text Text Text))) - (do Maybe/Monad - [pre-idx (index-of "#{" template) - [pre in] (split pre-idx template) - [_ in] (split 2 in) - post-idx (index-of "}" in) - [var post] (split post-idx in) - #let [[_ post] (? ["" ""] (split 1 post))]] - (wrap [pre var post]))) - -(do-template [<name> <type> <tag>] - [(def (<name> value) - (-> <type> AST) - [["" -1 -1] (<tag> value)])] - - [text$ Text #;TextS] - [symbol$ Ident #;SymbolS]) - -(def (unravel-template template) - (-> Text (List AST)) - (case (extract-var template) - (#;Some [pre var post]) - (#;Cons (text$ pre) - (#;Cons (symbol$ ["" var]) - (unravel-template post))) - - #;None - (#;Cons (text$ template) #;Nil))) - -(defmacro #export (<> tokens state) - (case tokens - (#;Cons [_ (#;TextS template)] #;Nil) - (let [++ (symbol$ ["" ""])] - (#;Right state (#;Cons (` (;let [(~ ++) (get@ #m;++ Text/Monoid)] - (;$ (~ ++) (~@ (unravel-template template))))) - #;Nil))) - - _ - (#;Left "Wrong syntax for <>"))) - -(def #export (split-lines text) - (-> Text (List Text)) - (case (: (Maybe (List Text)) - (do Maybe/Monad - [idx (index-of "\n" text) - [head post] (split (inc idx) text)] - (wrap (#;Cons head (split-lines post))))) - #;None - (#;Cons text #;Nil) - - (#;Some xs) - xs)) |