Module: Tx::Util

Included in:
Index, Index, Map, Map
Defined in:
lib/tx.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_wrapper_methods(*methods) ⇒ Object

Defines wrapper methods which perform boundary checking of pos and len.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tx.rb', line 12

def def_wrapper_methods(*methods)
  methods.each() do |name|
    define_method(name) do |*args|
      (str, pos, len, *opt) = args
      raise(ArgumentError, "argument pos is negative") if pos && pos < 0
      str_len = bytesize(str)
      pos ||= 0
      pos = str_len if pos > str_len
      len = str_len - pos if !len || len < 0 || len > str_len - pos
      add_encoding(@unsafe.__send__(name, str, pos, len, *opt))
    end
  end
end

Instance Method Details

#add_encoding(obj) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/tx.rb', line 32

def add_encoding(obj)
  case obj
    when Array
      obj.each(){ |e| add_encoding(e) }
    when String
      obj.force_encoding(@encoding)
  end
  return obj
end

#bytesize(str) ⇒ Object



46
47
48
# File 'lib/tx.rb', line 46

def bytesize(str)
  return str.bytesize
end

#default_encodingObject



28
29
30
# File 'lib/tx.rb', line 28

def default_encoding
  return Encoding.default_internal || Encoding::UTF_8
end

#to_binary(str) ⇒ Object



42
43
44
# File 'lib/tx.rb', line 42

def to_binary(str)
  return str.dup().force_encoding(Encoding::ASCII_8BIT)
end