Skip to content

Commit 71eeed9

Browse files
authored
patterns/protobuf: Remove global variables
In issue #346 it is noted that the format functions return the same value repeatidly and erroneously. This is due to the use of global variables which result on only their last value being used in format functions due to their delayed evaluation. Fixed by using local variables instead. Also remove tabs from the file and an unused tags variable.
1 parent e779b88 commit 71eeed9

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

patterns/protobuf.hexpat

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,70 @@ import std.mem;
88
import type.leb128;
99

1010
struct ZigZag32 {
11-
u32 value;
11+
u32 value;
1212
} [[sealed, format("format_zigzag32")]];
1313

1414
fn format_zigzag32(ZigZag32 zigzag) {
15-
return s32((s32(zigzag.value) << 1) ^ (s32(zigzag.value) >> 31));
15+
return s32((s32(zigzag.value) << 1) ^ (s32(zigzag.value) >> 31));
1616
};
1717

1818
struct ZigZag64 {
19-
u64 value;
19+
u64 value;
2020
} [[sealed, format("format_zigzag64")]];
2121

2222
fn format_zigzag64(ZigZag64 zigzag) {
23-
return s64((s64(zigzag.value) << 1) ^ (s64(zigzag.value) >> 63));
23+
return s64((s64(zigzag.value) << 1) ^ (s64(zigzag.value) >> 63));
2424
};
2525

2626
enum WireType : u8 {
27-
Varint = 0,
28-
_64Bit = 1,
29-
LengthDelimited = 2,
30-
StartGroup = 3,
31-
EndGroup = 4,
32-
_32Bit = 5
27+
Varint = 0,
28+
_64Bit = 1,
29+
LengthDelimited = 2,
30+
StartGroup = 3,
31+
EndGroup = 4,
32+
_32Bit = 5
3333
};
3434

35-
WireType wire_type;
36-
u32 tag;
37-
u32 field_number;
3835

3936
struct Key {
4037
type::uLEB128 keyDec;
41-
field_number = u32(keyDec) >> 3;
42-
wire_type = u32(keyDec) & 7;
38+
u32 field_number = u32(keyDec) >> 3;
39+
WireType wire_type = u32(keyDec) & 7;
4340
}[[sealed, format("format_key")]];
4441

4542
fn format_key(Key keyObject) {
46-
return std::format("{} with field number {}", wire_type, field_number);
43+
return std::format("{} with field number {}", keyObject.wire_type, keyObject.field_number);
4744
};
4845

4946
union _64Bit {
50-
u64 fixed64;
51-
ZigZag64 sfixed64;
52-
double dbl;
47+
u64 fixed64;
48+
ZigZag64 sfixed64;
49+
double dbl;
5350
};
5451

5552
union _32Bit {
56-
u32 fixed32;
57-
ZigZag32 sfixed32;
58-
float flt;
53+
u32 fixed32;
54+
ZigZag32 sfixed32;
55+
float flt;
5956
};
6057

6158
struct LengthDelimited {
62-
type::uLEB128 length;
63-
char data[length];
59+
type::uLEB128 length;
60+
char data[length];
6461
};
6562

6663

6764
struct Entry {
6865
Key key;
6966

70-
if (wire_type == WireType::Varint)
71-
type::uLEB128 value;
72-
else if (wire_type == WireType::_64Bit)
73-
_64Bit value;
74-
else if (wire_type == WireType::LengthDelimited)
75-
LengthDelimited value;
76-
else if (wire_type == WireType::_32Bit)
77-
_32Bit value;
67+
if (key.wire_type == WireType::Varint)
68+
type::uLEB128 value;
69+
else if (key.wire_type == WireType::_64Bit)
70+
_64Bit value;
71+
else if (key.wire_type == WireType::LengthDelimited)
72+
LengthDelimited value;
73+
else if (key.wire_type == WireType::_32Bit)
74+
_32Bit value;
7875
};
7976

8077
Entry entries[while(!std::mem::eof())] @ 0x00;

0 commit comments

Comments
 (0)