Skip to content

Commit c07783d

Browse files
author
yang.yu
committed
feat: add TerminatedType
1 parent 21f91eb commit c07783d

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

kaitai/sizeof.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ import (
55
"reflect"
66
)
77

8+
type Size interface {
9+
Size() (uint64, error)
10+
}
11+
812
func SizeOf(msg interface{}) (uint64, error) {
913
v := reflect.ValueOf(msg)
1014
return sizeOf(v)
1115
}
1216

1317
func sizeOf(fieldValue reflect.Value) (uint64, error) {
18+
// interface
19+
marshaler, ok := fieldValue.Interface().(Size)
20+
if ok && marshaler != nil {
21+
return marshaler.Size()
22+
}
23+
1424
switch fieldValue.Kind() {
1525
case reflect.Uint8, reflect.Int8, reflect.Bool:
1626
return 1, nil

kaitai/sizeof_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import (
44
"testing"
55
)
66

7+
type a struct {
8+
}
9+
10+
func (a *a) Size() (uint64, error) {
11+
return 4, nil
12+
}
13+
714
func TestSizeOf(t *testing.T) {
815
type args struct {
916
*Stream
@@ -23,9 +30,12 @@ func TestSizeOf(t *testing.T) {
2330
Slice []string
2431
Array [4]byte
2532
Args *args
33+
Sizer *a
34+
}
35+
result := args{
36+
Sizer: &a{},
2637
}
27-
result := args{}
28-
var initLength uint64 = 55
38+
var initLength uint64 = 59
2939

3040
l, err := SizeOf(result)
3141
if err != nil {

kaitai/util.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,116 @@ func (fw *FakeWriter) Write([]byte) (n int, err error) {
136136
func NewFakeWriter(reader io.ReadSeeker) io.ReadWriteSeeker {
137137
return &FakeWriter{reader}
138138
}
139+
140+
type BytesTerminatedType struct {
141+
Data []byte
142+
terminator byte
143+
include bool
144+
consume bool
145+
eosError bool
146+
}
147+
148+
func (b *BytesTerminatedType) Read(in []byte) error {
149+
b.Data = BytesTerminate(in, b.terminator, b.include)
150+
return nil
151+
}
152+
153+
func (b BytesTerminatedType) Write() ([]byte, error) {
154+
if b.include {
155+
return b.Data, nil
156+
}
157+
return append(b.Data, b.terminator), nil
158+
}
159+
160+
func (b BytesTerminatedType) Size() (uint64, error) {
161+
result := uint64(len(b.Data))
162+
if b.include {
163+
return result, nil
164+
}
165+
return result + 1, nil
166+
}
167+
168+
func (b BytesTerminatedType) Bytes() []byte {
169+
return b.Data
170+
}
171+
172+
type String struct {
173+
Data string
174+
encoding encoding.Encoding
175+
}
176+
177+
func (s *String) Read(in []byte) error {
178+
data, err := BytesToStr(in, s.encoding.NewDecoder())
179+
if err != nil {
180+
return err
181+
}
182+
s.Data = data
183+
return nil
184+
}
185+
186+
func (s String) Write() ([]byte, error) {
187+
data, err := StrToBytes(s.Data, s.encoding.NewEncoder())
188+
if err != nil {
189+
return nil, err
190+
}
191+
return data, nil
192+
}
193+
194+
func (s String) Size() (uint64, error) {
195+
d, err := s.Write()
196+
if err != nil {
197+
return 0, err
198+
}
199+
return uint64(len(d)), nil
200+
}
201+
202+
func (s String) String() string {
203+
return s.Data
204+
}
205+
206+
type StringTerminatedType struct {
207+
Data string
208+
encoding encoding.Encoding
209+
terminator byte
210+
include bool
211+
consume bool
212+
eosError bool
213+
}
214+
215+
func (s *StringTerminatedType) Read(in []byte) error {
216+
var err error
217+
data := BytesTerminate(in, s.terminator, s.include)
218+
str, err := BytesToStr(data, s.encoding.NewDecoder())
219+
if err != nil {
220+
return err
221+
}
222+
s.Data = str
223+
return nil
224+
}
225+
226+
func (s StringTerminatedType) Write() ([]byte, error) {
227+
data, err := StrToBytes(s.Data, s.encoding.NewEncoder())
228+
if err != nil {
229+
return nil, err
230+
}
231+
if s.include {
232+
return data, nil
233+
}
234+
return append(data, s.terminator), nil
235+
}
236+
237+
func (s StringTerminatedType) Size() (uint64, error) {
238+
d, err := s.Write()
239+
if err != nil {
240+
return 0, err
241+
}
242+
result := uint64(len(d))
243+
if s.include {
244+
return result, nil
245+
}
246+
return result + 1, nil
247+
}
248+
249+
func (s StringTerminatedType) String() string {
250+
return s.Data
251+
}

0 commit comments

Comments
 (0)