aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/text/encoding/utf8.lux
blob: dd0f6bc4e36d2f5bc062e5081a6abbc036c83ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
... 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 https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   ["[0]" ffi]
   [abstract
    [codec (.only Codec)]]
   [control
    ["[0]" try (.only Try)]]
   [data
    ["[0]" binary (.only Binary)]]
   [meta
    [compiler
     ["@" target]]]]]
 ["[0]" //])

(with_expansions [<jvm> (these (ffi.import java/lang/String
                                 "[1]::[0]"
                                 (new [[byte] java/lang/String])
                                 (getBytes [java/lang/String] [byte])))]
  (for @.old (these <jvm>)
       @.jvm (these <jvm>)

       @.js
       (these (ffi.import Uint8Array
                "[1]::[0]")

              ... On Node
              (ffi.import Buffer
                "[1]::[0]"
                ("static" from "as" from|encoded [ffi.String ffi.String] Buffer)
                ("static" from "as" from|decoded [Uint8Array] Buffer)
                (toString [ffi.String] ffi.String))

              ... On the browser
              (ffi.import TextEncoder
                "[1]::[0]"
                (new [ffi.String])
                (encode [ffi.String] Uint8Array))
              
              (ffi.import TextDecoder
                "[1]::[0]"
                (new [ffi.String])
                (decode [Uint8Array] ffi.String)))

       @.ruby
       (these (ffi.import String
                "[1]::[0]"
                (encode [Text] String)
                (force_encoding [Text] Text)
                (bytes [] Binary))

              (ffi.import Array
                "[1]::[0]"
                (pack [Text] String)))

       @.php
       (these (ffi.import Almost_Binary)
              (ffi.import (unpack [ffi.String ffi.String] Almost_Binary))
              (ffi.import (array_values [Almost_Binary] Binary))
              (def php_byte_array_format "C*"))

       @.scheme
       ... https://srfi.schemers.org/srfi-140/srfi-140.html
       (these (ffi.import (string->utf8 [Text] Binary))
              (ffi.import (utf8->string [Binary] Text)))
       (these)))

(def (encoded value)
  (-> Text Binary)
  (for @.old
       (java/lang/String::getBytes (ffi.as_string (//.name //.utf_8))
                                   (ffi.as_string value))

       @.jvm
       (java/lang/String::getBytes (ffi.as_string (//.name //.utf_8))
                                   (ffi.as_string value))

       @.js
       (cond ffi.on_nashorn?
             (as Binary (.js_object_do# "getBytes" value ["utf8"]))
             
             ffi.on_node_js?
             (|> (Buffer::from|encoded value "utf8")
                 ... This coercion is valid as per NodeJS's documentation:
                 ... https://nodejs.org/api/buffer.html#buffer_buffers_and_typedarrays
                 (as Uint8Array))
             
             ... On the browser
             (|> (TextEncoder::new (//.name //.utf_8))
                 (TextEncoder::encode [value]))
             )

       @.python
       (as Binary (.python_apply# (as_expected (.python_constant# "bytearray")) [value "utf-8"]))

       @.lua
       (.lua_utf8_encoded# value)

       @.ruby
       (|> value
           (as String)
           (String::encode "UTF-8")
           (String::bytes))

       @.php
       (|> (..unpack [..php_byte_array_format value])
           ..array_values
           ("php object new" "ArrayObject")
           (as Binary))

       @.scheme
       (..string->utf8 value)))

(def (decoded value)
  (-> Binary (Try Text))
  (with_expansions [<jvm> {try.#Success (ffi.of_string (java/lang/String::new value (ffi.as_string (//.name //.utf_8))))}]
    (for @.old <jvm>
         @.jvm <jvm>

         @.js
         (cond ffi.on_nashorn?
               (|> (.js_object_new# (.js_constant# "java.lang.String") [value "utf8"])
                   (as Text)
                   {try.#Success})

               ffi.on_node_js?
               (|> (Buffer::from|decoded value)
                   (Buffer::toString "utf8")
                   {try.#Success})
               
               ... On the browser
               (|> (TextDecoder::new (//.name //.utf_8))
                   (TextDecoder::decode value)
                   {try.#Success}))

         @.python
         (try (as Text (.python_object_do# "decode" (as_expected value) ["utf-8"])))

         @.lua
         {try.#Success (.lua_utf8_decoded# value)}

         @.ruby
         (|> value
             (as Array)
             (Array::pack "C*")
             (as String)
             (String::force_encoding "UTF-8")
             {try.#Success})

         @.php
         (|> value
             ("php pack" ..php_byte_array_format)
             {try.#Success})

         @.scheme
         (|> value
             ..utf8->string
             {try.#Success}))))

(def .public codec
  (Codec Binary Text)
  (implementation
   (def encoded ..encoded)
   (def decoded ..decoded)))