From 2894595306aa9fd74e41d2f92af093df8bbe4f94 Mon Sep 17 00:00:00 2001 From: Fabio Cardeal Date: Fri, 3 Mar 2017 20:44:21 -0300 Subject: [PATCH] Improve performance of skipchars for small inputs This mostly solves the problem, now the function is only significantly (2x) slower when there are no characters to skip, otherwise it's mostly the same for 1 or 2 characters and faster for 3 or more. Also improve the tests a little. --- base/iostream.jl | 2 +- test/iostream.jl | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/base/iostream.jl b/base/iostream.jl index 0fa45aea98954..e6a56b24ee477 100644 --- a/base/iostream.jl +++ b/base/iostream.jl @@ -327,7 +327,7 @@ function skipchars(io::IOStream, pred; linecomment=nothing) if c === linecomment readline(io) elseif !pred(c) - seek(io,position(io)-sizeof(string(c))) + skip(io, -codelen(c)) break end end diff --git a/test/iostream.jl b/test/iostream.jl index 3ca501aa8e4ab..1b817ce26d2b6 100644 --- a/test/iostream.jl +++ b/test/iostream.jl @@ -4,15 +4,15 @@ mktemp() do path, file function append_to_file(str) mark(file) - println(file, str) + print(file, str) flush(file) reset(file) end # test it doesn't error on eof - @test skipchars(file, isspace) == file + @test eof(skipchars(file, isspace)) - # test if it correctly skips + # test it correctly skips append_to_file(" ") @test eof(skipchars(file, isspace)) @@ -22,12 +22,20 @@ mktemp() do path, file # test it stops at the appropriate time append_to_file(" not a space") - @test skipchars(file, isspace) == file - @test !eof(file) && read(file, Char) == 'n' + @test !eof(skipchars(file, isspace)) + @test read(file, Char) == 'n' # test it correctly ignores the contents of comment lines append_to_file(" #not a space \n not a space") - @test skipchars(file, isspace, linecomment='#') == file - @test !eof(file) && read(file, Char) == 'n' + @test !eof(skipchars(file, isspace, linecomment='#')) + @test read(file, Char) == 'n' + + # test it correctly handles unicode + for (byte,char) in zip(1:4, ('@','߷','࿊','𐋺')) + append_to_file("abcdef$char") + @test Base.codelen(char) == byte + @test !eof(skipchars(file, isalpha)) + @test read(file, Char) == char + end end