Skip to content

Commit d8f1a31

Browse files
committed
Fix formatting issues. Add tests
1 parent c0ee310 commit d8f1a31

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

lib/binary_parser.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export class Parser {
286286
alias?: string;
287287
useContextVariables = false;
288288

289-
constructor() { }
289+
constructor() {}
290290

291291
static start() {
292292
return new Parser();
@@ -1046,13 +1046,7 @@ export class Parser {
10461046
if (this.next.options && this.next.options.type instanceof Parser) {
10471047
// Something in the nest
10481048
if (this.next.options.type.next) {
1049-
if (this.next.options.type.next.type === "bit") {
1050-
// Next is a bit field
1051-
return false;
1052-
} else {
1053-
// Next is something elses
1054-
return true;
1055-
}
1049+
return this.next.options.type.next.type !== "bit";
10561050
}
10571051
return false;
10581052
} else {
@@ -1077,10 +1071,7 @@ export class Parser {
10771071
parser.varName = ctx.generateVariable(parser.varName);
10781072
ctx.bitFields.push(parser);
10791073

1080-
if (
1081-
!this.next ||
1082-
this.nextNotBit()
1083-
) {
1074+
if (!this.next || this.nextNotBit()) {
10841075
const val = ctx.generateTmpVariable();
10851076

10861077
ctx.pushCode(`var ${val} = 0;`);
@@ -1127,7 +1118,8 @@ export class Parser {
11271118
if (rem) {
11281119
const mask = -1 >>> (32 - rem);
11291120
ctx.pushCode(
1130-
`${parser.varName} = (${val} & 0x${mask.toString(16)}) << ${length - rem
1121+
`${parser.varName} = (${val} & 0x${mask.toString(16)}) << ${
1122+
length - rem
11311123
};`
11321124
);
11331125
length -= rem;
@@ -1139,7 +1131,8 @@ export class Parser {
11391131
const mask = -1 >>> (32 - length);
11401132

11411133
ctx.pushCode(
1142-
`${parser.varName} ${length < (parser.options.length as number) ? "|=" : "="
1134+
`${parser.varName} ${
1135+
length < (parser.options.length as number) ? "|=" : "="
11431136
} ${val} >> ${offset} & 0x${mask.toString(16)};`
11441137
);
11451138

test/composite_parser.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,28 @@ function compositeParserTests(
983983
});
984984
});
985985

986+
it("standalone bit fields should work", () => {
987+
const parser = Parser.start().bit6("one").bit8("two");
988+
const buffer = factory([0xa8, 0x78]);
989+
const result = parser.parse(buffer);
990+
deepStrictEqual(result.one, 0xa8 >> 2);
991+
deepStrictEqual(result.two, 0x78 >> 2);
992+
});
993+
994+
it("bit to nested bit should work", () => {
995+
const parser = Parser.start()
996+
.bit6("one")
997+
.nest("nested", {
998+
type: new Parser().bit8("two").uint8("three"),
999+
});
1000+
const buffer = factory([0xa8, 0x78, 0x45]);
1001+
const result = parser.parse(buffer);
1002+
deepStrictEqual(result.one, 0xa8 >> 2);
1003+
deepStrictEqual(result.nested.two, 0x78 >> 2);
1004+
// switching to uint8 should start at next byte (skipping two bits here)
1005+
deepStrictEqual(result.nested.three, 0x45);
1006+
});
1007+
9861008
it("bit before nest should work", () => {
9871009
const parser = Parser.start()
9881010
.useContextVars()
@@ -1010,7 +1032,6 @@ function compositeParserTests(
10101032
},
10111033
});
10121034
});
1013-
10141035
});
10151036

10161037
describe("Constructors", () => {
@@ -1073,7 +1094,7 @@ function compositeParserTests(
10731094
});
10741095
});
10751096

1076-
it("should pass variable context to child parser", () => { });
1097+
it("should pass variable context to child parser", () => {});
10771098
const parser = Parser.start()
10781099
.uint16be("len")
10791100
.pointer("child", {

0 commit comments

Comments
 (0)