Skip to content

Commit dacc5bf

Browse files
authored
Merge pull request #895 from julia-vscode/valid-utf-check
Add a check for strings with NULL
2 parents 1e1efcd + b6f83cd commit dacc5bf

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/document.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function get_text(doc::Document)
3737
end
3838

3939
function set_text!(doc::Document, text)
40+
# TODO Remove this check eventually
41+
occursin('\0', text) && throw(LSInvalidFile("Tried to set a text with an embedded NULL as the document content."))
4042
doc._content = text
4143
doc._line_offsets = nothing
4244
doc._line_offsets2 = nothing
@@ -127,7 +129,7 @@ function get_offset2(doc::Document, line::Integer, character::Integer)
127129
pos = nextind(text, pos)
128130
end
129131

130-
return pos
132+
return pos
131133
end
132134

133135
# Note: to be removed
@@ -166,7 +168,7 @@ function get_line_offsets(doc::Document, force=false)
166168
doc._line_offsets = Int[0]
167169
text = get_text(doc)
168170
ind = firstindex(text)
169-
while ind <= lastindex(text)
171+
while ind <= lastindex(text)
170172
c = text[ind]
171173
nl = c == '\n' || c == '\r'
172174
if c == '\r' && ind + 1 <= lastindex(text) && text[ind + 1] == '\n'
@@ -175,7 +177,7 @@ function get_line_offsets(doc::Document, force=false)
175177
nl && push!(doc._line_offsets, ind)
176178
ind = nextind(text, ind)
177179
end
178-
end
180+
end
179181
return doc._line_offsets
180182
end
181183

@@ -184,7 +186,7 @@ function get_line_offsets2!(doc::Document, force=false)
184186
doc._line_offsets2 = Int[1]
185187
text = get_text(doc)
186188
ind = firstindex(text)
187-
while ind <= lastindex(text)
189+
while ind <= lastindex(text)
188190
c = text[ind]
189191
if c == '\n' || c == '\r'
190192
if c == '\r' && ind + 1 <= lastindex(text) && text[ind + 1] == '\n'
@@ -212,7 +214,7 @@ function get_line_of(line_offsets::Vector{Int}, offset::Integer)
212214
end
213215
line += 1
214216
end
215-
end
217+
end
216218
return line, line_offsets[line]
217219
end
218220

@@ -233,7 +235,7 @@ function get_position_at(doc::Document, offset::Integer)
233235
c = read(io, Char)
234236
character += 1
235237
if UInt32(c) >= 0x010000
236-
character += 1
238+
character += 1
237239
end
238240
end
239241
close(io)

src/exception_types.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ end
6060

6161
function Base.showerror(io::IO, ex::LSInfiniteLoop)
6262
print(io, ex.msg)
63-
end
63+
end
64+
65+
struct LSInvalidFile <: Exception
66+
msg::AbstractString
67+
end
68+
69+
function Base.showerror(io::IO, ex::LSInvalidFile)
70+
print(io, ex.msg)
71+
end

0 commit comments

Comments
 (0)