diff options
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/source/lux.lux | 10 | ||||
-rw-r--r-- | stdlib/source/lux/data/number.lux | 66 | ||||
-rw-r--r-- | stdlib/source/lux/data/text.lux | 7 |
3 files changed, 39 insertions, 44 deletions
diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux index 0da0a628a..d70318f83 100644 --- a/stdlib/source/lux.lux +++ b/stdlib/source/lux.lux @@ -3395,17 +3395,11 @@ (def: (clip1 from text) (-> Nat Text (Maybe Text)) - (let [to ("lux text size" text)] - (if (n.<= to from) - (#;Some ("lux text clip" text from to)) - #;None))) + ("lux text clip" text from ("lux text size" text))) (def: (clip2 from to text) (-> Nat Nat Text (Maybe Text)) - (if (and (n.<= ("lux text size" text) to) - (n.<= to from)) - (#;Some ("lux text clip" text from to)) - #;None)) + ("lux text clip" text from to)) (def: #export (error! message) {#;doc "## Causes an error, with the given error message. diff --git a/stdlib/source/lux/data/number.lux b/stdlib/source/lux/data/number.lux index 06a8809e1..e9009102b 100644 --- a/stdlib/source/lux/data/number.lux +++ b/stdlib/source/lux/data/number.lux @@ -172,8 +172,8 @@ ) ## [Values & Syntax] -(def: (get-char idx full) - (-> Nat Text Text) +(def: (get-char full idx) + (-> Text Nat (Maybe Text)) ("lux text clip" full idx (n.inc idx))) (do-template [<struct> <base> <char-set> <error>] @@ -181,7 +181,7 @@ (def: (encode value) (loop [input value output ""] - (let [digit (get-char (n.% <base> input) <char-set>) + (let [digit (maybe;assume (get-char <char-set> (n.% <base> input))) output' ("lux text concat" digit output) input' (n./ <base> input)] (if (n.= +0 input') @@ -197,7 +197,7 @@ (loop [idx +1 output +0] (if (n.< input-size idx) - (let [digit (get-char idx input)] + (let [digit (maybe;assume (get-char input idx))] (case ("lux text index" <char-set> digit +0) #;None (#E;Error ("lux text concat" <error> repr)) @@ -226,19 +226,20 @@ "-" "")] (loop [input (|> value (i./ <base>) (:: Number<Int> abs)) - output (get-char (|> value (i.% <base>) (:: Number<Int> abs) int-to-nat) - <char-set>)] + output (|> value (i.% <base>) (:: Number<Int> abs) int-to-nat + (get-char <char-set>) + maybe;assume)] (if (i.= 0 input) ("lux text concat" sign output) - (let [digit (get-char (int-to-nat (i.% <base> input)) <char-set>)] + (let [digit (maybe;assume (get-char <char-set> (int-to-nat (i.% <base> input))))] (recur (i./ <base> input) ("lux text concat" digit output)))))))) (def: (decode repr) (let [input-size ("lux text size" repr)] (if (n.>= +1 input-size) - (let [sign (case (get-char +0 repr) - "-" + (let [sign (case (get-char repr +0) + (^ (#;Some "-")) -1 _ @@ -247,7 +248,7 @@ (loop [idx (if (i.= -1 sign) +1 +0) output 0] (if (n.< input-size idx) - (let [digit (get-char idx input)] + (let [digit (maybe;assume (get-char input idx))] (case ("lux text index" <char-set> digit +0) #;None (#E;Error <error>) @@ -266,7 +267,7 @@ (def: (de-prefix input) (-> Text Text) - ("lux text clip" input +1 ("lux text size" input))) + (maybe;assume ("lux text clip" input +1 ("lux text size" input)))) (do-template [<struct> <nat> <char-bit-size> <error>] [(struct: #export <struct> (Codec Text Deg) @@ -314,8 +315,8 @@ (if (f.= 0.0 dec-left) ("lux text concat" "." output) (let [shifted (f.* <base> dec-left) - digit (get-char (|> shifted (f.% <base>) frac-to-int int-to-nat) - <char-set>)] + digit (|> shifted (f.% <base>) frac-to-int int-to-nat + (get-char <char-set>) maybe;assume)] (recur (f.% 1.0 shifted) ("lux text concat" output digit))))))] ("lux text concat" whole-part decimal-part))) @@ -323,8 +324,8 @@ (def: (decode repr) (case ("lux text index" repr "." +0) (#;Some split-index) - (let [whole-part ("lux text clip" repr +0 split-index) - decimal-part ("lux text clip" repr (n.inc split-index) ("lux text size" repr))] + (let [whole-part (maybe;assume ("lux text clip" repr +0 split-index)) + decimal-part (maybe;assume ("lux text clip" repr (n.inc split-index) ("lux text size" repr)))] (case [(:: <int> decode whole-part) (:: <int> decode decimal-part)] (^multi [(#;Some whole) (#;Some decimal)] @@ -368,8 +369,8 @@ (if (n.<= chunk-size num-digits) (list digits) (let [boundary (n.- chunk-size num-digits) - chunk ("lux text clip" digits boundary num-digits) - remaining ("lux text clip" digits +0 boundary)] + chunk (maybe;assume ("lux text clip" digits boundary num-digits)) + remaining (maybe;assume ("lux text clip" digits +0 boundary))] (list& chunk (segment-digits chunk-size remaining))))))) (def: (bin-segment-to-hex input) @@ -498,10 +499,10 @@ (let [sign (:: Number<Frac> signum value) raw-bin (:: Binary@Codec<Text,Frac> encode value) dot-idx (maybe;assume ("lux text index" raw-bin "." +0)) - whole-part ("lux text clip" raw-bin - (if (f.= -1.0 sign) +1 +0) - dot-idx) - decimal-part ("lux text clip" raw-bin (n.inc dot-idx) ("lux text size" raw-bin)) + whole-part (maybe;assume ("lux text clip" raw-bin + (if (f.= -1.0 sign) +1 +0) + dot-idx)) + decimal-part (maybe;assume ("lux text clip" raw-bin (n.inc dot-idx) ("lux text size" raw-bin))) hex-output (|> (<from> false decimal-part) ("lux text concat" ".") ("lux text concat" (<from> true whole-part)) @@ -517,8 +518,8 @@ 1.0)] (case ("lux text index" repr "." +0) (#;Some split-index) - (let [whole-part ("lux text clip" repr (if (f.= -1.0 sign) +1 +0) split-index) - decimal-part ("lux text clip" repr (n.inc split-index) ("lux text size" repr)) + (let [whole-part (maybe;assume ("lux text clip" repr (if (f.= -1.0 sign) +1 +0) split-index)) + decimal-part (maybe;assume ("lux text clip" repr (n.inc split-index) ("lux text size" repr))) as-binary (|> (<to> decimal-part) ("lux text concat" ".") ("lux text concat" (<to> whole-part)) @@ -671,13 +672,14 @@ (loop [idx +0 output (make-digits [])] (if (n.< length idx) - (case ("lux text index" "0123456789" (get-char idx input) +0) - #;None - #;None - - (#;Some digit) - (recur (n.inc idx) - (digits-put idx digit output))) + (let [char (maybe;assume (get-char input idx))] + (case ("lux text index" "0123456789" char +0) + #;None + #;None + + (#;Some digit) + (recur (n.inc idx) + (digits-put idx digit output)))) (#;Some output))) #;None))) @@ -741,7 +743,9 @@ false)] (if (and dotted? (n.<= (n.inc bit;width) length)) - (case (text-to-digits ("lux text clip" input +1 length)) + (case (|> ("lux text clip" input +1 length) + maybe;assume + text-to-digits) (#;Some digits) (loop [digits digits idx +0 diff --git a/stdlib/source/lux/data/text.lux b/stdlib/source/lux/data/text.lux index 21a170003..d0f1e6f15 100644 --- a/stdlib/source/lux/data/text.lux +++ b/stdlib/source/lux/data/text.lux @@ -33,14 +33,11 @@ (def: #export (clip from to input) (-> Nat Nat Text (Maybe Text)) - (if (and (n.<= ("lux text size" input) to) - (n.<= to from)) - (#;Some ("lux text clip" input from to)) - #;None)) + ("lux text clip" input from to)) (def: #export (clip' from input) (-> Nat Text (Maybe Text)) - (clip from (size input) input)) + ("lux text clip" input from (size input))) (def: #export (replace-all pattern value template) (-> Text Text Text Text) |