aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/text/encoding.lux
diff options
context:
space:
mode:
authorEduardo Julian2021-01-26 19:11:14 -0400
committerEduardo Julian2021-01-26 19:11:14 -0400
commit43d28326ad59c74439b96343cc8f619ed7d90231 (patch)
tree3f410d48bfca7d8141e2e7fda0f8d978934dd5c2 /stdlib/source/lux/data/text/encoding.lux
parentae56acf791c2ed9bd5865f85fffa00b025d310fe (diff)
Made the Python being generated more compatible with both P2.7 and P3.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/data/text/encoding.lux53
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)