11let s: save_cpo = &cpo
22set cpo &vim
33
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+
441function ! s: _true () abort
542 return 1
643endfunction
@@ -26,7 +63,6 @@ function! s:_resolve(val, prefix) abort
2663 return a: val
2764endfunction
2865
29-
3066function ! s: _vital_created (module) abort
3167 " define constant variables
3268 if ! exists (' s:const' )
@@ -59,6 +95,10 @@ function! s:decode(json, ...) abort
5995 let json = join (split (json, " \n " ), ' ' )
6096 let json = substitute (json, ' \\u34;' , ' \\"' , ' g' )
6197 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' )
62102 if settings.use_token
63103 let prefix = ' __Web.JSON__'
64104 while stridx (json, prefix) != -1
@@ -83,11 +123,9 @@ function! s:encode(val, ...) abort
83123 if type (a: val ) == 0
84124 return a: val
85125 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 . ' "'
91129 elseif type (a: val ) == 2
92130 if s: const .true == a: val
93131 return ' true'
0 commit comments