1
1
mirror of https://github.com/wader/fq.git synced 2024-12-29 16:42:06 +03:00

Merge pull request #202 from wader/binary-stdout-pad-fix

interp: Remove to*range pad argument and fix stdout padding issue
This commit is contained in:
Mattias Wadman 2022-03-17 20:22:54 +01:00 committed by GitHub
commit 9a7909abfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 29 deletions

View File

@ -19,6 +19,39 @@ import (
"github.com/wader/fq/pkg/interp" "github.com/wader/fq/pkg/interp"
) )
var unescapeRe = regexp.MustCompile(`\\(?:t|b|n|r|0(?:b[01]{8}|x[0-f]{2}))`)
func Unescape(s string) string {
return unescapeRe.ReplaceAllStringFunc(s, func(r string) string {
switch {
case r == `\n`:
return "\n"
case r == `\r`:
return "\r"
case r == `\t`:
return "\t"
case r == `\b`:
return "\b"
case strings.HasPrefix(r, `\0b`):
b, _ := bitio.BytesFromBitString(r[3:])
return string(b)
case strings.HasPrefix(r, `\0x`):
b, _ := hex.DecodeString(r[3:])
return string(b)
default:
return r
}
})
}
var escapeRe = regexp.MustCompile(`[^[:print:][:space:]]`)
func Escape(s string) string {
return string(escapeRe.ReplaceAllFunc([]byte(s), func(r []byte) []byte {
return []byte(fmt.Sprintf(`\0x%.2x`, r[0]))
}))
}
type CaseReadline struct { type CaseReadline struct {
expr string expr string
env []string env []string
@ -101,8 +134,13 @@ func (cr *CaseRun) Stdin() interp.Input {
} }
func (cr *CaseRun) Stdout() interp.Output { func (cr *CaseRun) Stdout() interp.Output {
var w io.Writer = cr.ActualStdoutBuf
if cr.getEnvInt("_STDOUT_HEX") != 0 {
w = hex.NewEncoder(cr.ActualStdoutBuf)
}
return CaseRunOutput{ return CaseRunOutput{
Writer: cr.ActualStdoutBuf, Writer: w,
Terminal: cr.getEnvInt("_STDOUT_ISTERMINAL") != 0, Terminal: cr.getEnvInt("_STDOUT_ISTERMINAL") != 0,
Width: cr.getEnvInt("_STDOUT_WIDTH"), Width: cr.getEnvInt("_STDOUT_WIDTH"),
Height: cr.getEnvInt("_STDOUT_HEIGHT"), Height: cr.getEnvInt("_STDOUT_HEIGHT"),
@ -327,31 +365,6 @@ type Section struct {
Value string Value string
} }
var unescapeRe = regexp.MustCompile(`\\(?:t|b|n|r|0(?:b[01]{8}|x[0-f]{2}))`)
func Unescape(s string) string {
return unescapeRe.ReplaceAllStringFunc(s, func(r string) string {
switch {
case r == `\n`:
return "\n"
case r == `\r`:
return "\r"
case r == `\t`:
return "\t"
case r == `\b`:
return "\b"
case strings.HasPrefix(r, `\0b`):
b, _ := bitio.BytesFromBitString(r[3:])
return string(b)
case strings.HasPrefix(r, `\0x`):
b, _ := hex.DecodeString(r[3:])
return string(b)
default:
return r
}
})
}
func SectionParser(re *regexp.Regexp, s string) []Section { func SectionParser(re *regexp.Regexp, s string) []Section {
var sections []Section var sections []Section

View File

@ -149,7 +149,7 @@ func (i *Interp) _toBits(c interface{}, a []interface{}) interface{} {
if err != nil { if err != nil {
return err return err
} }
bb, err := newBinaryFromBitReader(br, bv.unit, bv.pad) bb, err := newBinaryFromBitReader(br, bv.unit, 0)
if err != nil { if err != nil {
return err return err
} }
@ -272,6 +272,7 @@ type Binary struct {
pad int64 pad int64
} }
//nolint:unparam
func newBinaryFromBitReader(br bitio.ReaderAtSeeker, unit int, pad int64) (Binary, error) { func newBinaryFromBitReader(br bitio.ReaderAtSeeker, unit int, pad int64) (Binary, error) {
l, err := bitioextra.Len(br) l, err := bitioextra.Len(br)
if err != nil { if err != nil {

View File

@ -4,8 +4,6 @@ def tobitsrange: _tobits(1; true; 0);
def tobytesrange: _tobits(8; true; 0); def tobytesrange: _tobits(8; true; 0);
def tobits($pad): _tobits(1; false; $pad); def tobits($pad): _tobits(1; false; $pad);
def tobytes($pad): _tobits(8; false; $pad); def tobytes($pad): _tobits(8; false; $pad);
def tobitsrange($pad): _tobits(1; true; $pad);
def tobytesrange($pad): _tobits(8; true; $pad);
# same as regexp.QuoteMeta # same as regexp.QuoteMeta
def _re_quote_meta: def _re_quote_meta:

View File

@ -0,0 +1,8 @@
$ _STDOUT_ISTERMINAL=0 _STDOUT_HEX=1 NO_COLOR=1 fq -n '[1,1,1 | tobits] | tobytes'
07\
$ _STDOUT_ISTERMINAL=0 _STDOUT_HEX=1 NO_COLOR=1 fq -n '[1,1,1 | tobits] | tobytes(3)'
000007\
$ _STDOUT_ISTERMINAL=0 _STDOUT_HEX=1 NO_COLOR=1 fq -n '[1,1,1 | tobits] | tobits(9)'
0380\
$ _STDOUT_ISTERMINAL=0 _STDOUT_HEX=1 NO_COLOR=1 fq -n '[5 | tobits(12)], [3 | tobytes(3)] | tobytes'
0005000003\