Skip to content

Commit

Permalink
give Select.readline an optional deleimiter parameter (not yet tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Nov 30, 2011
1 parent f4defab commit e95cf3a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/pmc/socket.pmc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -439,27 +439,32 @@ Read the given number of bytes from the socket and return them in a string.


/* /*


=item C<METHOD readline()> =item C<METHOD readline(STRING delimiter)>


Read a line from the socket and return it in a string. Read a line from the socket and return it in a string. If C<delimiter> is
present, it is used to determine line endings instead of the default C<\n>.


=cut =cut


*/ */


METHOD readline() { METHOD readline(STRING *delimiter :optional,
INTVAL has_delimiter :opt_flag) {
INTVAL idx; INTVAL idx;
STRING *result; STRING *result;
STRING *buf; STRING *buf;
GET_ATTR_buf(INTERP, SELF, buf); GET_ATTR_buf(INTERP, SELF, buf);


if (!has_delimiter)
delimiter = CONST_STRING(INTERP, "\n");

if (Parrot_io_socket_is_closed(INTERP, SELF)) if (Parrot_io_socket_is_closed(INTERP, SELF))
RETURN(STRING * STRINGNULL); RETURN(STRING * STRINGNULL);


if (buf == STRINGNULL) if (buf == STRINGNULL)
buf = Parrot_io_reads(INTERP, SELF, CHUNK_SIZE); buf = Parrot_io_reads(INTERP, SELF, CHUNK_SIZE);


while ((idx = Parrot_str_find_index(INTERP, buf, CONST_STRING(INTERP, "\n"), 0)) < 0) { while ((idx = Parrot_str_find_index(INTERP, buf, delimiter, 0)) < 0) {
STRING * const more = Parrot_io_reads(INTERP, SELF, CHUNK_SIZE); STRING * const more = Parrot_io_reads(INTERP, SELF, CHUNK_SIZE);
if (Parrot_str_length(INTERP, more) == 0) { if (Parrot_str_length(INTERP, more) == 0) {
SET_ATTR_buf(INTERP, SELF, STRINGNULL); SET_ATTR_buf(INTERP, SELF, STRINGNULL);
Expand All @@ -468,7 +473,7 @@ Read a line from the socket and return it in a string.
buf = Parrot_str_concat(INTERP, buf, more); buf = Parrot_str_concat(INTERP, buf, more);
} }


idx++; idx += Parrot_str_length(INTERP, delimiter);
result = Parrot_str_substr(INTERP, buf, 0, idx); result = Parrot_str_substr(INTERP, buf, 0, idx);
buf = Parrot_str_substr(INTERP, buf, idx, Parrot_str_length(INTERP, buf) - idx); buf = Parrot_str_substr(INTERP, buf, idx, Parrot_str_length(INTERP, buf) - idx);
SET_ATTR_buf(INTERP, SELF, buf); SET_ATTR_buf(INTERP, SELF, buf);
Expand Down

0 comments on commit e95cf3a

Please sign in to comment.