Skip to content

Commit

Permalink
de-duplicate bit_set endian check proc, type assert underlying type i…
Browse files Browse the repository at this point in the history
…s integer
  • Loading branch information
jakubtomsu committed Oct 12, 2024
1 parent 35df575 commit f734c80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
32 changes: 15 additions & 17 deletions core/encoding/json/marshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -520,25 +520,8 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
}

case runtime.Type_Info_Bit_Set:
is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
if ti == nil {
return false
}
t := runtime.type_info_base(ti)
#partial switch info in t.variant {
case runtime.Type_Info_Integer:
switch info.endianness {
case .Platform: return false
case .Little: return ODIN_ENDIAN != .Little
case .Big: return ODIN_ENDIAN != .Big
}
}
return false
}

bit_data: u128
bit_size := u128(8*ti.size)

do_byte_swap := is_bit_set_different_endian_to_platform(info.underlying)

switch bit_size {
Expand Down Expand Up @@ -757,4 +740,19 @@ cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
}

return u
}

@(private)
is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
if ti == nil {
return false
}
t := runtime.type_info_base(ti)
info := t.variant.(runtime.Type_Info_Integer)
switch info.endianness {
case .Platform: return false
case .Little: return ODIN_ENDIAN != .Little
case .Big: return ODIN_ENDIAN != .Big
case: return false
}
}
16 changes: 0 additions & 16 deletions core/encoding/json/unmarshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,6 @@ assign_int :: proc(val: any, i: $T) -> bool {
case:
ti := type_info_of(v.id)
if bit_set_info, ok := ti.variant.(runtime.Type_Info_Bit_Set); ok {
is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
if ti == nil {
return false
}
t := runtime.type_info_base(ti)
#partial switch info in t.variant {
case runtime.Type_Info_Integer:
switch info.endianness {
case .Platform: return false
case .Little: return ODIN_ENDIAN != .Little
case .Big: return ODIN_ENDIAN != .Big
}
}
return false
}

do_byte_swap := is_bit_set_different_endian_to_platform(bit_set_info.underlying)
switch ti.size * 8 {
case 0: // no-op.
Expand Down

0 comments on commit f734c80

Please sign in to comment.