* ext/strscan/strscan.c: new method StringScanner#initialize_copy to allow #dup and #clone.

* test/strscan/test_strscan.rb: test StringScanner#dup.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5889 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2004-03-05 11:20:12 +00:00
Родитель 87275504ab
Коммит 492dfc7c08
3 изменённых файлов: 104 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Fri Mar 5 20:10:57 2004 Minero Aoki <aamine@loveruby.net>
* ext/strscan/strscan.c: new method StringScanner#initialize_copy
to allow #dup and #clone.
* test/strscan/test_strscan.rb: test StringScanner#dup.
Fri Mar 5 19:42:09 2004 Minero Aoki <aamine@loveruby.net>
* lib/net/http.rb (HTTPResponse#to_ary): should return an object
@ -6,7 +13,7 @@ Fri Mar 5 19:42:09 2004 Minero Aoki <aamine@loveruby.net>
Fri Mar 5 00:51:35 2004 Dave Thomas <dave@pragprog.com>
* lib/test/unit.rb: MOve RDoc documentation so that you can
* lib/test/unit.rb: Move RDoc documentation so that you can
now say 'ri Test::Unit'
Thu Mar 4 22:31:40 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>

Просмотреть файл

@ -73,10 +73,12 @@ static VALUE infect _((VALUE str, struct strscanner *p));
static VALUE extract_range _((struct strscanner *p, long beg_i, long end_i));
static VALUE extract_beg_len _((struct strscanner *p, long beg_i, long len));
void check_strscan _((VALUE obj));
static void strscan_mark _((struct strscanner *p));
static void strscan_free _((struct strscanner *p));
static VALUE strscan_s_allocate _((VALUE klass));
static VALUE strscan_initialize _((int argc, VALUE *argv, VALUE self));
static VALUE strscan_init_copy _((VALUE vself, VALUE vorig));
static VALUE strscan_s_mustc _((VALUE self));
static VALUE strscan_terminate _((VALUE self));
@ -217,6 +219,38 @@ strscan_initialize(argc, argv, self)
return self;
}
void
check_strscan(obj)
VALUE obj;
{
if (TYPE(obj) != T_DATA || RDATA(obj)->dmark != (RUBY_DATA_FUNC)strscan_mark) {
rb_raise(rb_eTypeError,
"wrong argument type %s (expected StringScanner)",
rb_obj_classname(obj));
}
}
/*
* call-seq:
* dup
* clone
*
* Duplicates a StringScanner object.
*/
static VALUE
strscan_init_copy(vself, vorig)
VALUE vself, vorig;
{
struct strscanner *self, *orig;
Data_Get_Struct(vself, struct strscanner, self);
check_strscan(vorig);
Data_Get_Struct(vorig, struct strscanner, orig);
memmove(self, orig, sizeof(struct strscanner));
return vself;
}
/* =======================================================================
Instance Methods
@ -1291,6 +1325,7 @@ Init_strscan()
rb_define_alloc_func(StringScanner, strscan_s_allocate);
rb_define_private_method(StringScanner, "initialize", strscan_initialize, -1);
rb_define_private_method(StringScanner, "initialize_copy", strscan_init_copy, 1);
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);

Просмотреть файл

@ -45,6 +45,44 @@ class TestStringScanner < Test::Unit::TestCase
}
end
def test_dup
s = StringScanner.new('test string')
d = s.dup
assert_equal s.inspect, d.inspect
assert_equal s.string, d.string
assert_equal s.pos, d.pos
assert_equal s.matched?, d.matched?
assert_equal s.eos?, d.eos?
s = StringScanner.new('test string')
s.scan(/test/)
d = s.dup
assert_equal s.inspect, d.inspect
assert_equal s.string, d.string
assert_equal s.pos, d.pos
assert_equal s.matched?, d.matched?
assert_equal s.eos?, d.eos?
s = StringScanner.new('test string')
s.scan(/test/)
s.scan(/NOT MATCH/)
d = s.dup
assert_equal s.inspect, d.inspect
assert_equal s.string, d.string
assert_equal s.pos, d.pos
assert_equal s.matched?, d.matched?
assert_equal s.eos?, d.eos?
s = StringScanner.new('test string')
s.terminate
d = s.dup
assert_equal s.inspect, d.inspect
assert_equal s.string, d.string
assert_equal s.pos, d.pos
assert_equal s.matched?, d.matched?
assert_equal s.eos?, d.eos?
end
def test_const_Version
assert_instance_of String, StringScanner::Version
assert_equal true, StringScanner::Version.frozen?
@ -484,4 +522,27 @@ class TestStringScanner < Test::Unit::TestCase
s.reset
assert_equal 0, s.pos
end
def test_matched_size
s = StringScanner.new('test string')
assert_nil s.matched_size
s.scan(/test/)
assert_equal 4, s.matched_size
assert_equal 4, s.matched_size
s.scan(//)
assert_equal 0, s.matched_size
s.scan(/x/)
assert_nil s.matched_size
assert_nil s.matched_size
s.terminate
assert_nil s.matched_size
# obsolete
s = StringScanner.new('test string')
assert_nil s.matchedsize
s.scan(/test/)
assert_equal 4, s.matched_size
s.terminate
assert_nil s.matched_size
end
end