Skip to content

Commit 4c162bd

Browse files
committed
Extract Alignable
1 parent 632843c commit 4c162bd

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

lib/flatbuffers/alignable.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2026 Sutou Kouhei <kou@clear-code.com>
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require_relative "append_as_bytes"
16+
17+
module FlatBuffers
18+
module Alignable
19+
using AppendAsBytes if const_defined?(:AppendAsBytes)
20+
21+
LARGEST_PADDING = "\x00" * 7
22+
23+
private
24+
def compute_padding_size(size, alignment_byte)
25+
size % alignment_byte
26+
end
27+
28+
def pad!(data, padding_size)
29+
return if padding_size.zero?
30+
data.append_as_bytes(LARGEST_PADDING[0, padding_size])
31+
end
32+
33+
def align!(data, alignment_byte)
34+
pad!(data, compute_padding_size(data.bytesize, alignment_byte))
35+
end
36+
end
37+
end

lib/flatbuffers/serializer.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
require_relative "alignable"
1516
require_relative "append_as_bytes"
1617

1718
module FlatBuffers
1819
using AppendAsBytes if const_defined?(:AppendAsBytes)
1920

2021
class Serializer
21-
module Alignable
22-
private
23-
def align32!(data)
24-
padding_size = data.bytesize % 4 # IO::Buffer.size_of(:s32)
25-
return if padding_size.zero?
26-
data.append_as_bytes("\x00" * padding_size)
27-
end
28-
end
29-
3022
module Packable
3123
private
3224
def pack_value(base_type, value)
@@ -60,7 +52,6 @@ def pack_value(base_type, value)
6052
packed_value = [value.bytesize].pack("L<")
6153
packed_value.append_as_bytes(value)
6254
packed_value.append_as_bytes("\x00")
63-
align32!(packed_value)
6455
packed_value
6556
when String
6657
klass = Object.const_get(base_type)
@@ -89,7 +80,7 @@ def add_field(field, value)
8980
packed_value = pack_value(field.base_type, value)
9081
@buffer.append_as_bytes(packed_value)
9182
unless field.padding.zero?
92-
@buffer.append_as_bytes("\x00" * field.padding)
83+
pad!(@buffer, field.padding)
9384
end
9485
end
9586

@@ -243,7 +234,7 @@ def add_field(field, value)
243234
end
244235

245236
def finish
246-
align32!(@field_values)
237+
align!(@field_values, View::OFFSET_SIZE)
247238
table_size = View::Table.compute_size(@field_values.bytesize)
248239

249240
field_offset_base =

0 commit comments

Comments
 (0)