Skip to content

Commit 90cfb82

Browse files
authored
Merge pull request #21117 from fcard/fix-skipchars-regression-intermediary
Improve performance of skipchars for small inputs
2 parents 91f3275 + 2894595 commit 90cfb82

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

base/iostream.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function skipchars(io::IOStream, pred; linecomment=nothing)
327327
if c === linecomment
328328
readline(io)
329329
elseif !pred(c)
330-
seek(io,position(io)-sizeof(string(c)))
330+
skip(io, -codelen(c))
331331
break
332332
end
333333
end

test/iostream.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
mktemp() do path, file
55
function append_to_file(str)
66
mark(file)
7-
println(file, str)
7+
print(file, str)
88
flush(file)
99
reset(file)
1010
end
1111

1212
# test it doesn't error on eof
13-
@test skipchars(file, isspace) == file
13+
@test eof(skipchars(file, isspace))
1414

15-
# test if it correctly skips
15+
# test it correctly skips
1616
append_to_file(" ")
1717
@test eof(skipchars(file, isspace))
1818

@@ -22,12 +22,20 @@ mktemp() do path, file
2222

2323
# test it stops at the appropriate time
2424
append_to_file(" not a space")
25-
@test skipchars(file, isspace) == file
26-
@test !eof(file) && read(file, Char) == 'n'
25+
@test !eof(skipchars(file, isspace))
26+
@test read(file, Char) == 'n'
2727

2828
# test it correctly ignores the contents of comment lines
2929
append_to_file(" #not a space \n not a space")
30-
@test skipchars(file, isspace, linecomment='#') == file
31-
@test !eof(file) && read(file, Char) == 'n'
30+
@test !eof(skipchars(file, isspace, linecomment='#'))
31+
@test read(file, Char) == 'n'
32+
33+
# test it correctly handles unicode
34+
for (byte,char) in zip(1:4, ('@','߷','','𐋺'))
35+
append_to_file("abcdef$char")
36+
@test Base.codelen(char) == byte
37+
@test !eof(skipchars(file, isalpha))
38+
@test read(file, Char) == char
39+
end
3240
end
3341

0 commit comments

Comments
 (0)