Skip to content

Commit 39811f2

Browse files
committed
Add UTF8 optimized _substring
1 parent 2462784 commit 39811f2

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/utf8optimizations.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,42 @@ function tryparsenext(q::Quoted{T,S,<:UInt8,<:UInt8}, str::Union{VectorBackedUTF
317317

318318
@label error
319319
return Nullable{T}(), i
320-
end
320+
end
321+
322+
@inline function _substring(::Type{String}, str::Union{VectorBackedUTF8String, String}, i, j, escapecount, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8})
323+
if escapecount > 0
324+
buffer = Vector{UInt8}(undef, j-i+1-escapecount)
325+
cur_i = i
326+
cur_buffer_i = 1
327+
@inbounds c = codeunit(str, cur_i)
328+
if opts.includequotes && c==opts.quotechar
329+
@inbounds buffer[cur_buffer_i] = c
330+
cur_i += 1
331+
cur_buffer_i += 1
332+
end
333+
while cur_i <= j
334+
@inbounds c = codeunit(str, cur_i)
335+
if c == opts.escapechar
336+
next_i = cur_i + 1
337+
if next_i <= j
338+
@inbounds next_c = codeunit(str, next_i)
339+
if next_c == opts.quotechar
340+
@inbounds buffer[cur_buffer_i] = next_c
341+
cur_buffer_i += 1
342+
cur_i = next_i
343+
end
344+
else
345+
@inbounds buffer[cur_buffer_i] = c
346+
cur_buffer_i += 1
347+
end
348+
else
349+
@inbounds buffer[cur_buffer_i] = c
350+
cur_buffer_i += 1
351+
end
352+
cur_i += 1
353+
end
354+
return String(buffer)
355+
else
356+
return unsafe_string(pointer(str, i), j-i+1)
357+
end
358+
end

0 commit comments

Comments
 (0)