Software /
code /
prosody
Comparison
util/json.lua @ 5517:9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Tue, 23 Apr 2013 15:55:49 -0400 |
parent | 5516:9733836629f9 |
child | 5561:52eef11cd8af |
comparison
equal
deleted
inserted
replaced
5516:9733836629f9 | 5517:9d7349bbe4d2 |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
4 -- | |
5 -- utf8char copyright (C) 2007 Rici Lake | |
6 -- | 4 -- |
7 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
8 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
9 -- | 7 -- |
10 | 8 |
39 for i=0,31 do | 37 for i=0,31 do |
40 local ch = s_char(i); | 38 local ch = s_char(i); |
41 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end | 39 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end |
42 end | 40 end |
43 | 41 |
44 local function utf8char(i) | 42 local function codepoint_to_utf8(code) |
45 if i >= 0 then | 43 if code < 0x80 then return s_char(code); end |
46 i = i - i%1 | 44 local bits0_6 = code % 64; |
47 if i < 128 then | 45 if code < 0x800 then |
48 return s_char(i) | 46 local bits6_5 = (code - bits0_6) / 64; |
49 else | 47 return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6); |
50 local c1 = i % 64 | 48 end |
51 i = (i - c1) / 64 | 49 local bits0_12 = code % 4096; |
52 if i < 32 then | 50 local bits6_6 = (bits0_12 - bits0_6) / 64; |
53 return s_char(0xC0+i, 0x80+c1) | 51 local bits12_4 = (code - bits0_12) / 4096; |
54 else | 52 return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6); |
55 local c2 = i % 64 | 53 end |
56 i = (i - c2) / 64 | |
57 if i < 16 and (i ~= 13 or c2 < 32) then | |
58 return s_char(0xE0+i, 0x80+c2, 0x80+c1) | |
59 elseif i >= 16 and i < 0x110 then | |
60 local c3 = i % 64 | |
61 i = (i - c3) / 64 | |
62 return s_char(0xF0+i, 0x80+c3, 0x80+c2, 0x80+c1) | |
63 end | |
64 end | |
65 end | |
66 end | |
67 end | |
68 | |
69 | 54 |
70 local valid_types = { | 55 local valid_types = { |
71 number = true, | 56 number = true, |
72 string = true, | 57 string = true, |
73 table = true, | 58 table = true, |
290 next(); | 275 next(); |
291 if not ch then error("unexpected eof in string"); end | 276 if not ch then error("unexpected eof in string"); end |
292 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end | 277 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end |
293 seq = seq..ch; | 278 seq = seq..ch; |
294 end | 279 end |
295 s = s..utf8char(tonumber(seq, 16)); | 280 s = s..codepoint_to_utf8(tonumber(seq, 16)); |
296 next(); | 281 next(); |
297 else error("invalid escape sequence in string"); end | 282 else error("invalid escape sequence in string"); end |
298 end | 283 end |
299 if ch == "\"" then | 284 if ch == "\"" then |
300 next(); | 285 next(); |