Class: TTFunk::EncodedString Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ttfunk/encoded_string.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encoded string takes care of placeholders in binary strings. Placeholders are used when bytes need to be placed in the stream before their value is known.

Instance Method Summary collapse

Constructor Details

#initialize {|| ... } ⇒ EncodedString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of EncodedString.

Yield Parameters:

  • (self)
Source Code
lib/ttfunk/encoded_string.rb, line 24
24
def initialize
25
  yield(self) if block_given?
26
end

Instance Method Details

#<<(obj) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Append to string.

Parameters:

Returns:

  • (self)
Source Code
lib/ttfunk/encoded_string.rb, line 32
32
def <<(obj)
33
  case obj
34
  when String
35
    io << obj
36
  when Placeholder
37
    add_placeholder(obj)
38
    io << ("\0" * obj.length)
39
  when self.class
40
    # adjust placeholders to be relative to the entire encoded string
41
    obj.placeholders.each_pair do |_, placeholder|
42
      add_placeholder(placeholder.dup, placeholder.position + io.length)
43
    end
44
45
    io << obj.unresolved_string
46
  end
47
48
  self
49
end

#align!(width = 4) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Append padding to align string to the specified word width.

Parameters:

  • width (Integer) (defaults to: 4)

Returns:

  • (self)
Source Code
lib/ttfunk/encoded_string.rb, line 66
66
def align!(width = 4)
67
  if (length % width).positive?
68
    self << ("\0" * (width - (length % width)))
69
  end
70
71
  self
72
end

#bytesArray<Integer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raw bytes.

Returns:

  • (Array<Integer>)

Raises:

Source Code
lib/ttfunk/encoded_string.rb, line 100
100
def bytes
101
  string.bytes
102
end

#concat(*objs) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Append multiple objects.

Parameters:

Returns:

  • (self)
Source Code
lib/ttfunk/encoded_string.rb, line 55
55
def concat(*objs)
56
  objs.each do |obj|
57
    self << obj
58
  end
59
  self
60
end

#lengthInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Length of this string.

Returns:

  • (Integer)
Source Code
lib/ttfunk/encoded_string.rb, line 77
77
def length
78
  io.length
79
end

#placeholdersHash{Symbol => Plaholder}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Plaholders

Returns:

  • (Hash{Symbol => Plaholder})
Source Code
lib/ttfunk/encoded_string.rb, line 131
131
def placeholders
132
  @placeholders ||= {}
133
end

#resolve_placeholder(name, value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Resolve placeholder.

Parameters:

  • name (Symbol)
  • value (String)
Source Code
lib/ttfunk/encoded_string.rb, line 116
116
def resolve_placeholder(name, value)
117
  last_pos = io.pos
118
119
  if (placeholder = placeholders[name])
120
    io.seek(placeholder.position)
121
    io.write(value[0..placeholder.length])
122
    placeholders.delete(name)
123
  end
124
ensure
125
  io.seek(last_pos)
126
end

#stringString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raw string.

Returns:

  • (String)

Raises:

Source Code
lib/ttfunk/encoded_string.rb, line 86
86
def string
87
  unless placeholders.empty?
88
    raise UnresolvedPlaceholderError,
89
      "string contains #{placeholders.size} unresolved placeholder(s)"
90
  end
91
92
  io.string
93
end

#unresolved_stringString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unresolved raw string.

Returns:

  • (String)
Source Code
lib/ttfunk/encoded_string.rb, line 107
107
def unresolved_string
108
  io.string
109
end