Class: Tx::Index

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Util
Includes:
Enumerable, Util
Defined in:
lib/tx.rb

Overview

Wrapper of UnsafeIndex. Boundary checking of pos/len and some methods are added.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

add_encoding, bytesize, def_wrapper_methods, default_encoding, to_binary

Constructor Details

#initialize(arg, encoding = nil) ⇒ Index

Returns a new instance of Index.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tx.rb', line 84

def initialize(arg, encoding = nil)
  if arg.is_a?(UnsafeIndex)
    @unsafe = arg
  else
    @unsafe = UnsafeIndex.new()
    if !@unsafe.open(arg)
      raise(IOError, "failed to open #{arg}")
    end
  end
  @encoding = encoding || default_encoding()
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



96
97
98
# File 'lib/tx.rb', line 96

def encoding
  @encoding
end

Instance Method Details

#each(&block) ⇒ Object



112
113
114
# File 'lib/tx.rb', line 112

def each(&block)
  to_a().each(&block)
end

#gsub(str, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tx.rb', line 131

def gsub(str, &block)
  bstr = to_binary(str)
  result = add_encoding("")
  prev_pos = 0
  scan(str) do |match, pos|
    result << add_encoding(bstr[prev_pos...pos])
    result << yield(match, pos)
    prev_pos = pos + bytesize(match)
  end
  result << add_encoding(bstr[prev_pos..-1])
  return result
end

#inspectObject



104
105
106
# File 'lib/tx.rb', line 104

def inspect()
  return "\#<%p:0x%x>" % [self.class, self.object_id]
end

#scan(str, &block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tx.rb', line 116

def scan(str, &block)
  bstr = to_binary(str)
  result = []
  pos = 0
  while pos < bytesize(str)
    plen = longest_prefix(str, pos)
    if plen >= 0
      args = [add_encoding(bstr[pos, plen]), pos]
      block ? yield(*args) : result.push(args)
    end
    pos += plen > 0 ? plen : 1
  end
  return block ? str : result
end

#to_aObject



108
109
110
# File 'lib/tx.rb', line 108

def to_a()
  return search_expansions("")
end