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.
Source Code
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.
Source Code
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.
Source Code
66 | def align!(width = 4) |
67 | if (length % width).positive? |
68 | self << ("\0" * (width - (length % width))) |
69 | end
|
70 | |
71 | self
|
72 | 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.
Source Code
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.
Source Code
55 | def concat(*objs) |
56 | objs.each do |obj| |
57 | self << obj |
58 | end
|
59 | self
|
60 | 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.
Source Code
77 | def length |
78 | io.length |
79 | 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
Source Code
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.
Source Code
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
|
#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.
Source Code
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_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.
Source Code
107 | def unresolved_string |
108 | io.string |
109 | end
|