Class: StringScanner

Inherits:
Object show all
Defined in:
lib/rmtools/text/string_scanner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lastObject (readonly)

Returns the value of attribute last.



5
6
7
# File 'lib/rmtools/text/string_scanner.rb', line 5

def last
  @last
end

Class Method Details

.each(string, *args, &b) ⇒ Object



87
88
89
# File 'lib/rmtools/text/string_scanner.rb', line 87

def self.each string, *args, &b
  new(string).each *args, &b
end

Instance Method Details

#+Object



83
# File 'lib/rmtools/text/string_scanner.rb', line 83

def +; string[pos, 1] end

#-Object



85
# File 'lib/rmtools/text/string_scanner.rb', line 85

def -; string[pos-matched_size-1, 1] end

#each(re, cbs = nil, &cb) ⇒ Object

#each( <Regexp>, { <0..255 | :~ | nil> => ->{|self|}, … } ) #each( <Regexp>, [ [ <Regexp>, ->{|self, <MatchData>|} ], … ] ) #each( <Regexp> ) {|self|} Example:

ss = StringScanner.new xpath
ss.each %r{\[-?\d+\]|\{[^\}]+\}}, 
  ?[ => lambda {|ss| 
    if node; node = FindByIndex[node, nslist, ss]
    else     return []     end  }, 
  ?{ => lambda {|ss| 
    if node; node = FindByProc[node, nslist, ss]
    else     return []     end  },
  nil => lambda {|str|
    node = node.is(Array) ?
      node.sum {|n| n.__find(str, nslist).to_a} : node.__find(str, nslist) 
  }


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rmtools/text/string_scanner.rb', line 24

def each(re, cbs=nil, &cb)
  @last = pos
  res = scan_until re
  if cbs
    if cbs.is Hash
      while res
        if cb = cbs[matched[0]] || cbs[:~]
          cb[self]
          @last = pos
          res = !eos? && scan_until(re)
        else break
        end
      end
      if !eos? and cb = cbs[nil]
        cb[tail]
      end
    else
      while res
        if cb = cbs.find {|pattern, fun| pattern and matched[pattern]}
          # patterns should be as explicit as possible
          cb[1][self, $~] if cb[1]
          @last = pos
          res = !eos? && scan_until(re)
        else break
        end
      end
      if !eos? and cb = cbs.find {|pair| pair[0].nil?}
        cb[1][tail]
      end
    end
  else
    while res
      cb[self]
      @last = pos
      res = !eos? && scan_until(re)
    end
  end
end

#headObject



63
64
65
# File 'lib/rmtools/text/string_scanner.rb', line 63

def head
  string[@last...pos-matched_size.to_i]
end

#hl_next(re) ⇒ Object



71
72
73
# File 'lib/rmtools/text/string_scanner.rb', line 71

def hl_next(re)
  (res = scan_until re) && Painter.hl(string[[pos-1000, 0].max..pos+1000], res)
end

#next_in(n) ⇒ Object



75
76
77
# File 'lib/rmtools/text/string_scanner.rb', line 75

def next_in(n)
  string[pos+n-1, 1]
end

#prev_in(n) ⇒ Object



79
80
81
# File 'lib/rmtools/text/string_scanner.rb', line 79

def prev_in(n)
  string[pos-matched_size-n, 1]
end

#tailObject



67
68
69
# File 'lib/rmtools/text/string_scanner.rb', line 67

def tail 
  post_match || string[pos..-1]
end