summaryrefslogtreecommitdiff
path: root/ext/stringio/stringio.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/stringio/stringio.c')
-rw-r--r--ext/stringio/stringio.c419
1 files changed, 273 insertions, 146 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 5c45e30950..41aa71f893 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -13,13 +13,14 @@
**********************************************************************/
static const char *const
-STRINGIO_VERSION = "3.1.3.dev";
+STRINGIO_VERSION = "3.2.1.dev";
#include <stdbool.h>
#include "ruby.h"
#include "ruby/io.h"
#include "ruby/encoding.h"
+#include "ruby/version.h"
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
#include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
@@ -35,15 +36,33 @@ STRINGIO_VERSION = "3.1.3.dev";
# define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass)
#endif
+static inline bool
+str_chilled_p(VALUE str)
+{
+#if (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4) || RUBY_API_VERSION_MAJOR >= 4
+ // Do not attempt to modify chilled strings on Ruby 3.4+
+ // RUBY_FL_USER2 == STR_CHILLED_LITERAL
+ // RUBY_FL_USER3 == STR_CHILLED_SYMBOL_TO_S
+ return FL_TEST_RAW(str, RUBY_FL_USER2 | RUBY_FL_USER3);
+#else
+ return false;
+#endif
+}
+
+#ifndef HAVE_TYPE_RB_IO_MODE_T
+typedef int rb_io_mode_t;
+#endif
+
struct StringIO {
VALUE string;
rb_encoding *enc;
long pos;
long lineno;
- int flags;
+ rb_io_mode_t flags;
int count;
};
+static struct StringIO *get_strio_for_read(VALUE self);
static VALUE strio_init(int, VALUE *, struct StringIO *, VALUE);
static VALUE strio_unget_bytes(struct StringIO *, const char *, long);
static long strio_write(VALUE self, VALUE str);
@@ -108,8 +127,14 @@ static const rb_data_type_t strio_data_type = {
static struct StringIO*
get_strio(VALUE self)
{
- struct StringIO *ptr = check_strio(rb_io_taint_check(self));
+ rb_check_frozen(self);
+ return get_strio_for_read(self);
+}
+static struct StringIO*
+get_strio_for_read(VALUE self)
+{
+ struct StringIO *ptr = check_strio(self);
if (!ptr) {
rb_raise(rb_eIOError, "uninitialized stream");
}
@@ -136,14 +161,38 @@ strio_substr(struct StringIO *ptr, long pos, long len, rb_encoding *enc)
return enc_subseq(str, pos, len, enc);
}
+static VALUE
+strio_readbuf(struct StringIO *ptr, VALUE str)
+{
+ if (!NIL_P(str)) {
+ StringValue(str);
+ rb_str_modify(str);
+ if (str == ptr->string) {
+ rb_raise(rb_eArgError, "cannot read into the underlying string");
+ }
+ }
+ return str;
+}
+
#define StringIO(obj) get_strio(obj)
+#define StringIOForRead(obj) get_strio_for_read(obj)
#define STRIO_READABLE FL_USER4
#define STRIO_WRITABLE FL_USER5
#define STRIO_READWRITE (STRIO_READABLE|STRIO_WRITABLE)
typedef char strio_flags_check[(STRIO_READABLE/FMODE_READABLE == STRIO_WRITABLE/FMODE_WRITABLE) * 2 - 1];
+#ifndef RB_FL_TEST_RAW
+# define RB_FL_TEST_RAW(obj, bits) (RBASIC(obj)->flags & (bits))
+#endif
+#ifndef RB_FL_SET_RAW
+# define RB_FL_SET_RAW(obj, bits) (RBASIC(obj)->flags |= (bits))
+#endif
+#ifndef RB_FL_UNSET_RAW
+# define RB_FL_UNSET_RAW(obj, bits) (RBASIC(obj)->flags &= ~(bits))
+#endif
+
#define STRIO_MODE_SET_P(strio, mode) \
- ((RBASIC(strio)->flags & STRIO_##mode) && \
+ (RB_FL_TEST_RAW(strio, STRIO_##mode) && \
((struct StringIO*)DATA_PTR(strio))->flags & FMODE_##mode)
#define CLOSED(strio) (!STRIO_MODE_SET_P(strio, READWRITE))
#define READABLE(strio) STRIO_MODE_SET_P(strio, READABLE)
@@ -154,7 +203,7 @@ static VALUE sym_exception;
static struct StringIO*
readable(VALUE strio)
{
- struct StringIO *ptr = StringIO(strio);
+ struct StringIO *ptr = StringIOForRead(strio);
if (!READABLE(strio)) {
rb_raise(rb_eIOError, "not opened for reading");
}
@@ -180,6 +229,21 @@ check_modifiable(struct StringIO *ptr)
else if (OBJ_FROZEN_RAW(ptr->string)) {
rb_raise(rb_eIOError, "not modifiable string");
}
+ else {
+ rb_str_modify(ptr->string);
+ }
+}
+
+static inline bool
+outside_p(struct StringIO *ptr, long pos)
+{
+ return NIL_P(ptr->string) || pos >= RSTRING_LEN(ptr->string);
+}
+
+static inline bool
+eos_p(struct StringIO *ptr)
+{
+ return outside_p(ptr, ptr->pos);
}
static VALUE
@@ -192,17 +256,32 @@ strio_s_allocate(VALUE klass)
* call-seq:
* StringIO.new(string = '', mode = 'r+') -> new_stringio
*
- * Note that +mode+ defaults to <tt>'r'</tt> if +string+ is frozen.
- *
* Returns a new \StringIO instance formed from +string+ and +mode+;
- * see {Access Modes}[rdoc-ref:File@Access+Modes]:
+ * the instance should be closed when no longer needed:
+ *
+ * strio = StringIO.new
+ * strio.string # => ""
+ * strio.closed_read? # => false
+ * strio.closed_write? # => false
+ * strio.close
*
- * strio = StringIO.new # => #<StringIO>
+ * If +string+ is frozen, the default +mode+ is <tt>'r'</tt>:
+ *
+ * strio = StringIO.new('foo'.freeze)
+ * strio.string # => "foo"
+ * strio.closed_read? # => false
+ * strio.closed_write? # => true
* strio.close
*
- * The instance should be closed when no longer needed.
+ * Argument +mode+ must be a valid
+ * {Access Mode}[rdoc-ref:File@Access+Modes],
+ * which may be a string or an integer constant:
+ *
+ * StringIO.new('foo', 'w+')
+ * StringIO.new('foo', File::RDONLY)
*
- * Related: StringIO.open (accepts block; closes automatically).
+ * Related: StringIO.open
+ * (passes the \StringIO object to the block; closes the object automatically on block exit).
*/
static VALUE
strio_initialize(int argc, VALUE *argv, VALUE self)
@@ -322,14 +401,15 @@ strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
ptr->pos = 0;
ptr->lineno = 0;
if (ptr->flags & FMODE_SETENC_BY_BOM) set_encoding_by_bom(ptr);
- RBASIC(self)->flags |= (ptr->flags & FMODE_READWRITE) * (STRIO_READABLE / FMODE_READABLE);
+ RB_FL_SET_RAW(self, (ptr->flags & FMODE_READWRITE) * (STRIO_READABLE / FMODE_READABLE));
return self;
}
static VALUE
strio_finalize(VALUE self)
{
- struct StringIO *ptr = StringIO(self);
+ struct StringIO *ptr = check_strio(self);
+ if (!ptr) return Qnil;
RB_OBJ_WRITE(self, &ptr->string, Qnil);
ptr->flags &= ~FMODE_READWRITE;
return self;
@@ -337,23 +417,20 @@ strio_finalize(VALUE self)
/*
* call-seq:
- * StringIO.open(string = '', mode = 'r+') {|strio| ... }
- *
- * Note that +mode+ defaults to <tt>'r'</tt> if +string+ is frozen.
+ * StringIO.open(string = '', mode = 'r+') -> new_stringio
+ * StringIO.open(string = '', mode = 'r+') {|strio| ... } -> object
*
- * Creates a new \StringIO instance formed from +string+ and +mode+;
- * see {Access Modes}[rdoc-ref:File@Access+Modes].
+ * Creates new \StringIO instance by calling <tt>StringIO.new(string, mode)</tt>.
*
- * With no block, returns the new instance:
+ * With no block given, returns the new instance:
*
* strio = StringIO.open # => #<StringIO>
*
- * With a block, calls the block with the new instance
+ * With a block given, calls the block with the new instance
* and returns the block's value;
- * closes the instance on block exit.
+ * closes the instance on block exit:
*
- * StringIO.open {|strio| p strio }
- * # => #<StringIO>
+ * StringIO.open('foo') {|strio| strio.string.upcase } # => "FOO"
*
* Related: StringIO.new.
*/
@@ -379,42 +456,42 @@ strio_s_new(int argc, VALUE *argv, VALUE klass)
}
/*
- * Returns +false+. Just for compatibility to IO.
+ * Returns +false+; for compatibility with IO.
*/
static VALUE
strio_false(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
return Qfalse;
}
/*
- * Returns +nil+. Just for compatibility to IO.
+ * Returns +nil+; for compatibility with IO.
*/
static VALUE
strio_nil(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
return Qnil;
}
/*
- * Returns an object itself. Just for compatibility to IO.
+ * Returns +self+; for compatibility with IO.
*/
static VALUE
strio_self(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
return self;
}
/*
- * Returns 0. Just for compatibility to IO.
+ * Returns 0; for compatibility with IO.
*/
static VALUE
strio_0(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
return INT2FIX(0);
}
@@ -424,7 +501,7 @@ strio_0(VALUE self)
static VALUE
strio_first(VALUE self, VALUE arg)
{
- StringIO(self);
+ StringIOForRead(self);
return arg;
}
@@ -434,7 +511,7 @@ strio_first(VALUE self, VALUE arg)
static VALUE
strio_unimpl(int argc, VALUE *argv, VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
rb_notimplement();
UNREACHABLE;
@@ -462,14 +539,14 @@ strio_unimpl(int argc, VALUE *argv, VALUE self)
static VALUE
strio_get_string(VALUE self)
{
- return StringIO(self)->string;
+ return StringIOForRead(self)->string;
}
/*
* call-seq:
* string = other_string -> other_string
*
- * Assigns the underlying string as +other_string+, and sets position to zero;
+ * Replaces the stored string with +other_string+, and sets the position to zero;
* returns +other_string+:
*
* StringIO.open('foo') do |strio|
@@ -483,7 +560,7 @@ strio_get_string(VALUE self)
* "foo"
* "bar"
*
- * Related: StringIO#string (returns the underlying string).
+ * Related: StringIO#string (returns the stored string).
*/
static VALUE
strio_set_string(VALUE self, VALUE string)
@@ -504,17 +581,22 @@ strio_set_string(VALUE self, VALUE string)
* call-seq:
* close -> nil
*
- * Closes +self+ for both reading and writing.
+ * Closes +self+ for both reading and writing; returns +nil+:
*
- * Raises IOError if reading or writing is attempted.
+ * strio = StringIO.new
+ * strio.closed? # => false
+ * strio.close # => nil
+ * strio.closed? # => true
+ * strio.read # Raises IOError: not opened for reading
+ * strio.write # Raises IOError: not opened for writing
*
- * Related: StringIO#close_read, StringIO#close_write.
+ * Related: StringIO#close_read, StringIO#close_write, StringIO.closed?.
*/
static VALUE
strio_close(VALUE self)
{
StringIO(self);
- RBASIC(self)->flags &= ~STRIO_READWRITE;
+ RB_FL_UNSET_RAW(self, STRIO_READWRITE);
return Qnil;
}
@@ -522,9 +604,16 @@ strio_close(VALUE self)
* call-seq:
* close_read -> nil
*
- * Closes +self+ for reading; closed-write setting remains unchanged.
+ * Closes +self+ for reading;
+ * closed-write setting remains unchanged;
+ * returns +nil+:
*
- * Raises IOError if reading is attempted.
+ * strio = StringIO.new
+ * strio.closed_read? # => false
+ * strio.close_read # => nil
+ * strio.closed_read? # => true
+ * strio.closed_write? # => false
+ * strio.read # Raises IOError: not opened for reading
*
* Related: StringIO#close, StringIO#close_write.
*/
@@ -535,7 +624,7 @@ strio_close_read(VALUE self)
if (!(ptr->flags & FMODE_READABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for reading");
}
- RBASIC(self)->flags &= ~STRIO_READABLE;
+ RB_FL_UNSET_RAW(self, STRIO_READABLE);
return Qnil;
}
@@ -543,11 +632,16 @@ strio_close_read(VALUE self)
* call-seq:
* close_write -> nil
*
- * Closes +self+ for writing; closed-read setting remains unchanged.
+ * Closes +self+ for writing; closed-read setting remains unchanged; returns +nil+:
*
- * Raises IOError if writing is attempted.
+ * strio = StringIO.new
+ * strio.closed_write? # => false
+ * strio.close_write # => nil
+ * strio.closed_write? # => true
+ * strio.closed_read? # => false
+ * strio.write('foo') # Raises IOError: not opened for writing
*
- * Related: StringIO#close, StringIO#close_read.
+ * Related: StringIO#close, StringIO#close_read, StringIO#closed_write?.
*/
static VALUE
strio_close_write(VALUE self)
@@ -556,7 +650,7 @@ strio_close_write(VALUE self)
if (!(ptr->flags & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
}
- RBASIC(self)->flags &= ~STRIO_WRITABLE;
+ RB_FL_UNSET_RAW(self, STRIO_WRITABLE);
return Qnil;
}
@@ -564,13 +658,21 @@ strio_close_write(VALUE self)
* call-seq:
* closed? -> true or false
*
- * Returns +true+ if +self+ is closed for both reading and writing,
- * +false+ otherwise.
+ * Returns whether +self+ is closed for both reading and writing:
+ *
+ * strio = StringIO.new
+ * strio.closed? # => false # Open for reading and writing.
+ * strio.close_read
+ * strio.closed? # => false # Still open for writing.
+ * strio.close_write
+ * strio.closed? # => true # Now closed for both.
+ *
+ * Related: StringIO.closed_read?, StringIO.closed_write?.
*/
static VALUE
strio_closed(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
if (!CLOSED(self)) return Qfalse;
return Qtrue;
}
@@ -579,12 +681,19 @@ strio_closed(VALUE self)
* call-seq:
* closed_read? -> true or false
*
- * Returns +true+ if +self+ is closed for reading, +false+ otherwise.
+ * Returns whether +self+ is closed for reading:
+ *
+ * strio = StringIO.new
+ * strio.closed_read? # => false
+ * strio.close_read
+ * strio.closed_read? # => true
+ *
+ * Related: StringIO#closed?, StringIO#closed_write?, StringIO#close_read.
*/
static VALUE
strio_closed_read(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
if (READABLE(self)) return Qfalse;
return Qtrue;
}
@@ -593,12 +702,19 @@ strio_closed_read(VALUE self)
* call-seq:
* closed_write? -> true or false
*
- * Returns +true+ if +self+ is closed for writing, +false+ otherwise.
+ * Returns whether +self+ is closed for writing:
+ *
+ * strio = StringIO.new
+ * strio.closed_write? # => false
+ * strio.close_write
+ * strio.closed_write? # => true
+ *
+ * Related: StringIO#close_write, StringIO#closed?, StringIO#closed_read?.
*/
static VALUE
strio_closed_write(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
if (WRITABLE(self)) return Qfalse;
return Qtrue;
}
@@ -607,19 +723,26 @@ static struct StringIO *
strio_to_read(VALUE self)
{
struct StringIO *ptr = readable(self);
- if (NIL_P(ptr->string)) return NULL;
- if (ptr->pos < RSTRING_LEN(ptr->string)) return ptr;
- return NULL;
+ if (eos_p(ptr)) return NULL;
+ return ptr;
}
/*
* call-seq:
* eof? -> true or false
*
- * Returns +true+ if positioned at end-of-stream, +false+ otherwise;
- * see {Position}[rdoc-ref:IO@Position].
+ * Returns whether +self+ is positioned at end-of-stream:
*
- * Raises IOError if the stream is not opened for reading.
+ * strio = StringIO.new('foo')
+ * strio.pos # => 0
+ * strio.eof? # => false
+ * strio.read # => "foo"
+ * strio.pos # => 3
+ * strio.eof? # => true
+ * strio.close_read
+ * strio.eof? # Raises IOError: not opened for reading
+ *
+ * Related: StringIO#pos.
*/
static VALUE
strio_eof(VALUE self)
@@ -637,7 +760,7 @@ strio_copy(VALUE copy, VALUE orig)
orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
if (copy == orig) return copy;
- ptr = StringIO(orig);
+ ptr = StringIOForRead(orig);
old_ptr = check_strio(copy);
if (old_ptr) {
old_string = old_ptr->string;
@@ -645,8 +768,8 @@ strio_copy(VALUE copy, VALUE orig)
}
DATA_PTR(copy) = ptr;
RB_OBJ_WRITTEN(copy, old_string, ptr->string);
- RBASIC(copy)->flags &= ~STRIO_READWRITE;
- RBASIC(copy)->flags |= RBASIC(orig)->flags & STRIO_READWRITE;
+ RB_FL_UNSET_RAW(copy, STRIO_READWRITE);
+ RB_FL_SET_RAW(copy, RB_FL_TEST_RAW(orig, STRIO_READWRITE));
++ptr->count;
return copy;
}
@@ -656,12 +779,12 @@ strio_copy(VALUE copy, VALUE orig)
* lineno -> current_line_number
*
* Returns the current line number in +self+;
- * see {Line Number}[rdoc-ref:IO@Line+Number].
+ * see {Line Number}[rdoc-ref:StringIO@Line+Number].
*/
static VALUE
strio_get_lineno(VALUE self)
{
- return LONG2NUM(StringIO(self)->lineno);
+ return LONG2NUM(StringIOForRead(self)->lineno);
}
/*
@@ -669,7 +792,7 @@ strio_get_lineno(VALUE self)
* lineno = new_line_number -> new_line_number
*
* Sets the current line number in +self+ to the given +new_line_number+;
- * see {Line Number}[rdoc-ref:IO@Line+Number].
+ * see {Line Number}[rdoc-ref:StringIO@Line+Number].
*/
static VALUE
strio_set_lineno(VALUE self, VALUE lineno)
@@ -683,7 +806,7 @@ strio_set_lineno(VALUE self, VALUE lineno)
* binmode -> self
*
* Sets the data mode in +self+ to binary mode;
- * see {Data Mode}[rdoc-ref:File@Data+Mode].
+ * see {Data Mode}[rdoc-ref:StringIO@Data+Mode].
*
*/
static VALUE
@@ -744,12 +867,12 @@ strio_reopen(int argc, VALUE *argv, VALUE self)
* pos -> stream_position
*
* Returns the current position (in bytes);
- * see {Position}[rdoc-ref:IO@Position].
+ * see {Position}[rdoc-ref:StringIO@Position].
*/
static VALUE
strio_get_pos(VALUE self)
{
- return LONG2NUM(StringIO(self)->pos);
+ return LONG2NUM(StringIOForRead(self)->pos);
}
/*
@@ -757,7 +880,7 @@ strio_get_pos(VALUE self)
* pos = new_position -> new_position
*
* Sets the current position (in bytes);
- * see {Position}[rdoc-ref:IO@Position].
+ * see {Position}[rdoc-ref:StringIO@Position].
*/
static VALUE
strio_set_pos(VALUE self, VALUE pos)
@@ -792,9 +915,9 @@ strio_rewind(VALUE self)
* call-seq:
* seek(offset, whence = SEEK_SET) -> 0
*
- * Sets the current position to the given integer +offset+ (in bytes),
+ * Sets the position to the given integer +offset+ (in bytes),
* with respect to a given constant +whence+;
- * see {Position}[rdoc-ref:IO@Position].
+ * see {IO#seek}[rdoc-ref:IO#seek].
*/
static VALUE
strio_seek(int argc, VALUE *argv, VALUE self)
@@ -816,7 +939,11 @@ strio_seek(int argc, VALUE *argv, VALUE self)
offset = ptr->pos;
break;
case 2:
- offset = RSTRING_LEN(ptr->string);
+ if (NIL_P(ptr->string)) {
+ offset = 0;
+ } else {
+ offset = RSTRING_LEN(ptr->string);
+ }
break;
default:
error_inval("invalid whence");
@@ -837,7 +964,7 @@ strio_seek(int argc, VALUE *argv, VALUE self)
static VALUE
strio_get_sync(VALUE self)
{
- StringIO(self);
+ StringIOForRead(self);
return Qtrue;
}
@@ -849,10 +976,9 @@ strio_get_sync(VALUE self)
* call-seq:
* each_byte {|byte| ... } -> self
*
- * With a block given, calls the block with each remaining byte in the stream;
- * see {Byte IO}[rdoc-ref:IO@Byte+IO].
+ * :include: stringio/each_byte.rdoc
*
- * With no block given, returns an enumerator.
+ * Related: StringIO#each_char, StringIO#each_codepoint, StringIO#each_line.
*/
static VALUE
strio_each_byte(VALUE self)
@@ -870,10 +996,10 @@ strio_each_byte(VALUE self)
/*
* call-seq:
- * getc -> character or nil
+ * getc -> character, byte, or nil
+ *
+ * :include: stringio/getc.rdoc
*
- * Reads and returns the next character from the stream;
- * see {Character IO}[rdoc-ref:IO@Character+IO].
*/
static VALUE
strio_getc(VALUE self)
@@ -885,7 +1011,7 @@ strio_getc(VALUE self)
int len;
char *p;
- if (NIL_P(str) || pos >= RSTRING_LEN(str)) {
+ if (eos_p(ptr)) {
return Qnil;
}
p = RSTRING_PTR(str)+pos;
@@ -896,17 +1022,17 @@ strio_getc(VALUE self)
/*
* call-seq:
- * getbyte -> byte or nil
+ * getbyte -> integer or nil
+ *
+ * :include: stringio/getbyte.rdoc
*
- * Reads and returns the next 8-bit byte from the stream;
- * see {Byte IO}[rdoc-ref:IO@Byte+IO].
*/
static VALUE
strio_getbyte(VALUE self)
{
struct StringIO *ptr = readable(self);
int c;
- if (NIL_P(ptr->string) || ptr->pos >= RSTRING_LEN(ptr->string)) {
+ if (eos_p(ptr)) {
return Qnil;
}
c = RSTRING_PTR(ptr->string)[ptr->pos++];
@@ -1075,12 +1201,11 @@ strio_readbyte(VALUE self)
/*
* call-seq:
- * each_char {|c| ... } -> self
+ * each_char {|char| ... } -> self
*
- * With a block given, calls the block with each remaining character in the stream;
- * see {Character IO}[rdoc-ref:IO@Character+IO].
+ * :include: stringio/each_char.rdoc
*
- * With no block given, returns an enumerator.
+ * Related: StringIO#each_byte, StringIO#each_codepoint, StringIO#each_line.
*/
static VALUE
strio_each_char(VALUE self)
@@ -1099,10 +1224,9 @@ strio_each_char(VALUE self)
* call-seq:
* each_codepoint {|codepoint| ... } -> self
*
- * With a block given, calls the block with each remaining codepoint in the stream;
- * see {Codepoint IO}[rdoc-ref:IO@Codepoint+IO].
+ * :include: stringio/each_codepoint.rdoc
*
- * With no block given, returns an enumerator.
+ * Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.
*/
static VALUE
strio_each_codepoint(VALUE self)
@@ -1336,9 +1460,8 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
* gets(limit, chomp: false) -> string or nil
* gets(sep, limit, chomp: false) -> string or nil
*
- * Reads and returns a line from the stream;
- * assigns the return value to <tt>$_</tt>;
- * see {Line IO}[rdoc-ref:IO@Line+IO].
+ * :include: stringio/gets.rdoc
+ *
*/
static VALUE
strio_gets(int argc, VALUE *argv, VALUE self)
@@ -1375,15 +1498,15 @@ strio_readline(int argc, VALUE *argv, VALUE self)
}
/*
+ * :markup: markdown
+ *
* call-seq:
* each_line(sep = $/, chomp: false) {|line| ... } -> self
* each_line(limit, chomp: false) {|line| ... } -> self
* each_line(sep, limit, chomp: false) {|line| ... } -> self
*
- * Calls the block with each remaining line read from the stream;
- * does nothing if already at end-of-file;
- * returns +self+.
- * See {Line IO}[rdoc-ref:IO@Line+IO].
+ * :include: stringio/each_line.md
+ *
*/
static VALUE
strio_each(int argc, VALUE *argv, VALUE self)
@@ -1524,9 +1647,10 @@ strio_write(VALUE self, VALUE str)
/*
* call-seq:
- * strio.putc(obj) -> obj
+ * putc(object) -> object
+ *
+ * :include: stringio/putc.rdoc
*
- * See IO#putc.
*/
static VALUE
strio_putc(VALUE self, VALUE ch)
@@ -1558,9 +1682,10 @@ strio_putc(VALUE self, VALUE ch)
/*
* call-seq:
- * strio.read([length [, outbuf]]) -> string, outbuf, or nil
+ * read(maxlen = nil, out_string = nil) → new_string, out_string, or nil
+ *
+ * :include: stringio/read.rdoc
*
- * See IO#read.
*/
static VALUE
strio_read(int argc, VALUE *argv, VALUE self)
@@ -1572,11 +1697,7 @@ strio_read(int argc, VALUE *argv, VALUE self)
switch (argc) {
case 2:
- str = argv[1];
- if (!NIL_P(str)) {
- StringValue(str);
- rb_str_modify(str);
- }
+ str = strio_readbuf(ptr, argv[1]);
/* fall through */
case 1:
if (!NIL_P(argv[0])) {
@@ -1584,10 +1705,9 @@ strio_read(int argc, VALUE *argv, VALUE self)
if (len < 0) {
rb_raise(rb_eArgError, "negative length %ld given", len);
}
- if (len > 0 &&
- (NIL_P(ptr->string) || ptr->pos >= RSTRING_LEN(ptr->string))) {
+ if (eos_p(ptr)) {
if (!NIL_P(str)) rb_str_resize(str, 0);
- return Qnil;
+ return len > 0 ? Qnil : rb_str_new(0, 0);
}
binary = 1;
break;
@@ -1633,15 +1753,17 @@ strio_read(int argc, VALUE *argv, VALUE self)
/*
* call-seq:
- * pread(maxlen, offset) -> string
- * pread(maxlen, offset, out_string) -> string
+ * pread(maxlen, offset, out_string = nil) -> new_string or out_string
+ *
+ * :include: stringio/pread.rdoc
*
- * See IO#pread.
*/
static VALUE
strio_pread(int argc, VALUE *argv, VALUE self)
{
VALUE rb_len, rb_offset, rb_buf;
+ struct StringIO *ptr = readable(self);
+
rb_scan_args(argc, argv, "21", &rb_len, &rb_offset, &rb_buf);
long len = NUM2LONG(rb_len);
long offset = NUM2LONG(rb_offset);
@@ -1650,6 +1772,12 @@ strio_pread(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eArgError, "negative string size (or size too big): %" PRIsVALUE, rb_len);
}
+ if (offset < 0) {
+ rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
+ }
+
+ rb_buf = strio_readbuf(ptr, rb_buf);
+
if (len == 0) {
if (NIL_P(rb_buf)) {
return rb_str_new("", 0);
@@ -1657,13 +1785,7 @@ strio_pread(int argc, VALUE *argv, VALUE self)
return rb_buf;
}
- if (offset < 0) {
- rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
- }
-
- struct StringIO *ptr = readable(self);
-
- if (offset >= RSTRING_LEN(ptr->string)) {
+ if (outside_p(ptr, offset)) {
rb_eof_error();
}
@@ -1753,15 +1875,15 @@ strio_syswrite_nonblock(int argc, VALUE *argv, VALUE self)
/*
* call-seq:
- * strio.length -> integer
- * strio.size -> integer
+ * size -> integer
+ *
+ * :include: stringio/size.rdoc
*
- * Returns the size of the buffer string.
*/
static VALUE
strio_size(VALUE self)
{
- VALUE string = StringIO(self)->string;
+ VALUE string = StringIOForRead(self)->string;
if (NIL_P(string)) {
return INT2FIX(0);
}
@@ -1794,27 +1916,34 @@ strio_truncate(VALUE self, VALUE len)
}
/*
- * call-seq:
- * strio.external_encoding => encoding
+ * call-seq:
+ * external_encoding -> encoding or nil
+ *
+ * Returns an Encoding object that represents the encoding of the string;
+ * see {Encodings}[rdoc-ref:StringIO@Encodings]:
+ *
+ * strio = StringIO.new('foo')
+ * strio.external_encoding # => #<Encoding:UTF-8>
+ *
+ * Returns +nil+ if +self+ has no string and is in write mode:
+ *
+ * strio = StringIO.new(nil, 'w+')
+ * strio.external_encoding # => nil
*
- * Returns the Encoding object that represents the encoding of the file.
- * If the stream is write mode and no encoding is specified, returns
- * +nil+.
*/
static VALUE
strio_external_encoding(VALUE self)
{
- struct StringIO *ptr = StringIO(self);
+ struct StringIO *ptr = StringIOForRead(self);
return rb_enc_from_encoding(get_enc(ptr));
}
/*
* call-seq:
- * strio.internal_encoding => encoding
+ * internal_encoding -> nil
*
- * Returns the Encoding of the internal string if conversion is
- * specified. Otherwise returns +nil+.
+ * Returns +nil+; for compatibility with IO.
*/
static VALUE
@@ -1849,14 +1978,15 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self)
enc = rb_find_encoding(ext_enc);
if (!enc) {
rb_io_enc_t convconfig;
- int oflags, fmode;
+ int oflags;
+ rb_io_mode_t fmode;
VALUE vmode = rb_str_append(rb_str_new_cstr("r:"), ext_enc);
rb_io_extract_modeenc(&vmode, 0, Qnil, &oflags, &fmode, &convconfig);
enc = convconfig.enc2;
}
}
ptr->enc = enc;
- if (!NIL_P(ptr->string) && WRITABLE(self)) {
+ if (!NIL_P(ptr->string) && WRITABLE(self) && !str_chilled_p(ptr->string)) {
rb_enc_associate(ptr->string, enc);
}
@@ -1882,16 +2012,9 @@ strio_set_encoding_by_bom(VALUE self)
}
/*
- * \IO streams for strings, with access similar to
- * {IO}[rdoc-ref:IO];
- * see {IO}[rdoc-ref:IO].
- *
- * === About the Examples
- *
- * Examples on this page assume that \StringIO has been required:
- *
- * require 'stringio'
+ * :markup: markdown
*
+ * :include: stringio/stringio.md
*/
void
Init_stringio(void)
@@ -1899,7 +2022,7 @@ Init_stringio(void)
#undef rb_intern
#ifdef HAVE_RB_EXT_RACTOR_SAFE
- rb_ext_ractor_safe(true);
+ rb_ext_ractor_safe(true);
#endif
VALUE StringIO = rb_define_class("StringIO", rb_cObject);
@@ -1991,7 +2114,9 @@ Init_stringio(void)
rb_define_method(StringIO, "set_encoding_by_bom", strio_set_encoding_by_bom, 0);
{
+ /* :stopdoc: */
VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable");
+ /* :startdoc: */
rb_define_method(mReadable, "readchar", strio_readchar, 0);
rb_define_method(mReadable, "readbyte", strio_readbyte, 0);
rb_define_method(mReadable, "readline", strio_readline, -1);
@@ -2001,7 +2126,9 @@ Init_stringio(void)
rb_include_module(StringIO, mReadable);
}
{
+ /* :stopdoc: */
VALUE mWritable = rb_define_module_under(rb_cIO, "generic_writable");
+ /* :startdoc: */
rb_define_method(mWritable, "<<", strio_addstr, 1);
rb_define_method(mWritable, "print", strio_print, -1);
rb_define_method(mWritable, "printf", strio_printf, -1);