Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions doc/stringio/getc.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Reads and returns the next character (or byte; see below) from the stream:

strio = StringIO.new('foo')
strio.getc # => "f"
strio.getc # => "o"
strio.getc # => "o"

Returns +nil+ if at end-of-stream:

strio.eof? # => true
strio.getc # => nil

Returns characters, not bytes:

strio = StringIO.new('тест')
strio.getc # => "т"
strio.getc # => "е"

strio = StringIO.new('こんにちは')
strio.getc # => "こ"
strio.getc # => "ん"

In each of the examples above, the stream is positioned at the beginning of a character;
in other cases that need not be true:

strio = StringIO.new('こんにちは') # Five 3-byte characters.
strio.pos = 3 # => 3 # At beginning of second character; returns character.
strio.getc # => "ん"
strio.pos = 4 # => 4 # At second byte of second character; returns byte.
strio.getc # => "\x82"
strio.pos = 5 # => 5 # At third byte of second character; returns byte.
strio.getc # => "\x93"

Related: StringIO.getbyte.
23 changes: 11 additions & 12 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ 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)
Expand All @@ -434,7 +434,7 @@ strio_false(VALUE self)
}

/*
* Returns +nil+. Just for compatibility to IO.
* Returns +nil+; for compatibility with IO.
*/
static VALUE
strio_nil(VALUE self)
Expand All @@ -444,7 +444,7 @@ strio_nil(VALUE self)
}

/*
* Returns an object itself. Just for compatibility to IO.
* Returns +self+; for compatibility with IO.
*/
static VALUE
strio_self(VALUE self)
Expand All @@ -454,7 +454,7 @@ strio_self(VALUE self)
}

/*
* Returns 0. Just for compatibility to IO.
* Returns 0; for compatibility with IO.
*/
static VALUE
strio_0(VALUE self)
Expand Down Expand Up @@ -514,7 +514,7 @@ strio_get_string(VALUE self)
* 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|
Expand All @@ -528,7 +528,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)
Expand Down Expand Up @@ -964,10 +964,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)
Expand Down Expand Up @@ -2071,10 +2071,9 @@ strio_external_encoding(VALUE self)

/*
* 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
Expand Down
88 changes: 0 additions & 88 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ static VALUE strscan_init_copy _((VALUE vself, VALUE vorig));

static VALUE strscan_s_mustc _((VALUE self));
static VALUE strscan_terminate _((VALUE self));
static VALUE strscan_clear _((VALUE self));
static VALUE strscan_get_string _((VALUE self));
static VALUE strscan_set_string _((VALUE self, VALUE str));
static VALUE strscan_concat _((VALUE self, VALUE str));
Expand All @@ -118,14 +117,11 @@ static VALUE strscan_search_full _((VALUE self, VALUE re,
static void adjust_registers_to_matched _((struct strscanner *p));
static VALUE strscan_getch _((VALUE self));
static VALUE strscan_get_byte _((VALUE self));
static VALUE strscan_getbyte _((VALUE self));
static VALUE strscan_peek _((VALUE self, VALUE len));
static VALUE strscan_peep _((VALUE self, VALUE len));
static VALUE strscan_scan_base10_integer _((VALUE self));
static VALUE strscan_unscan _((VALUE self));
static VALUE strscan_bol_p _((VALUE self));
static VALUE strscan_eos_p _((VALUE self));
static VALUE strscan_empty_p _((VALUE self));
static VALUE strscan_rest_p _((VALUE self));
static VALUE strscan_matched_p _((VALUE self));
static VALUE strscan_matched _((VALUE self));
Expand Down Expand Up @@ -384,21 +380,6 @@ strscan_terminate(VALUE self)
return self;
}

/*
* call-seq:
* clear -> self
*
* This method is obsolete; use the equivalent method StringScanner#terminate.
*/

/* :nodoc: */
static VALUE
strscan_clear(VALUE self)
{
rb_warning("StringScanner#clear is obsolete; use #terminate instead");
return strscan_terminate(self);
}

/*
* :markup: markdown
* :include: strscan/link_refs.txt
Expand Down Expand Up @@ -1217,22 +1198,6 @@ strscan_get_byte(VALUE self)
adjust_register_position(p, p->regs.end[0]));
}

/*
* call-seq:
* getbyte
*
* Equivalent to #get_byte.
* This method is obsolete; use #get_byte instead.
*/

/* :nodoc: */
static VALUE
strscan_getbyte(VALUE self)
{
rb_warning("StringScanner#getbyte is obsolete; use #get_byte instead");
return strscan_get_byte(self);
}

/*
* :markup: markdown
* :include: strscan/link_refs.txt
Expand Down Expand Up @@ -1268,22 +1233,6 @@ strscan_peek(VALUE self, VALUE vlen)
return extract_beg_len(p, p->curr, len);
}

/*
* call-seq:
* peep
*
* Equivalent to #peek.
* This method is obsolete; use #peek instead.
*/

/* :nodoc: */
static VALUE
strscan_peep(VALUE self, VALUE vlen)
{
rb_warning("StringScanner#peep is obsolete; use #peek instead");
return strscan_peek(self, vlen);
}

static VALUE
strscan_parse_integer(struct strscanner *p, int base, long len)
{
Expand Down Expand Up @@ -1523,22 +1472,6 @@ strscan_eos_p(VALUE self)
return EOS_P(p) ? Qtrue : Qfalse;
}

/*
* call-seq:
* empty?
*
* Equivalent to #eos?.
* This method is obsolete, use #eos? instead.
*/

/* :nodoc: */
static VALUE
strscan_empty_p(VALUE self)
{
rb_warning("StringScanner#empty? is obsolete; use #eos? instead");
return strscan_eos_p(self);
}

/*
* call-seq:
* rest?
Expand Down Expand Up @@ -2052,22 +1985,6 @@ strscan_rest_size(VALUE self)
return INT2FIX(i);
}

/*
* call-seq:
* restsize
*
* <tt>s.restsize</tt> is equivalent to <tt>s.rest_size</tt>.
* This method is obsolete; use #rest_size instead.
*/

/* :nodoc: */
static VALUE
strscan_restsize(VALUE self)
{
rb_warning("StringScanner#restsize is obsolete; use #rest_size instead");
return strscan_rest_size(self);
}

#define INSPECT_LENGTH 5

/*
Expand Down Expand Up @@ -2308,7 +2225,6 @@ Init_strscan(void)
rb_define_singleton_method(StringScanner, "must_C_version", strscan_s_mustc, 0);
rb_define_method(StringScanner, "reset", strscan_reset, 0);
rb_define_method(StringScanner, "terminate", strscan_terminate, 0);
rb_define_method(StringScanner, "clear", strscan_clear, 0);
rb_define_method(StringScanner, "string", strscan_get_string, 0);
rb_define_method(StringScanner, "string=", strscan_set_string, 1);
rb_define_method(StringScanner, "concat", strscan_concat, 1);
Expand All @@ -2333,11 +2249,9 @@ Init_strscan(void)

rb_define_method(StringScanner, "getch", strscan_getch, 0);
rb_define_method(StringScanner, "get_byte", strscan_get_byte, 0);
rb_define_method(StringScanner, "getbyte", strscan_getbyte, 0);
rb_define_method(StringScanner, "scan_byte", strscan_scan_byte, 0);
rb_define_method(StringScanner, "peek", strscan_peek, 1);
rb_define_method(StringScanner, "peek_byte", strscan_peek_byte, 0);
rb_define_method(StringScanner, "peep", strscan_peep, 1);

rb_define_private_method(StringScanner, "scan_base10_integer", strscan_scan_base10_integer, 0);
rb_define_private_method(StringScanner, "scan_base16_integer", strscan_scan_base16_integer, 0);
Expand All @@ -2347,7 +2261,6 @@ Init_strscan(void)
rb_define_method(StringScanner, "beginning_of_line?", strscan_bol_p, 0);
rb_alias(StringScanner, rb_intern("bol?"), rb_intern("beginning_of_line?"));
rb_define_method(StringScanner, "eos?", strscan_eos_p, 0);
rb_define_method(StringScanner, "empty?", strscan_empty_p, 0);
rb_define_method(StringScanner, "rest?", strscan_rest_p, 0);

rb_define_method(StringScanner, "matched?", strscan_matched_p, 0);
Expand All @@ -2362,7 +2275,6 @@ Init_strscan(void)

rb_define_method(StringScanner, "rest", strscan_rest, 0);
rb_define_method(StringScanner, "rest_size", strscan_rest_size, 0);
rb_define_method(StringScanner, "restsize", strscan_restsize, 0);

rb_define_method(StringScanner, "inspect", strscan_inspect, 0);

Expand Down
1 change: 1 addition & 0 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void
saved.finished = 0;
saved.final = Qundef;

ASSERT_vm_unlocking();
rb_ractor_ignore_belonging(true);
EC_PUSH_TAG(ec);
enum ruby_tag_type state = EC_EXEC_TAG();
Expand Down
22 changes: 14 additions & 8 deletions gc/default/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -2763,24 +2763,27 @@ rb_gc_impl_define_finalizer(void *objspace_ptr, VALUE obj, VALUE block)

RBASIC(obj)->flags |= FL_FINALIZE;

int lev = RB_GC_VM_LOCK();
unsigned int lev = RB_GC_VM_LOCK();

if (st_lookup(finalizer_table, obj, &data)) {
table = (VALUE)data;
VALUE dup_table = rb_ary_dup(table);

RB_GC_VM_UNLOCK(lev);
/* avoid duplicate block, table is usually small */
{
long len = RARRAY_LEN(table);
long i;

for (i = 0; i < len; i++) {
VALUE recv = RARRAY_AREF(table, i);
if (rb_equal(recv, block)) {
RB_GC_VM_UNLOCK(lev);
VALUE recv = RARRAY_AREF(dup_table, i);
if (rb_equal(recv, block)) { // can't be called with VM lock held
return recv;
}
}
}
lev = RB_GC_VM_LOCK();
RB_GC_GUARD(dup_table);

rb_ary_push(table, block);
}
Expand Down Expand Up @@ -2841,8 +2844,8 @@ get_final(long i, void *data)
return RARRAY_AREF(table, i + 1);
}

static void
run_final(rb_objspace_t *objspace, VALUE zombie)
static unsigned int
run_final(rb_objspace_t *objspace, VALUE zombie, unsigned int lev)
{
if (RZOMBIE(zombie)->dfree) {
RZOMBIE(zombie)->dfree(RZOMBIE(zombie)->data);
Expand All @@ -2853,7 +2856,9 @@ run_final(rb_objspace_t *objspace, VALUE zombie)
FL_UNSET(zombie, FL_FINALIZE);
st_data_t table;
if (st_delete(finalizer_table, &key, &table)) {
RB_GC_VM_UNLOCK(lev);
rb_gc_run_obj_finalizer(RARRAY_AREF(table, 0), RARRAY_LEN(table) - 1, get_final, (void *)table);
lev = RB_GC_VM_LOCK();
}
else {
rb_bug("FL_FINALIZE flag is set, but finalizers are not found");
Expand All @@ -2862,6 +2867,7 @@ run_final(rb_objspace_t *objspace, VALUE zombie)
else {
GC_ASSERT(!st_lookup(finalizer_table, key, NULL));
}
return lev;
}

static void
Expand All @@ -2874,9 +2880,9 @@ finalize_list(rb_objspace_t *objspace, VALUE zombie)
next_zombie = RZOMBIE(zombie)->next;
page = GET_HEAP_PAGE(zombie);

int lev = RB_GC_VM_LOCK();
unsigned int lev = RB_GC_VM_LOCK();

run_final(objspace, zombie);
lev = run_final(objspace, zombie, lev);
{
GC_ASSERT(BUILTIN_TYPE(zombie) == T_ZOMBIE);
GC_ASSERT(page->heap->final_slots_count > 0);
Expand Down
18 changes: 0 additions & 18 deletions spec/ruby/library/stringscanner/clear_spec.rb

This file was deleted.

Loading