File tree Expand file tree Collapse file tree 2 files changed +50
-5
lines changed
autoload/vital/__vital__/Web Expand file tree Collapse file tree 2 files changed +50
-5
lines changed Original file line number Diff line number Diff line change 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
@@ -83,11 +120,9 @@ function! s:encode(val, ...) abort
83
120
if type (a: val ) == 0
84
121
return a: val
85
122
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' )
123
+ let s = substitute (a: val , ' [\x01-\x1f\\"]' , ' \=s:control_chars[submatch(0)]' , ' g' )
124
+ let s = iconv (s , &encoding , ' utf-8' )
125
+ return ' "' . s . ' "'
91
126
elseif type (a: val ) == 2
92
127
if s: const .true == a: val
93
128
return ' true'
Original file line number Diff line number Diff line change @@ -29,6 +29,11 @@ Describe Web.JSON
29
29
\ JSON.decode('"He said \"I''m a vimmer\""'),
30
30
\ 'He said "I''m a vimmer"'
31
31
\)
32
+ " control chars
33
+ Assert Equals(JSON.decode('"\u0001\u0002\u0003\u0004\u0005\u0006\u0007"'), "\x01\x02\x03\x04\x05\x06\x07")
34
+ Assert Equals(JSON.decode('"\b\t\n\u000b\f\r\u000e\u000f"'), "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f")
35
+ Assert Equals(JSON.decode('"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"'), "\x10\x11\x12\x13\x14\x15\x16\x17")
36
+ Assert Equals(JSON.decode('"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"'), "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f")
32
37
" there should be iconv tests as well
33
38
End
34
39
@@ -91,6 +96,11 @@ Describe Web.JSON
91
96
\ JSON.encode('He said "I''m a vimmer"'),
92
97
\ '"He said \"I''m a vimmer\""'
93
98
\)
99
+ " control chars
100
+ Assert Equals(JSON.encode("\x01\x02\x03\x04\x05\x06\x07"), '"\u0001\u0002\u0003\u0004\u0005\u0006\u0007"')
101
+ Assert Equals(JSON.encode("\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"), '"\b\t\n\u000b\f\r\u000e\u000f"')
102
+ Assert Equals(JSON.encode("\x10\x11\x12\x13\x14\x15\x16\x17"), '"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"')
103
+ Assert Equals(JSON.encode("\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"), '"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"')
94
104
" there should be iconv tests as well
95
105
End
96
106
You can’t perform that action at this time.
0 commit comments