Skip to content

Commit a063f6c

Browse files
committed
cosmetic changes
1 parent b4e0f60 commit a063f6c

File tree

3 files changed

+128
-91
lines changed

3 files changed

+128
-91
lines changed

src/lunajson/decoder.lua

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local byte, char, find, gsub, match, sub =
99

1010
local _ENV = nil
1111

12+
1213
local function newdecoder()
1314
local json, pos, nullv, arraylen
1415

@@ -62,8 +63,9 @@ local function newdecoder()
6263

6364
--[[
6465
Numbers
65-
Conceptually, the longest prefix that matches to `-?(0|[1-9][0-9]*)(\.[0-9]*)?([eE][+-]?[0-9]*)?`
66-
(in regexp) is captured as a number and its conformance to the JSON spec is checked.
66+
Conceptually, the longest prefix that matches to
67+
`-?(0|[1-9][0-9]*)(\.[0-9]*)?([eE][+-]?[0-9]*)?` (in regexp) is
68+
captured as a number and its conformance to the JSON spec is checked.
6769
--]]
6870
-- deal with non-standard locales
6971
local radixmark = match(tostring(0.5), '[^0-9]')
@@ -91,8 +93,8 @@ local function newdecoder()
9193
return error_number()
9294
end
9395

94-
if c == 0x2E then -- is this `.`?
95-
num = match(json, '^.[0-9]*', pos) -- skipping 0
96+
if c == 0x2E then -- is this `.`?
97+
num = match(json, '^.[0-9]*', pos) -- skipping 0
9698
c = #num
9799
if c == 1 then
98100
return error_number()
@@ -101,14 +103,14 @@ local function newdecoder()
101103
c = byte(json, postmp)
102104
end
103105

104-
if c == 0x45 or c == 0x65 then -- is this e or E?
106+
if c == 0x45 or c == 0x65 then -- is this e or E?
105107
c = match(json, '^[^eE]*[eE][-+]?[0-9]+', pos)
106108
if not c then
107109
return error_number()
108110
end
109111
if num then
110112
num = c
111-
else -- `0e.*` is always 0.0
113+
else -- `0e.*` is always 0.0
112114
numret = 0.0
113115
end
114116
postmp = pos + #c
@@ -128,13 +130,13 @@ local function newdecoder()
128130
local function f_num(mns)
129131
pos = pos-1
130132
local num = match(json, '^.[0-9]*%.?[0-9]*', pos)
131-
if byte(num, -1) == 0x2E then
133+
if byte(num, -1) == 0x2E then -- `.`?
132134
return error_number()
133135
end
134136
local postmp = pos + #num
135137
local c = byte(json, postmp)
136138

137-
if c == 0x45 or c == 0x65 then -- e or E?
139+
if c == 0x45 or c == 0x65 then -- e or E?
138140
num = match(json, '^[^eE]*[eE][-+]?[0-9]+', pos)
139141
if not num then
140142
return error_number()
@@ -177,10 +179,13 @@ local function newdecoder()
177179
Strings
178180
--]]
179181
local f_str_hextbl = {
180-
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, inf, inf, inf, inf, inf, inf,
181-
inf, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, inf, inf, inf, inf, inf, inf, inf, inf, inf,
182-
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
183-
inf, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, inf, inf, inf, inf, inf, inf, inf, inf, inf,
182+
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
183+
0x8, 0x9, inf, inf, inf, inf, inf, inf,
184+
inf, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, inf,
185+
inf, inf, inf, inf, inf, inf, inf, inf,
186+
inf, inf, inf, inf, inf, inf, inf, inf,
187+
inf, inf, inf, inf, inf, inf, inf, inf,
188+
inf, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
184189
}
185190
f_str_hextbl.__index = function()
186191
return inf
@@ -211,12 +216,12 @@ local function newdecoder()
211216
f_str_hextbl[c3-47] * 0x10 +
212217
f_str_hextbl[c4-47]
213218
if ucode ~= inf then
214-
if ucode < 0x80 then -- 1byte
219+
if ucode < 0x80 then -- 1byte
215220
if rest then
216221
return char(ucode, rest)
217222
end
218223
return char(ucode)
219-
elseif ucode < 0x800 then -- 2byte
224+
elseif ucode < 0x800 then -- 2bytes
220225
c1 = floor(ucode / 0x40)
221226
c2 = ucode - c1 * 0x40
222227
c1 = c1 + 0xC0
@@ -225,7 +230,7 @@ local function newdecoder()
225230
return char(c1, c2, rest)
226231
end
227232
return char(c1, c2)
228-
elseif ucode < 0xD800 or 0xE000 <= ucode then -- 3byte
233+
elseif ucode < 0xD800 or 0xE000 <= ucode then -- 3bytes
229234
c1 = floor(ucode / 0x1000)
230235
ucode = ucode - c1 * 0x1000
231236
c2 = floor(ucode / 0x40)
@@ -237,7 +242,7 @@ local function newdecoder()
237242
return char(c1, c2, c3, rest)
238243
end
239244
return char(c1, c2, c3)
240-
elseif 0xD800 <= ucode and ucode < 0xDC00 then -- surrogate pair 1st
245+
elseif 0xD800 <= ucode and ucode < 0xDC00 then -- surrogate pair 1st
241246
if f_str_surrogate_prev == 0 then
242247
f_str_surrogate_prev = ucode
243248
if not rest then
@@ -247,7 +252,7 @@ local function newdecoder()
247252
end
248253
f_str_surrogate_prev = 0
249254
decodeerror("two contiguous 1st surrogate pair bytes")
250-
else -- surrogate pair 2nd
255+
else -- surrogate pair 2nd
251256
if f_str_surrogate_prev ~= 0 then
252257
ucode = 0x10000 +
253258
(f_str_surrogate_prev - 0xD800) * 0x400 +
@@ -288,38 +293,43 @@ local function newdecoder()
288293
local pos2 = pos
289294
local c1, c2
290295
repeat
291-
newpos = find(json, '"', pos2, true) -- search '"'
296+
newpos = find(json, '"', pos2, true) -- search '"'
292297
if not newpos then
293298
decodeerror("unterminated string")
294299
end
295300
pos2 = newpos+1
296-
while true do -- skip preceding '\\'s
301+
while true do -- skip preceding '\\'s
297302
c1, c2 = byte(json, newpos-2, newpos-1)
298303
if c2 ~= 0x5C or c1 ~= 0x5C then
299304
break
300305
end
301306
newpos = newpos-2
302307
end
303-
until c2 ~= 0x5C -- check '"' is not preceded by '\'
308+
until c2 ~= 0x5C -- leave if '"' is not preceded by '\'
304309

305310
local str = sub(json, pos, pos2-2)
306311
pos = pos2
307312

308-
if iskey then -- check key cache
313+
if iskey then -- check key cache
309314
local str2 = f_str_keycache[str]
310315
if str2 then
311316
return str2
312317
end
313318
end
314319
local str2 = str
315-
if find(str2, '\\', 1, true) then -- check if backslash occurs
316-
str2 = gsub(str2, '\\(.)([^\\]?[^\\]?[^\\]?[^\\]?[^\\]?)', f_str_subst) -- interpret escapes
320+
if find(str2, '\\', 1, true) then -- check whether a backslash exists
321+
-- We need to grab 4 characters after the escape char,
322+
-- for encoding unicode codepoint to UTF-8.
323+
-- As we need to ensure that every first surrogate pair byte is
324+
-- immediately followed by second one, we grab upto 5 characters and
325+
-- check the last for this purpose.
326+
str2 = gsub(str2, '\\(.)([^\\]?[^\\]?[^\\]?[^\\]?[^\\]?)', f_str_subst)
317327
if f_str_surrogate_prev ~= 0 then
318328
f_str_surrogate_prev = 0
319329
decodeerror("1st surrogate pair byte not continued by 2nd")
320330
end
321331
end
322-
if iskey then -- commit key cache
332+
if iskey then -- commit key cache
323333
f_str_keycache[str] = str2
324334
end
325335
return str2
@@ -336,17 +346,17 @@ local function newdecoder()
336346
pos = pos+1
337347

338348
local i = 0
339-
if byte(json, pos) ~= 0x5D then -- check closing bracket ']', that consists an empty array
349+
if byte(json, pos) ~= 0x5D then -- check closing bracket ']' which means the array empty
340350
local newpos = pos-1
341351
repeat
342352
i = i+1
343-
f = dispatcher[byte(json,newpos+1)] -- parse value
353+
f = dispatcher[byte(json,newpos+1)] -- parse value
344354
pos = newpos+2
345355
ary[i] = f()
346-
f, newpos = find(json, '^[ \n\r\t]*,[ \n\r\t]*', pos) -- check comma
356+
f, newpos = find(json, '^[ \n\r\t]*,[ \n\r\t]*', pos) -- check comma
347357
until not newpos
348358

349-
f, newpos = find(json, '^[ \n\r\t]*%]', pos) -- check closing bracket
359+
f, newpos = find(json, '^[ \n\r\t]*%]', pos) -- check closing bracket
350360
if not newpos then
351361
decodeerror("no closing bracket of an array")
352362
end
@@ -366,16 +376,16 @@ local function newdecoder()
366376

367377
f, pos = find(json, '^[ \n\r\t]*', pos)
368378
pos = pos+1
369-
if byte(json, pos) ~= 0x7D then -- check the closing bracket '}', that consists an empty object
379+
if byte(json, pos) ~= 0x7D then -- check closing bracket '}' which means the object empty
370380
local newpos = pos-1
371381

372382
repeat
373383
pos = newpos+1
374-
if byte(json, pos) ~= 0x22 then -- check '"'
384+
if byte(json, pos) ~= 0x22 then -- check '"'
375385
decodeerror("not key")
376386
end
377387
pos = pos+1
378-
local key = f_str(true) -- parse key
388+
local key = f_str(true) -- parse key
379389

380390
-- optimized for compact json
381391
-- c1, c2 == ':', <the first char of the value> or
@@ -392,13 +402,13 @@ local function newdecoder()
392402
f = dispatcher[c2]
393403
end
394404
end
395-
if f == f_err then -- read a colon and arbitrary number of spaces
405+
if f == f_err then -- read a colon and arbitrary number of spaces
396406
f, newpos = find(json, '^[ \n\r\t]*:[ \n\r\t]*', pos)
397407
if not newpos then
398408
decodeerror("no colon after a key")
399409
end
400410
end
401-
f = dispatcher[byte(json, newpos+1)] -- parse value
411+
f = dispatcher[byte(json, newpos+1)] -- parse value
402412
pos = newpos+2
403413
obj[key] = f()
404414
f, newpos = find(json, '^[ \n\r\t]*,[ \n\r\t]*', pos)
@@ -416,18 +426,27 @@ local function newdecoder()
416426
end
417427

418428
--[[
419-
The jump table to dispatch a parser for a value, indexed by the code of the value's first char.
429+
The jump table to dispatch a parser for a value,
430+
indexed by the code of the value's first char.
420431
Nil key means the end of json.
421432
--]]
422433
dispatcher = {
423-
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
424-
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
425-
f_err, f_err, f_str, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_mns, f_err, f_err,
426-
f_zro, f_num, f_num, f_num, f_num, f_num, f_num, f_num, f_num, f_num, f_err, f_err, f_err, f_err, f_err, f_err,
427-
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
428-
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_ary, f_err, f_err, f_err, f_err,
429-
f_err, f_err, f_err, f_err, f_err, f_err, f_fls, f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_nul, f_err,
430-
f_err, f_err, f_err, f_err, f_tru, f_err, f_err, f_err, f_err, f_err, f_err, f_obj, f_err, f_err, f_err, f_err,
434+
f_err, f_err, f_err, f_err, f_err, f_err, f_err,
435+
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
436+
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
437+
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
438+
f_err, f_err, f_str, f_err, f_err, f_err, f_err, f_err,
439+
f_err, f_err, f_err, f_err, f_err, f_mns, f_err, f_err,
440+
f_zro, f_num, f_num, f_num, f_num, f_num, f_num, f_num,
441+
f_num, f_num, f_err, f_err, f_err, f_err, f_err, f_err,
442+
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
443+
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
444+
f_err, f_err, f_err, f_err, f_err, f_err, f_err, f_err,
445+
f_err, f_err, f_err, f_ary, f_err, f_err, f_err, f_err,
446+
f_err, f_err, f_err, f_err, f_err, f_err, f_fls, f_err,
447+
f_err, f_err, f_err, f_err, f_err, f_err, f_nul, f_err,
448+
f_err, f_err, f_err, f_err, f_tru, f_err, f_err, f_err,
449+
f_err, f_err, f_err, f_obj, f_err, f_err, f_err, f_err,
431450
}
432451
dispatcher[0] = f_err
433452
dispatcher.__index = function()

src/lunajson/encoder.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ end
1616

1717
local _ENV = nil
1818

19+
1920
local function newencoder()
2021
local v, nullv
2122
local i, builder, visited

0 commit comments

Comments
 (0)