variable.c: matched backrefs only
* variable.c (rb_f_global_variables): add matched back references only, as well as defiend? operator. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53534 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1301d1f5bc
commit
92e803c9c8
@ -1,3 +1,8 @@
|
|||||||
|
Thu Jan 14 17:36:16 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* variable.c (rb_f_global_variables): add matched back references
|
||||||
|
only, as well as defiend? operator.
|
||||||
|
|
||||||
Thu Jan 14 16:12:09 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Thu Jan 14 16:12:09 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* sprintf.c (rb_str_format): format exact number more exactly.
|
* sprintf.c (rb_str_format): format exact number more exactly.
|
||||||
|
@ -1104,6 +1104,8 @@ VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourcel
|
|||||||
VALUE rb_reg_check_preprocess(VALUE);
|
VALUE rb_reg_check_preprocess(VALUE);
|
||||||
long rb_reg_search0(VALUE, VALUE, long, int, int);
|
long rb_reg_search0(VALUE, VALUE, long, int, int);
|
||||||
void rb_backref_set_string(VALUE string, long pos, long len);
|
void rb_backref_set_string(VALUE string, long pos, long len);
|
||||||
|
int rb_match_count(VALUE match);
|
||||||
|
int rb_match_nth_defined(int nth, VALUE match);
|
||||||
|
|
||||||
/* signal.c */
|
/* signal.c */
|
||||||
extern int ruby_enable_coredump;
|
extern int ruby_enable_coredump;
|
||||||
|
27
re.c
27
re.c
@ -1265,6 +1265,33 @@ rb_match_busy(VALUE match)
|
|||||||
FL_SET(match, MATCH_BUSY);
|
FL_SET(match, MATCH_BUSY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
rb_match_count(VALUE match)
|
||||||
|
{
|
||||||
|
struct re_registers *regs;
|
||||||
|
if (NIL_P(match)) return -1;
|
||||||
|
regs = RMATCH_REGS(match);
|
||||||
|
if (!regs) return -1;
|
||||||
|
return regs->num_regs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
rb_match_nth_defined(int nth, VALUE match)
|
||||||
|
{
|
||||||
|
struct re_registers *regs;
|
||||||
|
if (NIL_P(match)) return FALSE;
|
||||||
|
regs = RMATCH_REGS(match);
|
||||||
|
if (!regs) return FALSE;
|
||||||
|
if (nth >= regs->num_regs) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if (nth < 0) {
|
||||||
|
nth += regs->num_regs;
|
||||||
|
if (nth <= 0) return FALSE;
|
||||||
|
}
|
||||||
|
return (BEG(nth) != -1);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
match_set_string(VALUE m, VALUE string, long pos, long len)
|
match_set_string(VALUE m, VALUE string, long pos, long len)
|
||||||
{
|
{
|
||||||
|
@ -105,9 +105,13 @@ class TestVariable < Test::Unit::TestCase
|
|||||||
assert_empty(gv.grep(/\A(?!\$)/))
|
assert_empty(gv.grep(/\A(?!\$)/))
|
||||||
assert_nil($~)
|
assert_nil($~)
|
||||||
assert_not_include(gv, :$1)
|
assert_not_include(gv, :$1)
|
||||||
/.*/ =~ "global"
|
/(\w)(\d)?(.)(.)(.)(.)(.)(.)(.)(.)(\d)?(.)/ =~ "globalglobalglobal"
|
||||||
assert_not_nil($~)
|
assert_not_nil($~)
|
||||||
assert_include(global_variables-gv, :$1)
|
gv = global_variables - gv
|
||||||
|
assert_include(gv, :$1)
|
||||||
|
assert_not_include(gv, :$2)
|
||||||
|
assert_not_include(gv, :$11)
|
||||||
|
assert_include(gv, :$12)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_global_variable_0
|
def test_global_variable_0
|
||||||
|
20
variable.c
20
variable.c
@ -880,15 +880,25 @@ VALUE
|
|||||||
rb_f_global_variables(void)
|
rb_f_global_variables(void)
|
||||||
{
|
{
|
||||||
VALUE ary = rb_ary_new();
|
VALUE ary = rb_ary_new();
|
||||||
|
VALUE sym, backref = rb_backref_get();
|
||||||
|
|
||||||
rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary);
|
rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary);
|
||||||
if (!NIL_P(rb_backref_get())) {
|
if (!NIL_P(backref)) {
|
||||||
char buf[2];
|
char buf[2];
|
||||||
int i;
|
int i, nmatch = rb_match_count(backref);
|
||||||
buf[0] = '$';
|
buf[0] = '$';
|
||||||
for (i = 1; i <= 9; ++i) {
|
for (i = 1; i <= nmatch; ++i) {
|
||||||
buf[1] = (char)(i + '0');
|
if (!rb_match_nth_defined(i, backref)) continue;
|
||||||
rb_ary_push(ary, ID2SYM(rb_intern2(buf, 2)));
|
if (i < 10) {
|
||||||
|
/* probably reused, make static ID */
|
||||||
|
buf[1] = (char)(i + '0');
|
||||||
|
sym = ID2SYM(rb_intern2(buf, 2));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* dynamic symbol */
|
||||||
|
sym = rb_str_intern(rb_sprintf("$%d", i));
|
||||||
|
}
|
||||||
|
rb_ary_push(ary, sym);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ary;
|
return ary;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user