Skip to content

Commit

Permalink
eatwspace: use ios_peekutf8 rather than position & seek
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jul 26, 2012
1 parent 38235ce commit 89d59e4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
29 changes: 0 additions & 29 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,3 @@ for f = (:iswalnum, :iswalpha, :iswblank, :iswcntrl, :iswdigit,
)
@eval ($f)(c::Char) = bool(ccall($expr(:quote,f), Int32, (Char,), c))
end

## IOStream character manipulation ##

function eatwspace(s::IOStream)
p = position(s)
c = read(s, Char)
while iswspace(c) && !eof(s)
p = position(s)
c = read(s, Char)
end
if !iswspace(c)
seek(s, p)
end
end

function eatwspace_comment(s::IOStream, cmt::Char)
p = position(s)
c = read(s, Char)
while (iswspace(c) || c == cmt) && !eof(s)
if c == cmt
readline(s)
end
p = position(s)
c = read(s, Char)
end
if !(iswspace(c) || c == cmt)
seek(s, p)
end
end
21 changes: 21 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,24 @@ begin
readfds.nfds, readfds.data, C_NULL, C_NULL, tout)
end
end

## Character streams ##
_wstmp = Array(Char, 1)
function eatwspace(s::IOStream)
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
while status > 0 && iswspace(_wstmp[1])
c = read(s, Char) # advance one character
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
end
end

function eatwspace_comment(s::IOStream, cmt::Char)
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
while status > 0 && (iswspace(_wstmp[1]) || _wstmp[1] == cmt)
if _wstmp[1] == cmt
readline(s)
end
c = read(s, Char) # advance one character
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
end
end
4 changes: 3 additions & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export
den,depth,deserialize,det,dfill,diag,diagm,diagmm,diagmm!,dict,diff,
dist,distdim,distribute,div,dlmread,dlmwrite,dlopen,dlsym,done,dones,dot,
drand,drandn,dump,dup2,dzeros,each_col,each_col!,each_line,each_match,
each_row,each_row!,each_search,each_vec,each_vec!,edit,eig,elements,eltype,
each_row,each_row!,each_search,each_vec,each_vec!,
eatwspace,eatwspace_comment,
edit,eig,elements,eltype,
ends_with,enq_work,enqueue,enumerate,eof,eps,erf,erfc,errno,error,
esc,escape_string,exec,exit,exp,exp2,expm1,expr,exprnd,eye,
factor,factorial,falses,fd,fdio,fetch,fft,fft2,fft3,fft_num_threads,
Expand Down
1 change: 1 addition & 0 deletions src/julia.expmap
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
ios_pututf8;
ios_getc;
ios_getutf8;
ios_peekutf8;
jl_getutf8;
u8_isvalid;
u8_strwidth;
Expand Down
2 changes: 1 addition & 1 deletion src/support/ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ DLLEXPORT int ios_vprintf(ios_t *s, const char *format, va_list args);
/* high-level stream functions - input */
int ios_getnum(ios_t *s, char *data, uint32_t type);
DLLEXPORT int ios_getutf8(ios_t *s, uint32_t *pwc);
int ios_peekutf8(ios_t *s, uint32_t *pwc);
DLLEXPORT int ios_peekutf8(ios_t *s, uint32_t *pwc);
int ios_ungetutf8(ios_t *s, uint32_t wc);
//int ios_getstringz(ios_t *dest, ios_t *src);
//int ios_getstringn(ios_t *dest, ios_t *src, size_t nchars);
Expand Down

0 comments on commit 89d59e4

Please sign in to comment.