Skip to content

Commit

Permalink
regex.c
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/v1_1r@235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Jun 8, 1998
1 parent 5260c74 commit 6d32141
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 49 deletions.
18 changes: 16 additions & 2 deletions lib/delegate.rb
Expand Up @@ -21,7 +21,17 @@ def initialize(obj)
preserved -= ["__getobj__","to_s","nil?","to_a","hash","dup","==","=~"]
for method in obj.methods
next if preserved.include? method
eval "def self.#{method}(*args,&block); __getobj__.__send__(:#{method}, *args,&block); end"
eval <<EOS
def self.#{method}(*args, &block)
begin
__getobj__.__send__(:#{method}, *args, &block)
rescue Exception
n = if /:in `__getobj__'$/ =~ $@[0] then 1 else 2 end #`
$@[1,n] = nil
raise
end
end
EOS
end
end

Expand Down Expand Up @@ -53,6 +63,10 @@ def __setobj__(obj)

if __FILE__ == $0
foo = Object.new
def foo.test
raise 'this is OK'
end
foo2 = SimpleDelegator.new(foo)
p foo.hash == foo2.hash # => true
p foo.hash == foo2.hash # => true
foo.test # raise error!
end
13 changes: 4 additions & 9 deletions lib/weakref.rb
Expand Up @@ -41,14 +41,9 @@ def initialize(orig)

def __getobj__
unless ID_MAP[@__id]
$@ = caller(1)
$! = RefError.new("Illegal Reference - probably recycled")
raise
raise RefError, "Illegal Reference - probably recycled", caller(2)
end
ObjectSpace._id2ref(@__id)
# ObjectSpace.each_object do |obj|
# return obj if obj.id == @__id
# end
end

def weakref_alive?
Expand All @@ -66,9 +61,9 @@ def []

if __FILE__ == $0
foo = Object.new
p foo.hash
p foo.hash # original's hash value
foo = WeakRef.new(foo)
p foo.hash
p foo.hash # should be same hash value
ObjectSpace.garbage_collect
p foo.hash
p foo.hash # should raise exception (recycled)
end
66 changes: 35 additions & 31 deletions regex.c
Expand Up @@ -897,8 +897,6 @@ calculate_must_string(start, end)
case casefold_off:
return 0; /* should not check must_string */

case start_nowidth:
case stop_nowidth:
case pop_and_fail:
case anychar:
case begline:
Expand Down Expand Up @@ -943,6 +941,8 @@ calculate_must_string(start, end)
if (mcnt > 0) p += mcnt;
break;

case start_nowidth:
case stop_nowidth:
case finalize_jump:
case maybe_finalize_jump:
case finalize_push:
Expand Down Expand Up @@ -1622,7 +1622,7 @@ re_compile_pattern(pattern, size, bufp)
case '|':
/* Insert before the previous alternative a jump which
jumps to this alternative if the former fails. */
GET_BUFFER_SPACE(6);
GET_BUFFER_SPACE(3);
insert_jump(on_failure_jump, begalt, b + 6, b);
pending_exact = 0;
b += 3;
Expand All @@ -1645,9 +1645,11 @@ re_compile_pattern(pattern, size, bufp)
if (fixup_alt_jump)
store_jump(fixup_alt_jump, jump_past_alt, b);

/* Leave space for a jump after previous alternative---to be
filled in later. */
/* Mark and leave space for a jump after this alternative,
to be filled in later either by next alternative or
when know we're at the end of a series of alternatives. */
fixup_alt_jump = b;
GET_BUFFER_SPACE(3);
b += 3;

laststart = 0;
Expand Down Expand Up @@ -2630,8 +2632,13 @@ re_search(bufp, string, size, startpos, range, regs)
}
}

if (anchor && startpos > 0 && startpos < size
&& string[startpos-1] != '\n') goto advance;
if (anchor && startpos < size && string[startpos-1] != '\n') {
while (range > 0 && string[startpos] != '\n') {
range--;
startpos++;
}
goto advance;
}

if (fastmap && startpos == size && range >= 0
&& (bufp->can_be_null == 0 ||
Expand Down Expand Up @@ -2725,26 +2732,25 @@ typedef union
/* Macros used by re_match: */

/* I.e., regstart, regend, and reg_info. */

#define NUM_REG_ITEMS 3

/* Individual items aside from the registers. */
#define NUM_NONREG_ITEMS 3

/* We push at most this many things on the stack whenever we
fail. The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
arguments to the PUSH_FAILURE_POINT macro. */

#define MAX_NUM_FAILURE_ITEMS (num_regs * NUM_REG_ITEMS + 2)

#define MAX_NUM_FAILURE_ITEMS (num_regs * NUM_REG_ITEMS + NUM_NONREG_ITEMS)

/* We push this many things on the stack whenever we fail. */

#define NUM_FAILURE_ITEMS (last_used_reg * NUM_REG_ITEMS + 2)
#define NUM_FAILURE_ITEMS (last_used_reg * NUM_REG_ITEMS + NUM_REG_ITEMS)


/* This pushes most of the information about the current state we will want
if we ever fail back to it. */

#define PUSH_FAILURE_POINT(pattern_place, string_place) \
{ \
do { \
long last_used_reg, this_reg; \
\
/* Find out how many registers are active or have been matched. \
Expand Down Expand Up @@ -2781,19 +2787,19 @@ typedef union
*stackp++ = pattern_place; \
*stackp++ = string_place; \
*stackp++ = (unsigned char *)0; /* non-greedy flag */ \
}
} while(0)


/* This pops what PUSH_FAILURE_POINT pushes. */

#define POP_FAILURE_POINT() \
{ \
do { \
int temp; \
stackp -= 3; /* Remove failure points (and flag). */ \
stackp -= NUM_NONREG_ITEMS; /* Remove failure points (and flag). */ \
temp = (int) *--stackp; /* How many regs pushed. */ \
temp *= NUM_REG_ITEMS; /* How much to take off the stack. */ \
stackp -= temp; /* Remove the register info. */ \
}
} while(0)

/* Registers are set to a sentinel when they haven't yet matched. */
#define REG_UNSET_VALUE ((unsigned char *) -1)
Expand All @@ -2805,7 +2811,7 @@ typedef union
registers corresponding to the subexpressions of which we currently
are inside. */
#define SET_REGS_MATCHED \
{ unsigned this_reg; \
do { unsigned this_reg; \
for (this_reg = 0; this_reg < num_regs; this_reg++) \
{ \
if (IS_ACTIVE(reg_info[this_reg])) \
Expand All @@ -2815,7 +2821,7 @@ typedef union
else \
MATCHED_SOMETHING(reg_info[this_reg]) = 0; \
} \
}
} while(0)

#define AT_STRINGS_BEG(d) (d == string)
#define AT_STRINGS_END(d) (d == dend)
Expand Down Expand Up @@ -2891,7 +2897,6 @@ re_match(bufp, string_arg, size, pos, regs)
register unsigned char *d, *dend;
register int mcnt; /* Multipurpose. */
int options = bufp->options;
unsigned is_a_jump_n = 0;

/* Failure point stack. Each place that can handle a failure further
down the line pushes a failure point on this stack. It consists of
Expand Down Expand Up @@ -2998,7 +3003,6 @@ re_match(bufp, string_arg, size, pos, regs)
p - (unsigned char *) bufp->buffer,
*p);
#endif
is_a_jump_n = 0;
/* End of pattern means we might have succeeded. */
if (p == pend)
{
Expand Down Expand Up @@ -3114,7 +3118,8 @@ re_match(bufp, string_arg, size, pos, regs)
|| (enum regexpcode) p[-3] == start_memory)
&& (p + 1) != pend)
{
register unsigned char *p2 = p + 1;
int is_a_jump_n = 0;
register unsigned char *p2 = p + 2;
mcnt = 0;
switch (*p2++)
{
Expand All @@ -3136,8 +3141,8 @@ re_match(bufp, string_arg, size, pos, regs)
to an on_failure_jump, exit from the loop by forcing a
failure after pushing on the stack the on_failure_jump's
jump in the pattern, and d. */
if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump
&& (enum regexpcode) p1[3] == start_memory && p1[4] == *p)
if (mcnt < 0 && (enum regexpcode) *p2 == on_failure_jump
&& (enum regexpcode) p2[3] == start_memory && p2[4] == *p)
{
/* If this group ever matched anything, then restore
what its registers were before trying this last
Expand Down Expand Up @@ -3654,8 +3659,8 @@ re_match(bufp, string_arg, size, pos, regs)
/* Make the ones that weren't saved -1 or 0 again. */
for (this_reg = num_regs - 1; this_reg > last_used_reg; this_reg--)
{
regend[this_reg] = (unsigned char *)(-1L);
regstart[this_reg] = (unsigned char *)(-1L);
regend[this_reg] = REG_UNSET_VALUE;
regstart[this_reg] = REG_UNSET_VALUE;
IS_ACTIVE(reg_info[this_reg]) = 0;
MATCHED_SOMETHING(reg_info[this_reg]) = 0;
}
Expand All @@ -3667,7 +3672,6 @@ re_match(bufp, string_arg, size, pos, regs)
regend[this_reg] = *--stackp;
regstart[this_reg] = *--stackp;
}

if (p < pend)
{
int is_a_jump_n = 0;
Expand All @@ -3684,7 +3688,7 @@ re_match(bufp, string_arg, size, pos, regs)
case finalize_jump:
case finalize_push:
case jump:
p1 = p + 1;
p1++;
EXTRACT_NUMBER_AND_INCR (mcnt, p1);
p1 += mcnt;

Expand Down Expand Up @@ -3799,10 +3803,10 @@ group_match_null_string_p (p, end, reg_info)
of the `jump_past_alt' just before it. `mcnt' contains
the length of the alternative. */
EXTRACT_NUMBER (mcnt, p1 - 2);

#if 0
if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
return 0;

#endif
p1 += mcnt; /* Get past the n-th alternative. */
} /* if mcnt > 0 */
break;
Expand Down
15 changes: 8 additions & 7 deletions sample/observ.rb
Expand Up @@ -7,25 +7,26 @@ class Tick
include Observable
def initialize
Thread.start do
while TRUE
loop do
sleep 0.999
now = Time.now
changed
notify_observers(Time.now.strftime("%H:%M:%S"))
notify_observers(now.hour, now.min, now.sec)
end
end
end
end

class Clock
def initialize
@tick = Tick.new
def initialize(tick)
@tick = tick
@tick.add_observer(self)
end
def update(time)
print "\e[8D", time
def update(h, m, s)
printf "\e[8D%02d:%02d:%02d", h, m, s
STDOUT.flush
end
end

clock = Clock.new
clock = Clock.new(Tick.new)
sleep

0 comments on commit 6d32141

Please sign in to comment.