Class: StringScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysl/strscan/strscan.rb

Constant Summary collapse

Id =
"None$Id".freeze
Version =
"1.0.0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, dup = false) ⇒ StringScanner

Returns a new instance of StringScanner.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubysl/strscan/strscan.rb', line 80

def initialize(string, dup=false)
  if string.instance_of? String
    @original = string
    @string = string
  else
    @original = StringValue(string)
    @string = String.new @original
  end

  reset_state
end

Instance Attribute Details

#matchObject (readonly)

Returns the value of attribute match.



7
8
9
# File 'lib/rubysl/strscan/strscan.rb', line 7

def match
  @match
end

#posObject Also known as: pointer

Returns the value of attribute pos.



7
8
9
# File 'lib/rubysl/strscan/strscan.rb', line 7

def pos
  @pos
end

#prev_posObject (readonly)

Returns the value of attribute prev_pos.



7
8
9
# File 'lib/rubysl/strscan/strscan.rb', line 7

def prev_pos
  @prev_pos
end

Class Method Details

.must_C_versionObject



196
197
198
# File 'lib/rubysl/strscan/strscan.rb', line 196

def self.must_C_version
  self
end

Instance Method Details

#[](n) ⇒ Object

Raises:

  • (TypeError)


24
25
26
27
# File 'lib/rubysl/strscan/strscan.rb', line 24

def [](n)
  raise TypeError, "Bad argument #{n.inspect}" unless n.respond_to? :to_int
  @match[n] if @match
end

#bol?Boolean Also known as: beginning_of_line?

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubysl/strscan/strscan.rb', line 29

def bol?
  @pos == 0 or @string.getbyte(pos-1) == 10
end

#check(pattern) ⇒ Object



35
36
37
# File 'lib/rubysl/strscan/strscan.rb', line 35

def check(pattern)
  _scan pattern, false, true, true
end

#check_until(pattern) ⇒ Object



39
40
41
# File 'lib/rubysl/strscan/strscan.rb', line 39

def check_until(pattern)
  _scan pattern, false, true, false
end

#clearObject



43
44
45
46
# File 'lib/rubysl/strscan/strscan.rb', line 43

def clear
  warn "StringScanner#clear is obsolete; use #terminate instead" if $VERBOSE
  terminate
end

#concat(str) ⇒ Object Also known as: <<



48
49
50
51
# File 'lib/rubysl/strscan/strscan.rb', line 48

def concat(str)
  @string << StringValue(str)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/rubysl/strscan/strscan.rb', line 54

def empty?
  warn "StringScanner#empty? is obsolete; use #eos? instead?" if $VERBOSE
  eos?
end

#eos?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rubysl/strscan/strscan.rb', line 59

def eos?
  @pos >= @string.bytesize
end

#exist?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubysl/strscan/strscan.rb', line 63

def exist?(pattern)
  _scan pattern, false, false, false
end

#get_byteObject



67
68
69
# File 'lib/rubysl/strscan/strscan.rb', line 67

def get_byte
  scan(/./mn)
end

#getbyteObject



71
72
73
74
# File 'lib/rubysl/strscan/strscan.rb', line 71

def getbyte
  warn "StringScanner#getbyte is obsolete; use #get_byte instead" if $VERBOSE
  get_byte
end

#getchObject



76
77
78
# File 'lib/rubysl/strscan/strscan.rb', line 76

def getch
  scan(/./m)
end

#inspectObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rubysl/strscan/strscan.rb', line 92

def inspect
  if defined? @string
    if eos?
      str = "#<StringScanner fin>"
    else
      if string.bytesize - pos > 5
        rest = "#{string[pos..pos+4]}..."
      else
        rest = string[pos..string.bytesize]
      end

      if pos > 0
        if pos > 5
          prev = "...#{string[pos-5...pos]}"
        else
          prev = string[0...pos]
        end

        str = "#<StringScanner #{pos}/#{string.bytesize} #{prev.inspect} @ #{rest.inspect}>"
      else
        str = "#<StringScanner #{pos}/#{string.bytesize} @ #{rest.inspect}>"
      end
    end

    str.taint if @string.tainted?
    return str
  else
    "#<StringScanner (uninitialized)>"
  end
end

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/rubysl/strscan/strscan.rb', line 123

def match?(pattern)
  _scan pattern, false, false, true
end

#matchedObject



127
128
129
# File 'lib/rubysl/strscan/strscan.rb', line 127

def matched
  @match.to_s if @match
end

#matched?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/rubysl/strscan/strscan.rb', line 131

def matched?
  !!@match
end

#matched_sizeObject



135
136
137
# File 'lib/rubysl/strscan/strscan.rb', line 135

def matched_size
  @match.to_s.bytesize if @match
end

#matchedsizeObject



139
140
141
142
# File 'lib/rubysl/strscan/strscan.rb', line 139

def matchedsize
  warn "StringScanner#matchedsize is obsolete; use #matched_size instead" if $VERBOSE
  matched_size
end

#peek(len) ⇒ Object

Raises:

  • (ArgumentError)


238
239
240
241
242
243
244
245
246
247
# File 'lib/rubysl/strscan/strscan.rb', line 238

def peek(len)
  raise ArgumentError if len < 0
  return "" if len.zero?

  begin
    return @string[pos, len]
  rescue TypeError
    raise RangeError, "offset outside of possible range"
  end
end

#peep(len) ⇒ Object



249
250
251
252
# File 'lib/rubysl/strscan/strscan.rb', line 249

def peep(len)
  warn "StringScanner#peep is obsolete; use #peek instead" if $VERBOSE
  peek len
end

#post_matchObject



144
145
146
# File 'lib/rubysl/strscan/strscan.rb', line 144

def post_match
  @match.post_match if @match
end

#pre_matchObject



148
149
150
# File 'lib/rubysl/strscan/strscan.rb', line 148

def pre_match
  @string.byteslice(0, match.full.at(0)) if @match
end

#resetObject



158
159
160
161
# File 'lib/rubysl/strscan/strscan.rb', line 158

def reset
  reset_state
  self
end

#restObject



163
164
165
# File 'lib/rubysl/strscan/strscan.rb', line 163

def rest
  @string.byteslice(@pos, @string.bytesize - @pos)
end

#rest?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/rubysl/strscan/strscan.rb', line 167

def rest?
  return !eos?
end

#rest_sizeObject



171
172
173
# File 'lib/rubysl/strscan/strscan.rb', line 171

def rest_size
  @string.bytesize - @pos
end

#restsizeObject



175
176
177
178
# File 'lib/rubysl/strscan/strscan.rb', line 175

def restsize
  warn "StringScanner#restsize is obsolete; use #rest_size instead" if $VERBOSE
  rest_size
end

#scan(pattern) ⇒ Object



180
181
182
# File 'lib/rubysl/strscan/strscan.rb', line 180

def scan(pattern)
  _scan pattern, true, true, true
end

#scan_full(pattern, advance_pos, getstr) ⇒ Object



188
189
190
# File 'lib/rubysl/strscan/strscan.rb', line 188

def scan_full(pattern, advance_pos, getstr)
  _scan pattern, advance_pos, getstr, true
end

#scan_until(pattern) ⇒ Object



184
185
186
# File 'lib/rubysl/strscan/strscan.rb', line 184

def scan_until(pattern)
  _scan pattern, true, true, false
end

#search_full(pattern, advance_pos, getstr) ⇒ Object



192
193
194
# File 'lib/rubysl/strscan/strscan.rb', line 192

def search_full(pattern, advance_pos, getstr)
  _scan pattern, advance_pos, getstr, false
end

#skip(pattern) ⇒ Object



200
201
202
# File 'lib/rubysl/strscan/strscan.rb', line 200

def skip(pattern)
  _scan pattern, true, false, true
end

#skip_until(pattern) ⇒ Object



204
205
206
# File 'lib/rubysl/strscan/strscan.rb', line 204

def skip_until(pattern)
  _scan pattern, true, false, false
end

#stringObject



208
209
210
# File 'lib/rubysl/strscan/strscan.rb', line 208

def string
  @original
end

#string=(string) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rubysl/strscan/strscan.rb', line 212

def string=(string)
  reset_state

  if string.instance_of? String
    @original = string
    @string = string
  else
    @original = StringValue(string)
    @string = String.new @original
  end
end

#terminateObject



224
225
226
227
228
# File 'lib/rubysl/strscan/strscan.rb', line 224

def terminate
  @match = nil
  @pos = string.bytesize
  self
end

#unscanObject

Raises:



230
231
232
233
234
235
236
# File 'lib/rubysl/strscan/strscan.rb', line 230

def unscan
  raise ScanError if @match.nil?
  @pos = @prev_pos
  @prev_pos = nil
  @match = nil
  self
end