aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/text/encoding/utf8.lux
blob: 9b13114a4763b8017e152aa9cf4db535e8315af5 (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
(.using
 [library
  [lux "*"
   ["@" target]
   ["[0]" ffi]
   [abstract
    [codec {"+" Codec}]]
   [control
    ["[0]" try {"+" Try}]]
   [data
    ["[0]" binary {"+" Binary}]]]]
 ["[0]" //])

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

       @.js
       (as_is (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
       (as_is (ffi.import: String
                "[1]::[0]"
                (encode [Text] String)
                (force_encoding [Text] Text)
                (bytes [] Binary))

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

       @.php
       (as_is (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
       (as_is (ffi.import: (string->utf8 [Text] Binary))
              (ffi.import: (utf8->string [Binary] Text)))
       (as_is)))

(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" (:expected ("python constant" "bytearray")) [value "utf-8"]))

       @.lua
       ("lua utf8 encode" 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" (:expected value) ["utf-8"])))

         @.lua
         {try.#Success ("lua utf8 decode" 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}))))

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