From a3b9b19231047ec6da8decfc7d45db0598622651 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 14 Nov 2017 20:39:03 -0400 Subject: - Made "lux text clip" work like it used to. --- stdlib/source/lux.lux | 10 ++---- stdlib/source/lux/data/number.lux | 66 +++++++++++++++++++++------------------ stdlib/source/lux/data/text.lux | 7 ++--- 3 files changed, 39 insertions(+), 44 deletions(-) (limited to 'stdlib') 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 [ ] @@ -181,7 +181,7 @@ (def: (encode value) (loop [input value output ""] - (let [digit (get-char (n.% input) ) + (let [digit (maybe;assume (get-char (n.% input))) output' ("lux text concat" digit output) input' (n./ 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" digit +0) #;None (#E;Error ("lux text concat" repr)) @@ -226,19 +226,20 @@ "-" "")] (loop [input (|> value (i./ ) (:: Number abs)) - output (get-char (|> value (i.% ) (:: Number abs) int-to-nat) - )] + output (|> value (i.% ) (:: Number abs) int-to-nat + (get-char ) + maybe;assume)] (if (i.= 0 input) ("lux text concat" sign output) - (let [digit (get-char (int-to-nat (i.% input)) )] + (let [digit (maybe;assume (get-char (int-to-nat (i.% input))))] (recur (i./ 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" digit +0) #;None (#E;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: #export (Codec Text Deg) @@ -314,8 +315,8 @@ (if (f.= 0.0 dec-left) ("lux text concat" "." output) (let [shifted (f.* dec-left) - digit (get-char (|> shifted (f.% ) frac-to-int int-to-nat) - )] + digit (|> shifted (f.% ) frac-to-int int-to-nat + (get-char ) 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 [(:: decode whole-part) (:: 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 signum value) raw-bin (:: Binary@Codec 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 (|> ( false decimal-part) ("lux text concat" ".") ("lux text concat" ( 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 (|> ( decimal-part) ("lux text concat" ".") ("lux text concat" ( 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) -- cgit v1.2.3