Skip to content

Commit ad27cfe

Browse files
committed
Added "trim" and "padding" options for strings
1 parent 4443726 commit ad27cfe

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ the following keys:
174174
- `stripNull` - (Optional, must be used with `length`) If true, then strip
175175
null characters from end of the string. (Note: has no effect on encoding, but
176176
when used, then the parse() and encode() functions are not the exact opposite)
177+
- `trim` - (Optional, default to `false`) If true, then trim() (remove leading and trailing spaces)
178+
the parsed string.
179+
- `padding` - (Optional, Only used for encoding, default to `right`) If `left` then the string
180+
will be right aligned (padding left with leading spaces) depending of the `length` option
177181

178182
### buffer(name[, options])
179183
Parse bytes as a buffer. `name` should consist only of alpha numeric

lib/binary_parser.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,9 @@ Parser.prototype.generateString = function(ctx) {
758758
if (this.options.stripNull) {
759759
ctx.pushCode("{0} = {0}.replace(/\\x00+$/g, '')", name);
760760
}
761+
if (this.options.trim) {
762+
ctx.pushCode("{0} = {0}.trim()", name);
763+
}
761764
};
762765

763766
Parser.prototype.generate_encodeString = function(ctx) {
@@ -780,13 +783,22 @@ Parser.prototype.generate_encodeString = function(ctx) {
780783
// Compute padding length
781784
var padLen = ctx.generateTmpVariable();
782785
ctx.pushCode("{0} = {1} - {2}.length;", padLen, optLength, tmpBuf);
786+
if (this.options.padding === "left") {
787+
// Add heading padding spaces
788+
ctx.pushCode(
789+
"if ({0} > 0) {smartBuffer.writeString(' '.repeat({0}));}",
790+
padLen
791+
);
792+
}
783793
// Copy the temporary string buffer to current smartBuffer
784794
ctx.pushCode("smartBuffer.writeBuffer({0});", tmpBuf);
785-
// Add padding spaces
786-
ctx.pushCode(
787-
"if ({0} > 0) {smartBuffer.writeString(' '.repeat({0}));}",
788-
padLen
789-
);
795+
if (this.options.padding !== "left") {
796+
// Add trailing padding spaces
797+
ctx.pushCode(
798+
"if ({0} > 0) {smartBuffer.writeString(' '.repeat({0}));}",
799+
padLen
800+
);
801+
}
790802
} else {
791803
ctx.pushCode(
792804
"smartBuffer.writeString({0}, '{1}');",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "binary-parser-encoder",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Blazing-fast binary parser builder",
55
"main": "lib/binary_parser.js",
66
"devDependencies": {

test/primitive_parser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,19 @@ describe("Primitive parser", function() {
312312
a: "abc\u0000defghij\u0000"
313313
});
314314
});
315+
it("should parse string and trim content", function() {
316+
var buffer1 = Buffer.from(" abcd");
317+
var buffer2 = Buffer.from("abcd ");
318+
var buffer3 = Buffer.from(" abcd ");
319+
var parser = Parser.start().string("a", { length: 7, trim: true });
320+
var result = {
321+
a: "abcd"
322+
};
323+
324+
assert.deepEqual(parser.parse(buffer1), result);
325+
assert.deepEqual(parser.parse(buffer2), result);
326+
assert.deepEqual(parser.parse(buffer3), result);
327+
});
315328
});
316329

317330
describe("Buffer parser", function() {

test/yy_primitive_encoder.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,20 @@ describe("Primitive encoder", function() {
360360
var encoded = parser.encode(decoded);
361361
assert.deepEqual(encoded, buffer);
362362
});
363+
it("should encode string with default right padding", function() {
364+
var parser = Parser.start().string("a", { length: 6 });
365+
var encoded = parser.encode({ a: "abcd" });
366+
assert.deepEqual(encoded, Buffer.from("abcd "));
367+
encoded = parser.encode({ a: "abcdefgh" });
368+
assert.deepEqual(encoded, Buffer.from("abcdef"));
369+
});
370+
it("should encode string with left padding", function() {
371+
var parser = Parser.start().string("a", { length: 6, padding: "left" });
372+
var encoded = parser.encode({ a: "abcd" });
373+
assert.deepEqual(encoded, Buffer.from(" abcd"));
374+
encoded = parser.encode({ a: "abcdefgh" });
375+
assert.deepEqual(encoded, Buffer.from("abcdef"));
376+
});
363377
});
364378

365379
describe("Buffer encoder", function() {

0 commit comments

Comments
 (0)