Skip to content

Commit 4f988e2

Browse files
committed
Text.TOML: accept CRLF as newline
1 parent 68263ec commit 4f988e2

File tree

2 files changed

+69
-67
lines changed

2 files changed

+69
-67
lines changed

autoload/vital/__vital__/Text/TOML.vim

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ endfunction
2626
"
2727
" private api
2828
"
29-
" work around: '[^\r\n]*' doesn't work well in old-vim, but "[^\r\n]*" works well
30-
let s:skip_pattern = '\C^\%(\_s\+\|' . "#[^\r\n]*" . '\)'
29+
let s:skip_pattern = '\C^\%(\%(\s\|\r\?\n\)\+\|#[^\r\n]*\)'
3130
let s:bare_key_pattern = '\%([A-Za-z0-9_-]\+\)'
3231

3332
function! s:_skip(input) abort
34-
while s:_match(a:input, '\%(\_s\|#\)')
33+
while s:_match(a:input, '\%(\s\|\r\?\n\|#\)')
3534
let a:input.p = matchend(a:input.text, s:skip_pattern, a:input.p)
3635
endwhile
3736
endfunction
@@ -67,14 +66,10 @@ function! s:_eof(input) abort
6766
endfunction
6867

6968
function! s:_error(input) abort
70-
let buf = []
71-
let offset = 0
72-
while (a:input.p + offset) < a:input.length && a:input.text[a:input.p + offset] !~# "[\r\n]"
73-
let buf += [a:input.text[a:input.p + offset]]
74-
let offset += 1
75-
endwhile
69+
let s = matchstr(a:input.text, s:regex_prefix . '.\{-}\ze\%(\r\?\n\|$\)', a:input.p)
70+
let s = substitute(s, '\r', '\\r', 'g')
7671

77-
throw printf("vital: Text.TOML: Illegal toml format at `%s'.", join(buf, ''))
72+
throw printf("vital: Text.TOML: Illegal toml format at `%s'.", s)
7873
endfunction
7974

8075
function! s:_parse(input) abort
@@ -162,8 +157,8 @@ endfunction
162157
function! s:_multiline_basic_string(input) abort
163158
let s = s:_consume(a:input, '"\{3}\%(\\.\|\_.\)\{-}"\{3}')
164159
let s = s[3 : -4]
165-
let s = substitute(s, "^\n", '', '')
166-
let s = substitute(s, '\\' . "\n" . '\_s*', '', 'g')
160+
let s = substitute(s, '^\r\?\n', '', '')
161+
let s = substitute(s, '\\\%(\s\|\r\?\n\)*', '', 'g')
167162
return s:_unescape(s)
168163
endfunction
169164

@@ -175,7 +170,7 @@ endfunction
175170
function! s:_multiline_literal(input) abort
176171
let s = s:_consume(a:input, "'\\{3}.\\{-}'\\{3}")
177172
let s = s[3 : -4]
178-
let s = substitute(s, "^\n", '', '')
173+
let s = substitute(s, '^\r\?\n', '', '')
179174
return s
180175
endfunction
181176

test/Text/TOML.vim

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -75,48 +75,53 @@ function! s:suite.__parse__()
7575
let multiline_basic_strings = themis#suite('Multi-line basic strings')
7676

7777
function! multiline_basic_strings.trims_first_newline()
78-
let data = s:TOML.parse(join([
79-
\ 'hoge="""',
80-
\ 'One',
81-
\ 'Two"""',
82-
\], "\n"))
83-
84-
call s:assert.same(data.hoge, "One\nTwo")
78+
for newline in ["\n", "\r\n"]
79+
let data = s:TOML.parse(join([
80+
\ 'str1 = """',
81+
\ 'Roses are red',
82+
\ 'Violets are blue"""',
83+
\], newline))
84+
85+
call s:assert.same(data.str1, join([
86+
\ 'Roses are red',
87+
\ 'Violets are blue'
88+
\], newline))
89+
endfor
8590
endfunction
8691

8792
function! multiline_basic_strings.trims_whitespaces_after_backslash()
88-
let data = s:TOML.parse(join([
89-
\ 'hoge= """',
90-
\ 'The quick brown \',
91-
\ '',
92-
\ '',
93-
\ ' fox jumps over \',
94-
\ ' the lazy dog."""',
95-
\], "\n"))
96-
97-
call s:assert.same(data.hoge, 'The quick brown fox jumps over the lazy dog.')
98-
endfunction
99-
100-
function! multiline_basic_strings.trims_whitespaces_after_backslash2()
101-
let data = s:TOML.parse(join([
102-
\ 'hoge = """\',
103-
\ ' The quick brown \',
104-
\ ' fox jumps over \',
105-
\ ' the lazy dog.\',
106-
\ ' """',
107-
\], "\n"))
108-
109-
call s:assert.same(data.hoge, 'The quick brown fox jumps over the lazy dog.')
93+
for newline in ["\n", "\r\n"]
94+
let data = s:TOML.parse(join([
95+
\ 'str2 = """',
96+
\ 'The quick brown \',
97+
\ '',
98+
\ '',
99+
\ ' fox jumps over \',
100+
\ ' the lazy dog."""',
101+
\ 'str3 = """\',
102+
\ ' The quick brown \',
103+
\ ' fox jumps over \',
104+
\ ' the lazy dog.\',
105+
\ ' """',
106+
\], newline))
107+
108+
call s:assert.same(data.str2, 'The quick brown fox jumps over the lazy dog.')
109+
call s:assert.same(data.str3, 'The quick brown fox jumps over the lazy dog.')
110+
endfor
110111
endfunction
111112

112113
function! multiline_basic_strings.includes_escaped_character()
113-
let data = s:TOML.parse(join([
114-
\ 'hoge = """\',
115-
\ 'delimiter = ''\"""''\',
116-
\ '"""',
117-
\], "\n"))
118-
119-
call s:assert.same(data.hoge, 'delimiter = ''"""''')
114+
for newline in ["\n", "\r\n"]
115+
let data = s:TOML.parse(join([
116+
\ 'str4 = """Here are two quotation marks: "". Simple enough."""',
117+
\ 'str5 = """Here are three quotation marks: ""\"."""',
118+
\ 'str6 = """Here are fifteen quotation marks: ""\"""\"""\"""\"""\"."""',
119+
\], newline))
120+
121+
call s:assert.same(data.str4, 'Here are two quotation marks: "". Simple enough.')
122+
call s:assert.same(data.str5, 'Here are three quotation marks: """.')
123+
call s:assert.same(data.str6, 'Here are fifteen quotation marks: """"""""""""""".')
124+
endfor
120125
endfunction
121126
endfunction
122127

@@ -135,24 +140,26 @@ function! s:suite.__parse__()
135140
endfunction
136141

137142
function! parse.multiline_literal_string()
138-
let data = s:TOML.parse(join([
139-
\ 'regex2 = ''''''I [dw]on''t need \d{2} apples''''''',
140-
\ 'lines = ''''''',
141-
\ 'The first newline is',
142-
\ 'trimmed in raw strings.',
143-
\ ' All other whitespace',
144-
\ ' is preserved.',
145-
\ '''''''',
146-
\], "\n"))
147-
148-
call s:assert.same(data.regex2, 'I [dw]on''t need \d{2} apples')
149-
call s:assert.same(data.lines, join([
150-
\ 'The first newline is',
151-
\ 'trimmed in raw strings.',
152-
\ ' All other whitespace',
153-
\ ' is preserved.',
154-
\ '',
155-
\], "\n"))
143+
for newline in ["\n", "\r\n"]
144+
let data = s:TOML.parse(join([
145+
\ 'regex2 = ''''''I [dw]on''t need \d{2} apples''''''',
146+
\ 'lines = ''''''',
147+
\ 'The first newline is',
148+
\ 'trimmed in raw strings.',
149+
\ ' All other whitespace',
150+
\ ' is preserved.',
151+
\ '''''''',
152+
\], newline))
153+
154+
call s:assert.same(data.regex2, 'I [dw]on''t need \d{2} apples')
155+
call s:assert.same(data.lines, join([
156+
\ 'The first newline is',
157+
\ 'trimmed in raw strings.',
158+
\ ' All other whitespace',
159+
\ ' is preserved.',
160+
\ '',
161+
\], newline))
162+
endfor
156163
endfunction
157164

158165
function! parse.integer()

0 commit comments

Comments
 (0)