diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/text/encoding.lux | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/stdlib/source/lux/data/text/encoding.lux b/stdlib/source/lux/data/text/encoding.lux index df1714484..2050cbc8c 100644 --- a/stdlib/source/lux/data/text/encoding.lux +++ b/stdlib/source/lux/data/text/encoding.lux @@ -173,11 +173,8 @@ ["#::." (new [[byte] java/lang/String]) (getBytes [java/lang/String] [byte])]))] - (for {@.old - (as_is <jvm>) - - @.jvm - (as_is <jvm>) + (for {@.old (as_is <jvm>) + @.jvm (as_is <jvm>) @.js (as_is (host.import: Uint8Array) @@ -195,12 +192,14 @@ (host.import: TextDecoder (new [host.String]) - (decode [Uint8Array] host.String)))})) + (decode [Uint8Array] host.String)))} + (as_is))) (def: (to_utf8 value) (-> Text Binary) (for {@.old (java/lang/String::getBytes (..name ..utf_8) + ## TODO: Remove coercion below. ## The coercion below may seem ## gratuitous, but removing it ## causes a grave compilation problem. @@ -222,31 +221,35 @@ ## On the browser (|> (TextEncoder::new [(..name ..utf_8)]) (TextEncoder::encode [value])) - )})) + ) + + @.python + (:coerce Binary ("python apply" (:assume ("python constant" "bytearray")) value "utf-8"))})) (def: (from_utf8 value) (-> Binary (Try Text)) - (for {@.old - (#try.Success (java/lang/String::new value (..name ..utf_8))) + (with_expansions [<jvm> (#try.Success (java/lang/String::new value (..name ..utf_8)))] + (for {@.old <jvm> + @.jvm <jvm> - @.jvm - (#try.Success (java/lang/String::new value (..name ..utf_8))) + @.js + (cond host.on_nashorn? + (|> ("js object new" ("js constant" "java.lang.String") [value "utf8"]) + (:coerce Text) + #try.Success) - @.js - (cond host.on_nashorn? - (|> ("js object new" ("js constant" "java.lang.String") [value "utf8"]) - (:coerce Text) - #try.Success) + host.on_node_js? + (|> (Buffer::from|decode [value]) + (Buffer::toString ["utf8"]) + #try.Success) + + ## On the browser + (|> (TextDecoder::new [(..name ..utf_8)]) + (TextDecoder::decode [value]) + #try.Success)) - host.on_node_js? - (|> (Buffer::from|decode [value]) - (Buffer::toString ["utf8"]) - #try.Success) - - ## On the browser - (|> (TextDecoder::new [(..name ..utf_8)]) - (TextDecoder::decode [value]) - #try.Success))})) + @.python + (host.try (:coerce Text ("python object do" "decode" (:assume value) "utf-8")))}))) (structure: #export utf8 (Codec Binary Text) |