Skip to content

Commit fa74c9b

Browse files
fix bad indent
1 parent 2097f98 commit fa74c9b

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

lib/binary_parser.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,12 +1062,14 @@ export class Parser {
10621062
parser.varName = ctx.generateVariable(parser.varName);
10631063
ctx.bitFields.push(parser);
10641064

1065-
let sum = 0;
10661065
if (
10671066
!this.next ||
10681067
(this.next && ['bit', 'nest'].indexOf(this.next.type) < 0)
10691068
) {
1070-
ctx.bitFields.forEach(parser => { sum += parser.options.length as number; });
1069+
let sum = 0;
1070+
ctx.bitFields.forEach(parser => {
1071+
sum += parser.options.length as number;
1072+
});
10711073

10721074
if (sum <= 8) {
10731075
sum = 8;
@@ -1082,35 +1084,36 @@ export class Parser {
10821084
'Currently, bit field sequences longer than 4-bytes is not supported.'
10831085
);
10841086
}
1085-
}
10861087

1087-
const tmpVal = ctx.generateTmpVariable();
1088-
ctx.pushCode(`var ${tmpVal} = 0;`);
1089-
let bitOffset = 0;
1090-
ctx.bitFields.forEach(parser => {
1091-
ctx.pushCode(
1092-
`${tmpVal} |= (${parser.varName} << ${
1093-
sum - (parser.options.length as number) - bitOffset});`
1094-
);
1095-
ctx.pushCode(`${tmpVal} = ${tmpVal} >>> 0;`);
1096-
bitOffset += parser.options.length as number;
1097-
});
1098-
if (sum == 8) {
1099-
ctx.pushCode(`smartBuffer.writeUInt8(${tmpVal});`);
1100-
} else if (sum == 16) {
1101-
ctx.pushCode(`smartBuffer.writeUInt16BE(${tmpVal});`);
1102-
} else if (sum == 24) {
1103-
const val1 = ctx.generateTmpVariable();
1104-
const val2 = ctx.generateTmpVariable();
1105-
ctx.pushCode(`var ${val1} = (${tmpVal} >>> 8);`);
1106-
ctx.pushCode(`var ${val2} = (${tmpVal} & 0x0ff);`);
1107-
ctx.pushCode(`smartBuffer.writeUInt16BE(${val1});`);
1108-
ctx.pushCode(`smartBuffer.writeUInt8(${val2});`);
1109-
} else if (sum == 32) {
1110-
ctx.pushCode(`smartBuffer.writeUInt32BE(${tmpVal});`);
1111-
}
1112-
1113-
ctx.bitFields = [];
1088+
1089+
const tmpVal = ctx.generateTmpVariable();
1090+
ctx.pushCode(`var ${tmpVal} = 0;`);
1091+
let bitOffset = 0;
1092+
ctx.bitFields.forEach(parser => {
1093+
ctx.pushCode(
1094+
`${tmpVal} |= (${parser.varName} << ${
1095+
sum - (parser.options.length as number) - bitOffset});`
1096+
);
1097+
ctx.pushCode(`${tmpVal} = ${tmpVal} >>> 0;`);
1098+
bitOffset += parser.options.length as number;
1099+
});
1100+
if (sum == 8) {
1101+
ctx.pushCode(`smartBuffer.writeUInt8(${tmpVal});`);
1102+
} else if (sum == 16) {
1103+
ctx.pushCode(`smartBuffer.writeUInt16BE(${tmpVal});`);
1104+
} else if (sum == 24) {
1105+
const val1 = ctx.generateTmpVariable();
1106+
const val2 = ctx.generateTmpVariable();
1107+
ctx.pushCode(`var ${val1} = (${tmpVal} >>> 8);`);
1108+
ctx.pushCode(`var ${val2} = (${tmpVal} & 0x0ff);`);
1109+
ctx.pushCode(`smartBuffer.writeUInt16BE(${val1});`);
1110+
ctx.pushCode(`smartBuffer.writeUInt8(${val2});`);
1111+
} else if (sum == 32) {
1112+
ctx.pushCode(`smartBuffer.writeUInt32BE(${tmpVal});`);
1113+
}
1114+
1115+
ctx.bitFields = [];
1116+
}
11141117
}
11151118

11161119
private generateSeek(ctx: Context) {

test/yy_primitive_encoder.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ describe("Primitive encoder", function() {
145145
);
146146
});
147147

148+
it('should encode 1-byte-length 8 bit field', function() {
149+
var parser = new Parser().bit8("a");
150+
151+
var buf = binaryLiteral("11111111");
152+
153+
assert.deepEqual(parser.parse(buf), {a: 255});
154+
assert.deepEqual(parser.encode({a: 255}), buf);
155+
});
156+
157+
it('should encode 1-byte-length 2x 4 bit fields', function() {
158+
var parser = new Parser().bit4("a").bit4("b");
159+
160+
var buf = binaryLiteral("1111 1111");
161+
162+
assert.deepEqual(parser.parse(buf), {a: 15, b: 15});
163+
assert.deepEqual(parser.encode({a: 15, b: 15}), buf);
164+
});
165+
148166
it("should encode 1-byte-length bit field sequence", function() {
149167
var parser = new Parser()
150168
.bit1("a")

0 commit comments

Comments
 (0)