summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/marshal.nim50
1 files changed, 23 insertions, 27 deletions
diff --git a/lib/pure/marshal.nim b/lib/pure/marshal.nim
index 701dff88c..36e6cf52f 100644
--- a/lib/pure/marshal.nim
+++ b/lib/pure/marshal.nim
@@ -31,35 +31,11 @@
 ##
 ## **Note**: The ``to`` and ``$$`` operations are available at compile-time!
 
-import streams, typeinfo, json, intsets, tables
+import streams, typeinfo, json, intsets, tables, unicode
 
 proc ptrToInt(x: pointer): int {.inline.} =
   result = cast[int](x) # don't skip alignment
 
-proc binaryToUtf8(s: string): string =
-  ## converts binary data to valid UTF-8 string
-  result = newStringOfCap(s.len)
-  for c in s:
-    let code = ord(c)
-    if code > 0x7F:
-      result.add(chr(0xC0 or (code shr 6)))
-      result.add(chr(0x80 or (code and 0x3F)))
-    else:
-      result.add(c)
-
-proc utf8ToBinary(s: string): string =
-  result = newStringOfCap(s.len)
-  var i = 0
-  while i < s.len:
-    var code = ord(s[i])
-    if code > 127:
-      if (code and 0xE0) != 0xC0 or (code and 0x1F) > 3 or i + 1 == s.len:
-        raise newException(ValueError, "invalid binary encoding")
-      code = ((code and 0x1F) shl 6) or (ord(s[i + 1]) and 0x3F)
-      inc i
-    result.add(chr(code))
-    inc i
-
 proc storeAny(s: Stream, a: Any, stored: var IntSet) =
   case a.kind
   of akNone: assert false
@@ -116,7 +92,15 @@ proc storeAny(s: Stream, a: Any, stored: var IntSet) =
   of akString:
     var x = getString(a)
     if isNil(x): s.write("null")
-    else: s.write(escapeJson(binaryToUtf8(x)))
+    elif x.validateUtf8() == -1: s.write(escapeJson(x))
+    else:
+      s.write("[")
+      var i = 0
+      for c in x:
+        if i > 0: s.write(", ")
+        s.write($ord(c))
+        inc(i)
+      s.write("]")
   of akInt..akInt64, akUInt..akUInt64: s.write($getBiggestInt(a))
   of akFloat..akFloat128: s.write($getBiggestFloat(a))
 
@@ -229,8 +213,20 @@ proc loadAny(p: var JsonParser, a: Any, t: var Table[BiggestInt, pointer]) =
       setPointer(a, nil)
       next(p)
     of jsonString:
-      setString(a, utf8ToBinary(p.str))
+      setString(a, p.str)
+      next(p)
+    of jsonArrayStart:
       next(p)
+      var str = ""
+      while p.kind == jsonInt:
+        let code = p.getInt()
+        if code < 0 or code > 255:
+          raiseParseErr(p, "invalid charcode: " & $code)
+        str.add(chr(code))
+        next(p)
+      if p.kind == jsonArrayEnd: next(p)
+      else: raiseParseErr(p, "an array of charcodes expected for string")
+      setString(a, str)
     else: raiseParseErr(p, "string expected")
   of akInt..akInt64, akUInt..akUInt64:
     if p.kind == jsonInt:
s='oid'>9344e9fbf ^
11b695875 ^


9344e9fbf ^


11b695875 ^
9344e9fbf ^

11b695875 ^


9344e9fbf ^
























11b695875 ^
69bc07793 ^
9344e9fbf ^

69bc07793 ^
9344e9fbf ^


























































































































































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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270