@@ -127,28 +127,30 @@ module IO::ByteFormat
127
127
{% for mod in %w(LittleEndian BigEndian) % }
128
128
module {{mod.id}}
129
129
{% for type , i in %w(Int8 UInt8 Int16 UInt16 Int32 UInt32 Int64 UInt64) % }
130
+ {% bytesize = 2 ** (i / 2 ) % }
131
+
130
132
def self.encode (int : {{type .id}}, io : IO )
131
- buffer = pointerof (int).as(UInt8 [{{2 ** (i / 2 ) }}]* ).value
133
+ buffer = pointerof (int).as(UInt8 [{{bytesize }}]* ).value
132
134
buffer.reverse! unless SystemEndian == self
133
135
io.write(buffer.to_slice)
134
136
end
135
137
136
138
def self.encode (int : {{type .id}}, bytes : Bytes )
137
- buffer = pointerof (int).as(UInt8 [{{2 ** (i / 2 ) }}]* ).value
139
+ buffer = pointerof (int).as(UInt8 [{{bytesize }}]* ).value
138
140
buffer.reverse! unless SystemEndian == self
139
141
buffer.to_slice.copy_to(bytes)
140
142
end
141
143
142
144
def self.decode (type : {{type .id}}.class, io : IO )
143
- buffer = uninitialized UInt8 [{{2 ** (i / 2 ) }}]
145
+ buffer = uninitialized UInt8 [{{bytesize }}]
144
146
io.read_fully(buffer.to_slice)
145
147
buffer.reverse! unless SystemEndian == self
146
148
buffer.to_unsafe.as(Pointer ({{type .id}})).value
147
149
end
148
150
149
151
def self.decode (type : {{type .id}}.class, bytes : Bytes )
150
- buffer = uninitialized UInt8 [{{2 ** (i / 2 ) }}]
151
- bytes.copy_to(buffer.to_slice)
152
+ buffer = uninitialized UInt8 [{{bytesize }}]
153
+ bytes.to_slice[ 0 , {{bytesize}}]. copy_to(buffer.to_slice)
152
154
buffer.reverse! unless SystemEndian == self
153
155
buffer.to_unsafe.as(Pointer ({{type .id}})).value
154
156
end
0 commit comments