Class: StringPtr

Inherits:
Object
  • Object
show all
Defined in:
lib/unmangler/string_ptr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s = nil, pos = 0) ⇒ StringPtr

Returns a new instance of StringPtr.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/unmangler/string_ptr.rb', line 6

def initialize s=nil, pos=0
  case s
  when String
    @string = s
    @pos = pos
  when StringPtr
    @string = s.string
    @pos = s.pos + pos
  when NilClass
    @string = nil
    @pos = pos
  else
    raise "invalid s: #{s.inspect}"
  end
end

Instance Attribute Details

#posObject

Returns the value of attribute pos.



4
5
6
# File 'lib/unmangler/string_ptr.rb', line 4

def pos
  @pos
end

#stringObject

Returns the value of attribute string.



4
5
6
# File 'lib/unmangler/string_ptr.rb', line 4

def string
  @string
end

Instance Method Details

#+(n) ⇒ Object



103
104
105
# File 'lib/unmangler/string_ptr.rb', line 103

def + n
  self.class.new @string, @pos+n
end

#-(x) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/unmangler/string_ptr.rb', line 107

def - x
  case x
  when Numeric
    # shift pointer
    self.class.new @string, @pos-x
  when StringPtr
    # return diff btw 2 ptrs
    raise "subtracting different pointers" if self.string != x.string
    @pos - x.pos
  end
end

#<<(s) ⇒ Object



58
59
60
61
# File 'lib/unmangler/string_ptr.rb', line 58

def << s
  @string[@pos, s.size] = s
  @pos += s.size
end

#=~(re) ⇒ Object



71
72
73
# File 'lib/unmangler/string_ptr.rb', line 71

def =~ re
  @string[@pos..-1] =~ re
end

#[](pos, len = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/unmangler/string_ptr.rb', line 22

def [] pos, len=nil
  if t = @string[@pos..-1]
    if len
      t[pos, len]
    else
      t[pos]
    end
  else
    nil
  end
end

#[]=(pos, x, y = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/unmangler/string_ptr.rb', line 34

def []= pos, x, y=nil
  if y
    # s[10, 3] = 'abc'
    @string[@pos+pos, x] = y
  elsif pos.is_a?(Range)
    range = pos
    if range.end > 0
      # s[10..12] = 'abc'
      @string[Range.new(range.begin+@pos, range.end+@pos)] = x
    else
      if range.begin >= 0
        # s[10..-1] = 'abc'
        @string[Range.new(range.begin+@pos, range.end)] = x
      else
        # s[-3..-1] = 'abc'
        @string[range] = x
      end
    end
  else
    # s[10] = 'a'
    @string[@pos+pos] = x
  end
end

#dec!Object



76
# File 'lib/unmangler/string_ptr.rb', line 76

def dec!; @pos-=1; end

#get_inc!Object

get current character and then increment like in C:

c = *p++


81
82
83
84
85
# File 'lib/unmangler/string_ptr.rb', line 81

def get_inc!
  c = @string[@pos]
  @pos+=1
  c
end

#inc!Object



75
# File 'lib/unmangler/string_ptr.rb', line 75

def inc!; @pos+=1; end

#inc_get!Object

increment and then get current character like in C:

c = *++p


90
91
92
93
# File 'lib/unmangler/string_ptr.rb', line 90

def inc_get!
  @pos+=1
  @string[@pos]
end

#index(needle) ⇒ Object



63
64
65
# File 'lib/unmangler/string_ptr.rb', line 63

def index needle
  (@string[@pos..-1] || '').index(needle)
end

#inspectObject



119
120
121
# File 'lib/unmangler/string_ptr.rb', line 119

def inspect
  "#<StringPtr @pos=#{@pos.inspect}: #{@string[@pos..-1].inspect}>"
end

#strlenObject



67
68
69
# File 'lib/unmangler/string_ptr.rb', line 67

def strlen
  (@string[@pos..-1] || '').bytesize
end

#trim!Object



95
96
97
98
99
100
101
# File 'lib/unmangler/string_ptr.rb', line 95

def trim!
  return unless @string
  if idx = @string.index("\x00")
    @string[idx..-1] = ''
    @pos = @string.size if @pos > @string.size
  end
end