Skip to content

Commit 6f267b6

Browse files
committed
Delivers keichi#17 Add an option to set custom string padding char
1 parent 482fb0b commit 6f267b6

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ the following keys:
177177
- `trim` - (Optional, default to `false`) If true, then trim() (remove leading and trailing spaces)
178178
the parsed string.
179179
- `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
180+
will be right aligned (padding left with `padd` char or space) depending of the `length` option
181+
- `padd` - (Optional, Only used for encoding with `length` specified) A string from which first character (1 Byte)
182+
is used as a padding char if necessary (provided string length is less than `length` option). Note: Only 'ascii'
183+
or utf8 < 0x80 are alowed (fallback to 'space' padding else).
181184

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

lib/binary_parser.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,20 +782,31 @@ Parser.prototype.generate_encodeString = function(ctx) {
782782
// Compute padding length
783783
var padLen = ctx.generateTmpVariable();
784784
ctx.pushCode("{0} = {1} - {2}.length;", padLen, optLength, tmpBuf);
785+
var padCharVar = ctx.generateTmpVariable();
786+
var padChar = " ";
787+
if (this.options.padd && typeof this.options.padd === "string") {
788+
var code = this.options.padd.charCodeAt(0);
789+
if (code < 0x80) {
790+
padChar = String.fromCharCode(code);
791+
}
792+
}
793+
ctx.pushCode('{0} = "{1}";', padCharVar, padChar);
785794
if (this.options.padding === "left") {
786795
// Add heading padding spaces
787796
ctx.pushCode(
788-
"if ({0} > 0) {smartBuffer.writeString(' '.repeat({0}));}",
789-
padLen
797+
"if ({0} > 0) {smartBuffer.writeString({1}.repeat({0}));}",
798+
padLen,
799+
padCharVar
790800
);
791801
}
792802
// Copy the temporary string buffer to current smartBuffer
793803
ctx.pushCode("smartBuffer.writeBuffer({0});", tmpBuf);
794804
if (this.options.padding !== "left") {
795805
// Add trailing padding spaces
796806
ctx.pushCode(
797-
"if ({0} > 0) {smartBuffer.writeString(' '.repeat({0}));}",
798-
padLen
807+
"if ({0} > 0) {smartBuffer.writeString({1}.repeat({0}));}",
808+
padLen,
809+
padCharVar
799810
);
800811
}
801812
} else {

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.3",
3+
"version": "1.4.4",
44
"description": "Blazing-fast binary parser builder",
55
"main": "lib/binary_parser.js",
66
"devDependencies": {

test/yy_primitive_encoder.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,39 @@ describe("Primitive encoder", function() {
374374
encoded = parser.encode({ a: "abcdefgh" });
375375
assert.deepEqual(encoded, Buffer.from("abcdef"));
376376
});
377+
it("should encode string with right padding and provided padding char", function() {
378+
var parser = Parser.start().string("a", { length: 6, padd: "x" });
379+
var encoded = parser.encode({ a: "abcd" });
380+
assert.deepEqual(encoded, Buffer.from("abcdxx"));
381+
encoded = parser.encode({ a: "abcdefgh" });
382+
assert.deepEqual(encoded, Buffer.from("abcdef"));
383+
});
384+
it("should encode string with left padding and provided padding char", function() {
385+
var parser = Parser.start().string("a", {
386+
length: 6,
387+
padding: "left",
388+
padd: "."
389+
});
390+
var encoded = parser.encode({ a: "abcd" });
391+
assert.deepEqual(encoded, Buffer.from("..abcd"));
392+
encoded = parser.encode({ a: "abcdefgh" });
393+
assert.deepEqual(encoded, Buffer.from("abcdef"));
394+
});
395+
it("should encode string with padding and padding char 0", function() {
396+
var parser = Parser.start().string("a", { length: 6, padd: "\u0000" });
397+
var encoded = parser.encode({ a: "abcd" });
398+
assert.deepEqual(encoded, Buffer.from("abcd\u0000\u0000"));
399+
});
400+
it("should encode string with padding and first byte of padding char", function() {
401+
var parser = Parser.start().string("a", { length: 6, padd: "1234" });
402+
var encoded = parser.encode({ a: "abcd" });
403+
assert.deepEqual(encoded, Buffer.from("abcd11"));
404+
});
405+
it("should encode string with space padding when padd char is not encoded on 1 Byte", function() {
406+
var parser = Parser.start().string("a", { length: 6, padd: "こ" });
407+
var encoded = parser.encode({ a: "abcd" });
408+
assert.deepEqual(encoded, Buffer.from("abcd "));
409+
});
377410
});
378411

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

0 commit comments

Comments
 (0)