Skip to content

Commit

Permalink
Move chomp to jl_readuntil
Browse files Browse the repository at this point in the history
  • Loading branch information
mpastell committed Jan 10, 2017
1 parent fe1535a commit 1cd2051
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 92 deletions.
4 changes: 2 additions & 2 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ take!(s::IOStream) =
ccall(:jl_take_buffer, Vector{UInt8}, (Ptr{Void},), s.ios)

function readuntil(s::IOStream, delim::UInt8)
ccall(:jl_readuntil, Array{UInt8,1}, (Ptr{Void}, UInt8, UInt8), s.ios, delim, 0)
ccall(:jl_readuntil, Array{UInt8,1}, (Ptr{Void}, UInt8, UInt8, UInt8), s.ios, delim, 0, 0)
end

function readline(s::IOStream, chomp::Bool=false)
ccall(:jl_readline, Ref{String}, (Ptr{Void}, UInt8), s.ios, chomp)
ccall(:jl_readuntil, Ref{String}, (Ptr{Void}, UInt8, UInt8, UInt8), s.ios, '\n', 1, chomp)
end

function readbytes_all!(s::IOStream, b::Array{UInt8}, nb)
Expand Down
51 changes: 11 additions & 40 deletions src/support/ios.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ size_t ios_copyall(ios_t *to, ios_t *from)

#define LINE_CHUNK_SIZE 160

size_t ios_copyuntil(ios_t *to, ios_t *from, char delim)
size_t ios_copyuntil(ios_t *to, ios_t *from, char delim, uint8_t chomp)
{
size_t total = 0, avail = (size_t)(from->size - from->bpos);
while (!ios_eof(from)) {
Expand All @@ -821,7 +821,8 @@ size_t ios_copyuntil(ios_t *to, ios_t *from, char delim)
}
else {
size_t ntowrite = pd - (from->buf+from->bpos) + 1;
written = ios_write(to, from->buf+from->bpos, ntowrite);
size_t nchomp = ios_nchomp(from, ntowrite, chomp);
written = ios_write(to, from->buf+from->bpos, ntowrite - nchomp);
from->bpos += ntowrite;
total += written;
return total;
Expand All @@ -831,50 +832,20 @@ size_t ios_copyuntil(ios_t *to, ios_t *from, char delim)
return total;
}

size_t ios_copyline(ios_t *to, ios_t *from, uint8_t chomp)
size_t ios_nchomp(ios_t *from, size_t ntowrite, uint8_t chomp)
{

size_t total = 0, avail = (size_t)(from->size - from->bpos);

while (!ios_eof(from)) {
if (avail == 0) {
avail = ios_readprep(from, LINE_CHUNK_SIZE);
if (avail == 0)
break;
}
size_t written;
char *n = (char*)memchr(from->buf+from->bpos, '\n', avail);

if (n == NULL) {
written = ios_write(to, from->buf+from->bpos, avail);
from->bpos += avail;
total += written;
avail = 0;
size_t nchomp = 0;
if (chomp) {
if (ntowrite > 1 && from->buf[from->bpos+ntowrite - 2] == '\r') {
nchomp = 2;
}
else {
size_t nchomp = 0;
size_t ntowrite = n - (from->buf+from->bpos) + 1;

if (chomp) {
if (ntowrite > 1 && from->buf[from->bpos+ntowrite - 2] == '\r') {
nchomp = 2;
}
else {
nchomp = 1;
}
}

written = ios_write(to, from->buf+from->bpos, ntowrite - nchomp);
from->bpos += ntowrite;
total += written;
return total;
nchomp = 1;
}
}
from->_eof = 1;
return total;
return nchomp;
}


static void _ios_init(ios_t *s)
{
// put all fields in a sane initial state
Expand Down Expand Up @@ -1179,7 +1150,7 @@ char *ios_readline(ios_t *s)
{
ios_t dest;
ios_mem(&dest, 0);
ios_copyuntil(&dest, s, '\n');
ios_copyuntil(&dest, s, '\n', 0);
size_t n;
return ios_take_buffer(&dest, &n);
}
Expand Down
4 changes: 2 additions & 2 deletions src/support/ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ JL_DLLEXPORT int ios_get_writable(ios_t *s);
JL_DLLEXPORT void ios_set_readonly(ios_t *s);
JL_DLLEXPORT size_t ios_copy(ios_t *to, ios_t *from, size_t nbytes);
JL_DLLEXPORT size_t ios_copyall(ios_t *to, ios_t *from);
JL_DLLEXPORT size_t ios_copyuntil(ios_t *to, ios_t *from, char delim);
JL_DLLEXPORT size_t ios_copyline(ios_t *to, ios_t *from, uint8_t chomp);
JL_DLLEXPORT size_t ios_copyuntil(ios_t *to, ios_t *from, char delim, uint8_t chomp);
JL_DLLEXPORT size_t ios_nchomp(ios_t *from, size_t ntowrite, uint8_t chomp);
// ensure at least n bytes are buffered if possible. returns # available.
JL_DLLEXPORT size_t ios_readprep(ios_t *from, size_t n);

Expand Down
52 changes: 4 additions & 48 deletions src/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,16 @@ JL_DLLEXPORT jl_array_t *jl_take_buffer(ios_t *s)
return a;
}

JL_DLLEXPORT jl_value_t *jl_readuntil(ios_t *s, uint8_t delim, uint8_t str)
JL_DLLEXPORT jl_value_t *jl_readuntil(ios_t *s, uint8_t delim, uint8_t str, uint8_t chomp)
{
jl_array_t *a;
// manually inlined common case
char *pd = (char*)memchr(s->buf+s->bpos, delim, (size_t)(s->size - s->bpos));
if (pd) {
size_t n = pd-(s->buf+s->bpos)+1;
if (str) {
jl_value_t *str = jl_pchar_to_string(s->buf + s->bpos, n);
size_t nchomp = ios_nchomp(s, n, chomp);
jl_value_t *str = jl_pchar_to_string(s->buf + s->bpos, n - nchomp);
s->bpos += n;
return str;
}
Expand All @@ -271,7 +272,7 @@ JL_DLLEXPORT jl_value_t *jl_readuntil(ios_t *s, uint8_t delim, uint8_t str)
ios_t dest;
ios_mem(&dest, 0);
ios_setbuf(&dest, (char*)a->data, 80, 0);
size_t n = ios_copyuntil(&dest, s, delim);
size_t n = ios_copyuntil(&dest, s, delim, chomp);
if (dest.buf != a->data) {
a = jl_take_buffer(&dest);
}
Expand All @@ -292,51 +293,6 @@ JL_DLLEXPORT jl_value_t *jl_readuntil(ios_t *s, uint8_t delim, uint8_t str)
return (jl_value_t*)a;
}

JL_DLLEXPORT jl_value_t *jl_readline(ios_t *s, uint8_t chomp)
{
jl_array_t *a;
// manually inlined common case
char *n = (char*)memchr(s->buf+s->bpos, '\n', (size_t)(s->size - s->bpos));
if (n) {
size_t ntowrite = n-(s->buf+s->bpos)+1;
size_t nchomp = 0;
if (chomp)
{
if (ntowrite > 1 && s->buf[s->bpos+ntowrite - 2] == '\r') {
nchomp = 2;
}
else {
nchomp = 1;
}
}
jl_value_t *str = jl_pchar_to_string(s->buf + s->bpos, ntowrite - nchomp);
s->bpos += ntowrite;
return str;
}
else {
a = jl_alloc_array_1d(jl_array_uint8_type, 80);
ios_t dest;
ios_mem(&dest, 0);
ios_setbuf(&dest, (char*)a->data, 80, 0);
size_t n = ios_copyline(&dest, s, chomp);
if (dest.buf != a->data) {
a = jl_take_buffer(&dest);
}
else {
#ifdef STORE_ARRAY_LEN
a->length = n;
#endif
a->nrows = n;
((char*)a->data)[n] = '\0';
}

JL_GC_PUSH1(&a);
jl_value_t *st = jl_array_to_string(a);
JL_GC_POP();
return st;
}
}

JL_DLLEXPORT uint64_t jl_ios_get_nbyte_int(ios_t *s, const size_t n)
{
assert(n <= 8);
Expand Down

0 comments on commit 1cd2051

Please sign in to comment.