-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return early when packing zero length fields (#149)
* Ignore zero length fields * add flag that allows parsing of prefixed composites to only up to specified length * remove debug line * add tests for zero length fields Co-authored-by: dakinola <dakinola@thoughtmachine.net>
- Loading branch information
1 parent
9c1cf1e
commit e946dc1
Showing
13 changed files
with
280 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package padding | ||
|
||
import ( | ||
"bytes" | ||
"unicode/utf8" | ||
) | ||
|
||
// Right returns a new right-side padder | ||
var Right func(pad rune) Padder = NewRightPadder | ||
|
||
type rightPadder struct { | ||
pad []byte | ||
} | ||
|
||
// NewRightPadder takes the given byte character and returns a padder | ||
// which pads fields to the right of their values (for left-justified values) | ||
func NewRightPadder(pad rune) Padder { | ||
buf := make([]byte, utf8.RuneLen(pad)) | ||
utf8.EncodeRune(buf, pad) | ||
|
||
return &rightPadder{buf} | ||
} | ||
|
||
func (p *rightPadder) Pad(data []byte, length int) []byte { | ||
if len(data) >= length { | ||
return data | ||
} | ||
|
||
padding := bytes.Repeat(p.pad, length-len(data)) | ||
return append(data, padding...) | ||
} | ||
|
||
func (p *rightPadder) Unpad(data []byte) []byte { | ||
pad, _ := utf8.DecodeRune(p.pad) | ||
|
||
return bytes.TrimRightFunc(data, func(r rune) bool { | ||
return r == pad | ||
}) | ||
} | ||
|
||
func (p *rightPadder) Inspect() []byte { | ||
return p.pad | ||
} |
Oops, something went wrong.