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)


24
25
26
# File 'lib/ttfunk/encoded_string.rb', line 24

def initialize
  yield(self) if block_given?
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)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ttfunk/encoded_string.rb', line 32

def <<(obj)
  case obj
  when String
    io << obj
  when Placeholder
    add_placeholder(obj)
    io << ("\0" * obj.length)
  when self.class
    # adjust placeholders to be relative to the entire encoded string
    obj.placeholders.each_pair do |_, placeholder|
      add_placeholder(placeholder.dup, placeholder.position + io.length)
    end

    io << obj.unresolved_string
  end

  self
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)


66
67
68
69
70
71
72
# File 'lib/ttfunk/encoded_string.rb', line 66

def align!(width = 4)
  if (length % width).positive?
    self << ("\0" * (width - (length % width)))
  end

  self
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:



100
101
102
# File 'lib/ttfunk/encoded_string.rb', line 100

def bytes
  string.bytes
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)


55
56
57
58
59
60
# File 'lib/ttfunk/encoded_string.rb', line 55

def concat(*objs)
  objs.each do |obj|
    self << obj
  end
  self
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)


77
78
79
# File 'lib/ttfunk/encoded_string.rb', line 77

def length
  io.length
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})


131
132
133
# File 'lib/ttfunk/encoded_string.rb', line 131

def placeholders
  @placeholders ||= {}
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)


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ttfunk/encoded_string.rb', line 116

def resolve_placeholder(name, value)
  last_pos = io.pos

  if (placeholder = placeholders[name])
    io.seek(placeholder.position)
    io.write(value[0..placeholder.length])
    placeholders.delete(name)
  end
ensure
  io.seek(last_pos)
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:



86
87
88
89
90
91
92
93
# File 'lib/ttfunk/encoded_string.rb', line 86

def string
  unless placeholders.empty?
    raise UnresolvedPlaceholderError,
      "string contains #{placeholders.size} unresolved placeholder(s)"
  end

  io.string
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)


107
108
109
# File 'lib/ttfunk/encoded_string.rb', line 107

def unresolved_string
  io.string
end