Module: RPEG::RE

Extended by:
RE
Included in:
RE
Defined in:
lib/rpeg/re.rb

Overview

A straight port of LPEG’s re module, though without any support for locales

Constant Summary collapse

Pattern =
RPEG::Pattern
PATTERN =
internals

Instance Method Summary collapse

Instance Method Details

#compile(pattern, *defs) ⇒ Object

What does “compiled” mean here?

Oh. Maybe it is the Pattern built from the regexp-y thing.



18
19
20
21
22
23
24
25
26
27
# File 'lib/rpeg/re.rb', line 18

def compile(pattern, *defs)
  return pattern if pattern.is_a?(Pattern)

  defs = [{}] if defs.empty? # for the sake of p_def, below

  cp = PATTERN.match(pattern, 0, *defs)
  raise "incorrect pattern" unless cp

  cp
end

#find(str, pattern, start_pos = 0) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rpeg/re.rb', line 34

def find(str, pattern, start_pos = 0)
  cp = @fmem[pattern]
  unless cp
    cp = compile(pattern) / 0
    cp = RPEG.P([RPEG.Cp() * cp * RPEG.Cp() + 1 * RPEG.V(0)])
    @fmem[pattern] = cp
  end

  i, e = cp.match(str, start_pos)

  return [i, e - 1] if i
end

#gsub(str, pattern, rep) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rpeg/re.rb', line 47

def gsub(str, pattern, rep)
  g = @gmem[pattern] || {} #-- ensure gmem[p] is not collected while here. What does that mean?
  @gmem[pattern] = g
  cp = g[rep]
  unless cp
    cp = compile(pattern)
    cp = RPEG.Cs((cp / rep + 1)**0)
    g[rep] = cp
  end
  cp.match(str)
end

#match(str, pattern, start_pos = 0) ⇒ Object



29
30
31
32
# File 'lib/rpeg/re.rb', line 29

def match(str, pattern, start_pos = 0)
  cp = (@mem[pattern] ||= compile(pattern))
  cp.match(str, start_pos)
end