Class: Owasp::Esapi::Codec::PushableString

Inherits:
Object
  • Object
show all
Defined in:
lib/codec/pushable_string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ PushableString

Setup a pushable string stream will setup UTF_8 encoding on the input


13
14
15
16
17
18
19
# File 'lib/codec/pushable_string.rb', line 13

def initialize(string)
  @input = string.force_encoding(Encoding::UTF_8)
  @index = 0
  @mark = 0
  @temp = nil
  @push = nil
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index


8
9
10
# File 'lib/codec/pushable_string.rb', line 8

def index
  @index
end

Instance Method Details

#hex?(c) ⇒ Boolean

Check if a given character is a hexadecimal character

Returns:

  • (Boolean)

88
89
90
91
# File 'lib/codec/pushable_string.rb', line 88

def hex?(c)
  return false if c.nil?
  c =~ /[a-fA-F0-9]/
end

#markObject

Mark the stream for rewind


82
83
84
85
# File 'lib/codec/pushable_string.rb', line 82

def mark
  @temp = @push
  @mark = @index
end

#nextObject

Get the next token off of the stream


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/codec/pushable_string.rb', line 22

def next
  unless @push.nil?
    t = @push
    @push = nil
    return t
  end
  return nil if @input.nil?
  return nil if @input.size == 0
  return nil if @index >= @input.size

  t = @input[@index]
  @index += 1
  t
end

#next?Boolean

Check to see if we have another token on the stream

Returns:

  • (Boolean)

54
55
56
# File 'lib/codec/pushable_string.rb', line 54

def next?
  !@push.nil? ? true : @input.nil? ? false : @input.empty? ? false : @index >= @input.length ? false : true
end

#next_hexObject

fetch the next hex token in the string or nil


38
39
40
41
42
43
# File 'lib/codec/pushable_string.rb', line 38

def next_hex
  c = self.next
  return nil if c.nil?
  return c if hex?(c)
  nil
end

#next_octalObject

Fetch the next octal token int eh string or nil


46
47
48
49
50
51
# File 'lib/codec/pushable_string.rb', line 46

def next_octal
  c = self.next
  return nil if c.nil?
  return c if octal?(c)
  nil
end

#octal?(c) ⇒ Boolean

Check if a given character is an octal character

Returns:

  • (Boolean)

94
95
96
97
# File 'lib/codec/pushable_string.rb', line 94

def octal?(c)
  return false if c.nil?
  c =~ /[0-7]/
end

#peekObject

Peek into the stream and fetch teh next character without moving the index


73
74
75
76
77
78
79
# File 'lib/codec/pushable_string.rb', line 73

def peek
  return @push if !@push.nil?
  return nil if @input.nil?
  return nil if @input.empty?
  return nil if @index >= @input.size
  @input[@index]
end

#peek?(c) ⇒ Boolean

Peek into teh stream and see if the next character is the one in question

Returns:

  • (Boolean)

64
65
66
67
68
69
70
# File 'lib/codec/pushable_string.rb', line 64

def peek?(c)
  return true if !@push.nil? and @push == c
  return false if @input.empty?
  return false if @input.nil?
  return false if @index >= @input.size
  @input[@index] == c
end

#push(c) ⇒ Object

Push a character back onto the string, this is a unread operation


59
60
61
# File 'lib/codec/pushable_string.rb', line 59

def push(c)
  @push = c
end

#remainderObject

Fetch the rest of the string from the current index


106
107
108
109
110
# File 'lib/codec/pushable_string.rb', line 106

def remainder
  t = @input.slice(@index,@input.size-@index)
  return @push + t unless @push.nil?
  t
end

#resetObject

Reset the index back to the mark


100
101
102
103
# File 'lib/codec/pushable_string.rb', line 100

def reset
  @push = @temp
  @index = @mark
end