Class: MicroJSON::Decoder::StringCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/moon/packages/micro_json/micro_json.rb

Overview

Utility class for tracking String index position and retrieving values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ StringCursor

Returns a new instance of StringCursor.

Parameters:


168
169
170
171
# File 'lib/moon/packages/micro_json/micro_json.rb', line 168

def initialize(str)
  @index = 0
  @str = str
end

Instance Attribute Details

#indexInteger

Returns:


165
166
167
# File 'lib/moon/packages/micro_json/micro_json.rb', line 165

def index
  @index
end

#strObject (readonly)

Returns the value of attribute str


161
162
163
# File 'lib/moon/packages/micro_json/micro_json.rb', line 161

def str
  @str
end

Instance Method Details

#charString

Return the current character

Returns:


183
184
185
# File 'lib/moon/packages/micro_json/micro_json.rb', line 183

def char
  @str[@index]
end

#eos?Boolean

Have we reached the end of the String?

Returns:


176
177
178
# File 'lib/moon/packages/micro_json/micro_json.rb', line 176

def eos?
  @index >= @str.length
end

#nextString

Increments the index and returns the character at that point

Returns:

Raises:

  • (RangeError)

207
208
209
210
211
# File 'lib/moon/packages/micro_json/micro_json.rb', line 207

def next
  raise RangeError, 'stepping outside string\'s range' if @index >= @str.length
  @index += 1
  char
end

#prevString

Decrements the index and returns the character at that point

Returns:

Raises:

  • (RangeError)

198
199
200
201
202
# File 'lib/moon/packages/micro_json/micro_json.rb', line 198

def prev
  raise RangeError, 'stepping outside string\'s range' if @index <= 0
  @index -= 1
  char
end

#string(length) ⇒ String

Return a string from the current position

Parameters:

Returns:


191
192
193
# File 'lib/moon/packages/micro_json/micro_json.rb', line 191

def string(length)
  @str[@index, length]
end