mirror of
https://github.com/wader/fq.git
synced 2024-12-28 08:02:28 +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:
commit
9a7909abfa
@ -19,6 +19,39 @@ import (
|
||||
"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 {
|
||||
expr string
|
||||
env []string
|
||||
@ -101,8 +134,13 @@ func (cr *CaseRun) Stdin() interp.Input {
|
||||
}
|
||||
|
||||
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{
|
||||
Writer: cr.ActualStdoutBuf,
|
||||
Writer: w,
|
||||
Terminal: cr.getEnvInt("_STDOUT_ISTERMINAL") != 0,
|
||||
Width: cr.getEnvInt("_STDOUT_WIDTH"),
|
||||
Height: cr.getEnvInt("_STDOUT_HEIGHT"),
|
||||
@ -327,31 +365,6 @@ type Section struct {
|
||||
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 {
|
||||
var sections []Section
|
||||
|
||||
|
@ -149,7 +149,7 @@ func (i *Interp) _toBits(c interface{}, a []interface{}) interface{} {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bb, err := newBinaryFromBitReader(br, bv.unit, bv.pad)
|
||||
bb, err := newBinaryFromBitReader(br, bv.unit, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -272,6 +272,7 @@ type Binary struct {
|
||||
pad int64
|
||||
}
|
||||
|
||||
//nolint:unparam
|
||||
func newBinaryFromBitReader(br bitio.ReaderAtSeeker, unit int, pad int64) (Binary, error) {
|
||||
l, err := bitioextra.Len(br)
|
||||
if err != nil {
|
||||
|
@ -4,8 +4,6 @@ def tobitsrange: _tobits(1; true; 0);
|
||||
def tobytesrange: _tobits(8; true; 0);
|
||||
def tobits($pad): _tobits(1; 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
|
||||
def _re_quote_meta:
|
||||
|
8
pkg/interp/testdata/binary_stdout.fqtest
vendored
Normal file
8
pkg/interp/testdata/binary_stdout.fqtest
vendored
Normal 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\
|
Loading…
Reference in New Issue
Block a user