Skip to content

Commit 08f00b5

Browse files
fcardtkelman
authored andcommitted
RFC: Fix skipchars inexact error/hanging (#16892) (#18752)
* Fix skipchars inexact error (#16892) * fix indent ((squash)) * add parentheses to test_skipchars macrocalls ((squash)) The whitespace separating strings containing whitespace was hurting my eyes. * Remove now unused unexported function peekchar And its friend, the _chtmp const. * Make Char(0xffffffff) as a possible value for the linecomment argument ((squash)) * change `action` argument name to `skipcomment` ((squash)) * Simplify function * Restore peekchar * Remove support for IOBuffer * Trailling whitespace + typo
1 parent 145eae8 commit 08f00b5

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

base/iostream.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ end
312312
const _chtmp = Array{Char}(1)
313313
function peekchar(s::IOStream)
314314
if ccall(:ios_peekutf8, Cint, (Ptr{Void}, Ptr{Char}), s, _chtmp) < 0
315-
return Char(-1)
315+
return typemax(Char)
316316
end
317317
return _chtmp[1]
318318
end
@@ -321,15 +321,16 @@ function peek(s::IOStream)
321321
ccall(:ios_peekc, Cint, (Ptr{Void},), s)
322322
end
323323

324-
function skipchars(s::IOStream, pred; linecomment::Char=Char(0xffffffff))
325-
ch = peekchar(s); status = Int(ch)
326-
while status >= 0 && (pred(ch) || ch == linecomment)
327-
if ch == linecomment
328-
readline(s)
329-
else
330-
read(s, Char) # advance one character
324+
function skipchars(io::IOStream, pred; linecomment=nothing)
325+
while !eof(io)
326+
c = read(io, Char)
327+
if c === linecomment
328+
readline(io)
329+
elseif !pred(c)
330+
seek(io,position(io)-sizeof(string(c)))
331+
break
331332
end
332-
ch = peekchar(s); status = Int(ch)
333333
end
334-
return s
334+
return io
335335
end
336+

test/choosetests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function choosetests(choices = [])
3535
"enums", "cmdlineargs", "i18n", "workspace", "libdl", "int",
3636
"checked", "intset", "floatfuncs", "compile", "distributed", "inline",
3737
"boundscheck", "error", "ambiguous", "cartesian", "asmvariant", "osutils",
38-
"channels"
38+
"channels", "iostream"
3939
]
4040
profile_skipped = false
4141
if startswith(string(Sys.ARCH), "arm")

test/iostream.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Test skipchars for IOStreams
2+
mktemp() do path, file
3+
function append_to_file(str)
4+
mark(file)
5+
println(file, str)
6+
flush(file)
7+
reset(file)
8+
end
9+
10+
# test it doesn't error on eof
11+
@test skipchars(file, isspace) == file
12+
13+
# test if it correctly skips
14+
append_to_file(" ")
15+
@test eof(skipchars(file, isspace))
16+
17+
# test it correctly detects comment lines
18+
append_to_file("# \n ")
19+
@test eof(skipchars(file, isspace, linecomment='#'))
20+
21+
# test it stops at the appropriate time
22+
append_to_file(" not a space")
23+
@test skipchars(file, isspace) == file
24+
@test !eof(file) && read(file, Char) == 'n'
25+
26+
# test it correctly ignores the contents of comment lines
27+
append_to_file(" #not a space \n not a space")
28+
@test skipchars(file, isspace, linecomment='#') == file
29+
@test !eof(file) && read(file, Char) == 'n'
30+
end
31+

0 commit comments

Comments
 (0)