1
1
let s: save_cpo = &cpo
2
2
set cpo &vim
3
3
4
+ let s: control_chars = {
5
+ \ ' \' : ' \\' ,
6
+ \ ' "' : ' \"' ,
7
+ \ " \x01 " : ' \u0001' ,
8
+ \ " \x02 " : ' \u0002' ,
9
+ \ " \x03 " : ' \u0003' ,
10
+ \ " \x04 " : ' \u0004' ,
11
+ \ " \x05 " : ' \u0005' ,
12
+ \ " \x06 " : ' \u0006' ,
13
+ \ " \x07 " : ' \u0007' ,
14
+ \ " \x08 " : ' \b' ,
15
+ \ " \x09 " : ' \t' ,
16
+ \ " \x0a " : ' \n' ,
17
+ \ " \x0b " : ' \u000b' ,
18
+ \ " \x0c " : ' \f' ,
19
+ \ " \x0d " : ' \r' ,
20
+ \ " \x0e " : ' \u000e' ,
21
+ \ " \x0f " : ' \u000f' ,
22
+ \ " \x10 " : ' \u0010' ,
23
+ \ " \x11 " : ' \u0011' ,
24
+ \ " \x12 " : ' \u0012' ,
25
+ \ " \x13 " : ' \u0013' ,
26
+ \ " \x14 " : ' \u0014' ,
27
+ \ " \x15 " : ' \u0015' ,
28
+ \ " \x16 " : ' \u0016' ,
29
+ \ " \x17 " : ' \u0017' ,
30
+ \ " \x18 " : ' \u0018' ,
31
+ \ " \x19 " : ' \u0019' ,
32
+ \ " \x1a " : ' \u001a' ,
33
+ \ " \x1b " : ' \u001b' ,
34
+ \ " \x1c " : ' \u001c' ,
35
+ \ " \x1d " : ' \u001d' ,
36
+ \ " \x1e " : ' \u001e' ,
37
+ \ " \x1f " : ' \u001f' ,
38
+ \ }
39
+ lockvar s: control_chars
40
+
4
41
function ! s: _true () abort
5
42
return 1
6
43
endfunction
@@ -26,7 +63,6 @@ function! s:_resolve(val, prefix) abort
26
63
return a: val
27
64
endfunction
28
65
29
-
30
66
function ! s: _vital_created (module) abort
31
67
" define constant variables
32
68
if ! exists (' s:const' )
@@ -59,6 +95,10 @@ function! s:decode(json, ...) abort
59
95
let json = join (split (json, " \n " ), ' ' )
60
96
let json = substitute (json, ' \\u34;' , ' \\"' , ' g' )
61
97
let json = substitute (json, ' \\u\(\x\x\x\x\)' , ' \=s:string.nr2enc_char("0x".submatch(1))' , ' g' )
98
+ " convert surrogate pair
99
+ let json = substitute (json, ' \([\uD800-\uDBFF]\)\([\uDC00-\uDFFF]\)' ,
100
+ \ ' \=nr2char(0x10000+and(0x7ff,char2nr(submatch(1)))*0x400+and(0x3ff,char2nr(submatch(2))))' ,
101
+ \ ' g' )
62
102
if settings.use_token
63
103
let prefix = ' __Web.JSON__'
64
104
while stridx (json, prefix) != -1
@@ -83,11 +123,9 @@ function! s:encode(val, ...) abort
83
123
if type (a: val ) == 0
84
124
return a: val
85
125
elseif type (a: val ) == 1
86
- let json = ' "' . escape (a: val , ' \"' ) . ' "'
87
- let json = substitute (json, " \r " , ' \\r' , ' g' )
88
- let json = substitute (json, " \n " , ' \\n' , ' g' )
89
- let json = substitute (json, " \t " , ' \\t' , ' g' )
90
- return iconv (json, &encoding , ' utf-8' )
126
+ let s = substitute (a: val , ' [\x01-\x1f\\"]' , ' \=s:control_chars[submatch(0)]' , ' g' )
127
+ let s = iconv (s , &encoding , ' utf-8' )
128
+ return ' "' . s . ' "'
91
129
elseif type (a: val ) == 2
92
130
if s: const .true == a: val
93
131
return ' true'
0 commit comments