Class: TTFunk::EncodedString Private
- Inherits:
-
Object
- Object
- TTFunk::EncodedString
- 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
-
#<<(obj) ⇒ self
private
Append to string.
-
#align!(width = 4) ⇒ self
private
Append padding to align string to the specified word width.
-
#bytes ⇒ Array<Integer>
private
Raw bytes.
-
#concat(*objs) ⇒ self
private
Append multiple objects.
-
#initialize {|| ... } ⇒ EncodedString
constructor
private
A new instance of EncodedString.
-
#length ⇒ Integer
private
Length of this string.
-
#placeholders ⇒ Hash{Symbol => Plaholder}
private
Plaholders.
-
#resolve_placeholder(name, value) ⇒ void
private
Resolve placeholder.
-
#string ⇒ String
private
Raw string.
-
#unresolved_string ⇒ String
private
Unresolved raw string.
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.
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.
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.
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 |
#bytes ⇒ Array<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.
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.
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 |
#length ⇒ 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.
Length of this string.
77 78 79 |
# File 'lib/ttfunk/encoded_string.rb', line 77 def length io.length end |
#placeholders ⇒ Hash{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
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.
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 |
#string ⇒ String
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.
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_string ⇒ String
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.
107 108 109 |
# File 'lib/ttfunk/encoded_string.rb', line 107 def unresolved_string io.string end |