diff options
-rw-r--r-- | stream.c | 18 | ||||
-rw-r--r-- | txr.1 | 21 |
2 files changed, 26 insertions, 13 deletions
@@ -4403,25 +4403,19 @@ struct strm_ctx *get_ctx(val stream) val get_string(val stream_in, val nchars_in, val close_after_p) { + val self = lit("get-string"); val stream = default_arg_strict(stream_in, std_input); - val strstream = make_string_output_stream(); val nchars = default_null_arg(nchars_in); - val ch; - - if (nchars) { - for (; gt(nchars, zero) && (ch = get_char(stream)); - nchars = minus(nchars, one)) - put_char(ch, strstream); - } else { - while ((ch = get_char(stream))) - put_char(ch, strstream); - } + ucnum nc = if3(nchars, c_unum(nchars, self), UINT_PTR_MAX); + struct strm_ops *ops = coerce(struct strm_ops *, + cobj_ops(self, stream, stream_cls)); + val str = ops->get_string(stream, nc, WEOF, self); if ((missingp(close_after_p) && (!opt_compat || opt_compat > 102)) || default_arg_strict(close_after_p, t)) close_stream(stream, t); - return get_string_from_stream(strstream); + return str ? str : null_string; } val open_directory(val path) @@ -67408,6 +67408,15 @@ longer than .metn count , but may be shorter. +The +.code get-string +function does not return +.code nil +upon reaching the end of the stream. Even if no character are available, it +returns an empty string. +This behavior is different from +.codn get-delimited-string . + .coNP Function @ get-delimited-string .synb .mets (get-delimited-string >> [ stream >> [ count <> [ stop-char ]]]) @@ -67418,6 +67427,14 @@ The function reads characters from a stream, returning them as a string. +If +.code get-delimited-string +is not able to read any characters from +.meta stream +due to the end-of-stream condition, it returns +.codn nil . + +If If the .meta count argument is given, it should be a non-negative integer. The function @@ -67425,7 +67442,9 @@ stops reading when it has accumulated .meta count characters. If .meta count -is zero, no characters are read and an empty string is returned. +is zero, no characters are read and +.code nil +is returned. If the .meta stop-char |