mirror of
https://github.com/wader/fq.git
synced 2024-11-12 22:47:00 +03:00
interp: Rename s/bufferrange/buffer and cleanup
This commit is contained in:
parent
4eccb1ee71
commit
01a407a462
@ -21,11 +21,98 @@ func init() {
|
||||
functionRegisterFns = append(functionRegisterFns, func(i *Interp) []Function {
|
||||
return []Function{
|
||||
{"_tobitsrange", 0, 2, i._toBitsRange, nil},
|
||||
{"_is_buffer", 0, 0, i._isBuffer, nil},
|
||||
{"open", 0, 0, i._open, nil},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type ToBuffer interface {
|
||||
ToBuffer() (Buffer, error)
|
||||
}
|
||||
|
||||
func toBitBuf(v interface{}) (*bitio.Buffer, error) {
|
||||
return toBitBufEx(v, false)
|
||||
}
|
||||
|
||||
func toBitBufEx(v interface{}, inArray bool) (*bitio.Buffer, error) {
|
||||
switch vv := v.(type) {
|
||||
case ToBuffer:
|
||||
bv, err := vv.ToBuffer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bv.bb.BitBufRange(bv.r.Start, bv.r.Len)
|
||||
case string:
|
||||
return bitio.NewBufferFromBytes([]byte(vv), -1), nil
|
||||
case int, float64, *big.Int:
|
||||
bi, err := toBigInt(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if inArray {
|
||||
if bi.Cmp(big.NewInt(255)) > 0 || bi.Cmp(big.NewInt(0)) < 0 {
|
||||
return nil, fmt.Errorf("buffer byte list must be bytes (0-255) got %v", bi)
|
||||
}
|
||||
n := bi.Uint64()
|
||||
b := [1]byte{byte(n)}
|
||||
return bitio.NewBufferFromBytes(b[:], -1), nil
|
||||
}
|
||||
|
||||
// TODO: how should this work? "0xf | tobytes" 4bits or 8bits? now 4
|
||||
//padBefore := (8 - (bi.BitLen() % 8)) % 8
|
||||
padBefore := 0
|
||||
bb, err := bitio.NewBufferFromBytes(bi.Bytes(), -1).BitBufRange(int64(padBefore), int64(bi.BitLen()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bb, nil
|
||||
case []interface{}:
|
||||
var rr []bitio.BitReadAtSeeker
|
||||
// TODO: optimize byte array case, flatten into one slice
|
||||
for _, e := range vv {
|
||||
eBB, eErr := toBitBufEx(e, true)
|
||||
if eErr != nil {
|
||||
return nil, eErr
|
||||
}
|
||||
rr = append(rr, eBB)
|
||||
}
|
||||
|
||||
mb, err := bitio.NewMultiBitReader(rr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bb, err := bitio.NewBufferFromBitReadSeeker(mb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bb, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("value can't be a buffer")
|
||||
}
|
||||
}
|
||||
|
||||
func toBuffer(v interface{}) (Buffer, error) {
|
||||
switch vv := v.(type) {
|
||||
case ToBuffer:
|
||||
return vv.ToBuffer()
|
||||
default:
|
||||
bb, err := toBitBuf(v)
|
||||
if err != nil {
|
||||
return Buffer{}, err
|
||||
}
|
||||
return newBufferFromBuffer(bb, 8), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Interp) _isBuffer(c interface{}, a []interface{}) interface{} {
|
||||
_, ok := c.(ToBuffer)
|
||||
return ok
|
||||
}
|
||||
|
||||
// note is used to implement tobytes*/0 also
|
||||
func (i *Interp) _toBitsRange(c interface{}, a []interface{}) interface{} {
|
||||
var unit int
|
||||
@ -52,7 +139,7 @@ func (i *Interp) _toBitsRange(c interface{}, a []interface{}) interface{} {
|
||||
|
||||
// TODO: unit > 8?
|
||||
|
||||
bv, err := toBufferView(c)
|
||||
bv, err := toBuffer(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -60,27 +147,27 @@ func (i *Interp) _toBitsRange(c interface{}, a []interface{}) interface{} {
|
||||
|
||||
if !r {
|
||||
bb, _ := bv.toBuffer()
|
||||
return newBufferRangeFromBuffer(bb, unit)
|
||||
return newBufferFromBuffer(bb, unit)
|
||||
}
|
||||
|
||||
return bv
|
||||
}
|
||||
|
||||
type openFile struct {
|
||||
BufferRange
|
||||
Buffer
|
||||
filename string
|
||||
progressFn progressreadseeker.ProgressFn
|
||||
}
|
||||
|
||||
var _ ToBufferView = (*openFile)(nil)
|
||||
var _ ToBuffer = (*openFile)(nil)
|
||||
|
||||
func (of *openFile) Display(w io.Writer, opts Options) error {
|
||||
_, err := fmt.Fprintf(w, "<openFile %q>\n", of.filename)
|
||||
return err
|
||||
}
|
||||
|
||||
func (of *openFile) ToBufferView() (BufferRange, error) {
|
||||
return newBufferRangeFromBuffer(of.bb, 8), nil
|
||||
func (of *openFile) ToBuffer() (Buffer, error) {
|
||||
return newBufferFromBuffer(of.bb, 8), nil
|
||||
}
|
||||
|
||||
// def open: #:: string| => buffer
|
||||
@ -163,24 +250,24 @@ func (i *Interp) _open(c interface{}, a []interface{}) interface{} {
|
||||
return bbf
|
||||
}
|
||||
|
||||
var _ Value = BufferRange{}
|
||||
var _ ToBufferView = BufferRange{}
|
||||
var _ Value = Buffer{}
|
||||
var _ ToBuffer = Buffer{}
|
||||
|
||||
type BufferRange struct {
|
||||
type Buffer struct {
|
||||
bb *bitio.Buffer
|
||||
r ranges.Range
|
||||
unit int
|
||||
}
|
||||
|
||||
func newBufferRangeFromBuffer(bb *bitio.Buffer, unit int) BufferRange {
|
||||
return BufferRange{
|
||||
func newBufferFromBuffer(bb *bitio.Buffer, unit int) Buffer {
|
||||
return Buffer{
|
||||
bb: bb,
|
||||
r: ranges.Range{Start: 0, Len: bb.Len()},
|
||||
unit: unit,
|
||||
}
|
||||
}
|
||||
|
||||
func (bv BufferRange) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
|
||||
func (bv Buffer) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
|
||||
bb, err := bv.bb.BitBufRange(r.Start, r.Len)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -192,7 +279,7 @@ func (bv BufferRange) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (BufferRange) ExtKeys() []string {
|
||||
func (Buffer) ExtKeys() []string {
|
||||
return []string{
|
||||
"size",
|
||||
"start",
|
||||
@ -202,18 +289,18 @@ func (BufferRange) ExtKeys() []string {
|
||||
}
|
||||
}
|
||||
|
||||
func (bv BufferRange) ToBufferView() (BufferRange, error) {
|
||||
func (bv Buffer) ToBuffer() (Buffer, error) {
|
||||
return bv, nil
|
||||
}
|
||||
|
||||
func (bv BufferRange) JQValueLength() interface{} {
|
||||
func (bv Buffer) JQValueLength() interface{} {
|
||||
return int(bv.r.Len / int64(bv.unit))
|
||||
}
|
||||
func (bv BufferRange) JQValueSliceLen() interface{} {
|
||||
func (bv Buffer) JQValueSliceLen() interface{} {
|
||||
return bv.JQValueLength()
|
||||
}
|
||||
|
||||
func (bv BufferRange) JQValueIndex(index int) interface{} {
|
||||
func (bv Buffer) JQValueIndex(index int) interface{} {
|
||||
if index < 0 {
|
||||
return nil
|
||||
}
|
||||
@ -226,17 +313,17 @@ func (bv BufferRange) JQValueIndex(index int) interface{} {
|
||||
extraBits := uint((8 - bv.r.Len%8) % 8)
|
||||
return new(big.Int).Rsh(new(big.Int).SetBytes(buf.Bytes()), extraBits)
|
||||
}
|
||||
func (bv BufferRange) JQValueSlice(start int, end int) interface{} {
|
||||
func (bv Buffer) JQValueSlice(start int, end int) interface{} {
|
||||
rStart := int64(start * bv.unit)
|
||||
rLen := int64((end - start) * bv.unit)
|
||||
|
||||
return BufferRange{
|
||||
return Buffer{
|
||||
bb: bv.bb,
|
||||
r: ranges.Range{Start: bv.r.Start + rStart, Len: rLen},
|
||||
unit: bv.unit,
|
||||
}
|
||||
}
|
||||
func (bv BufferRange) JQValueKey(name string) interface{} {
|
||||
func (bv Buffer) JQValueKey(name string) interface{} {
|
||||
switch name {
|
||||
case "size":
|
||||
return new(big.Int).SetInt64(bv.r.Len / int64(bv.unit))
|
||||
@ -253,28 +340,28 @@ func (bv BufferRange) JQValueKey(name string) interface{} {
|
||||
if bv.unit == 1 {
|
||||
return bv
|
||||
}
|
||||
return BufferRange{bb: bv.bb, r: bv.r, unit: 1}
|
||||
return Buffer{bb: bv.bb, r: bv.r, unit: 1}
|
||||
case "bytes":
|
||||
if bv.unit == 8 {
|
||||
return bv
|
||||
}
|
||||
return BufferRange{bb: bv.bb, r: bv.r, unit: 8}
|
||||
return Buffer{bb: bv.bb, r: bv.r, unit: 8}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (bv BufferRange) JQValueEach() interface{} {
|
||||
func (bv Buffer) JQValueEach() interface{} {
|
||||
return nil
|
||||
}
|
||||
func (bv BufferRange) JQValueType() string {
|
||||
func (bv Buffer) JQValueType() string {
|
||||
return "buffer"
|
||||
}
|
||||
func (bv BufferRange) JQValueKeys() interface{} {
|
||||
func (bv Buffer) JQValueKeys() interface{} {
|
||||
return gojqextra.FuncTypeNameError{Name: "keys", Typ: "buffer"}
|
||||
}
|
||||
func (bv BufferRange) JQValueHas(key interface{}) interface{} {
|
||||
func (bv Buffer) JQValueHas(key interface{}) interface{} {
|
||||
return gojqextra.HasKeyTypeError{L: "buffer", R: fmt.Sprintf("%v", key)}
|
||||
}
|
||||
func (bv BufferRange) JQValueToNumber() interface{} {
|
||||
func (bv Buffer) JQValueToNumber() interface{} {
|
||||
buf, err := bv.toBytesBuffer(bv.r)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -282,21 +369,21 @@ func (bv BufferRange) JQValueToNumber() interface{} {
|
||||
extraBits := uint((8 - bv.r.Len%8) % 8)
|
||||
return new(big.Int).Rsh(new(big.Int).SetBytes(buf.Bytes()), extraBits)
|
||||
}
|
||||
func (bv BufferRange) JQValueToString() interface{} {
|
||||
func (bv Buffer) JQValueToString() interface{} {
|
||||
return bv.JQValueToGoJQ()
|
||||
}
|
||||
func (bv BufferRange) JQValueToGoJQ() interface{} {
|
||||
func (bv Buffer) JQValueToGoJQ() interface{} {
|
||||
buf, err := bv.toBytesBuffer(bv.r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
func (bv BufferRange) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
func (bv Buffer) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "buffer"}
|
||||
}
|
||||
|
||||
func (bv BufferRange) Display(w io.Writer, opts Options) error {
|
||||
func (bv Buffer) Display(w io.Writer, opts Options) error {
|
||||
if opts.RawOutput {
|
||||
bb, err := bv.toBuffer()
|
||||
if err != nil {
|
||||
@ -311,6 +398,6 @@ func (bv BufferRange) Display(w io.Writer, opts Options) error {
|
||||
return hexdump(w, bv, opts)
|
||||
}
|
||||
|
||||
func (bv BufferRange) toBuffer() (*bitio.Buffer, error) {
|
||||
func (bv Buffer) toBuffer() (*bitio.Buffer, error) {
|
||||
return bv.bb.BitBufRange(bv.r.Start, bv.r.Len)
|
||||
}
|
@ -343,7 +343,7 @@ func dump(v *decode.Value, w io.Writer, opts Options) error {
|
||||
}))
|
||||
}
|
||||
|
||||
func hexdump(w io.Writer, bv BufferRange, opts Options) error {
|
||||
func hexdump(w io.Writer, bv Buffer, opts Options) error {
|
||||
bb, err := bv.toBuffer()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -65,7 +65,7 @@ func makeStringBitBufTransformFn(
|
||||
return func(c interface{}, a []interface{}) interface{} {
|
||||
switch c := c.(type) {
|
||||
case string:
|
||||
bb, err := toBuffer(c)
|
||||
bb, err := toBitBuf(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -81,9 +81,9 @@ func makeStringBitBufTransformFn(
|
||||
}
|
||||
outBB := bitio.NewBufferFromBytes(buf.Bytes(), -1)
|
||||
|
||||
return newBufferRangeFromBuffer(outBB, 8)
|
||||
return newBufferFromBuffer(outBB, 8)
|
||||
default:
|
||||
bb, err := toBuffer(c)
|
||||
bb, err := toBitBuf(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -110,7 +110,7 @@ func makeStringBitBufTransformFn(
|
||||
// transform to buffer using fn
|
||||
func makeBitBufTransformFn(fn func(r io.Reader) (io.Reader, error)) func(c interface{}, a []interface{}) interface{} {
|
||||
return func(c interface{}, a []interface{}) interface{} {
|
||||
inBB, err := toBuffer(c)
|
||||
inBB, err := toBitBuf(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -127,14 +127,14 @@ func makeBitBufTransformFn(fn func(r io.Reader) (io.Reader, error)) func(c inter
|
||||
|
||||
outBB := bitio.NewBufferFromBytes(outBuf.Bytes(), -1)
|
||||
|
||||
return newBufferRangeFromBuffer(outBB, 8)
|
||||
return newBufferFromBuffer(outBB, 8)
|
||||
}
|
||||
}
|
||||
|
||||
// transform to buffer using fn
|
||||
func makeHashFn(fn func() (hash.Hash, error)) func(c interface{}, a []interface{}) interface{} {
|
||||
return func(c interface{}, a []interface{}) interface{} {
|
||||
inBB, err := toBuffer(c)
|
||||
inBB, err := toBitBuf(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -149,7 +149,7 @@ func makeHashFn(fn func() (hash.Hash, error)) func(c interface{}, a []interface{
|
||||
|
||||
outBB := bitio.NewBufferFromBytes(h.Sum(nil), -1)
|
||||
|
||||
return newBufferRangeFromBuffer(outBB, 8)
|
||||
return newBufferFromBuffer(outBB, 8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,7 +223,7 @@ func (i *Interp) aesCtr(c interface{}, a []interface{}) interface{} {
|
||||
ivBytes = make([]byte, block.BlockSize())
|
||||
}
|
||||
|
||||
bb, err := toBuffer(c)
|
||||
bb, err := toBitBuf(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -234,12 +234,12 @@ func (i *Interp) aesCtr(c interface{}, a []interface{}) interface{} {
|
||||
return err
|
||||
}
|
||||
|
||||
return newBufferRangeFromBuffer(bitio.NewBufferFromBytes(buf.Bytes(), -1), 8)
|
||||
return newBufferFromBuffer(bitio.NewBufferFromBytes(buf.Bytes(), -1), 8)
|
||||
}
|
||||
|
||||
func (i *Interp) _hexdump(c interface{}, a []interface{}) gojq.Iter {
|
||||
opts := i.Options(a[0])
|
||||
bv, err := toBufferView(c)
|
||||
bv, err := toBuffer(c)
|
||||
if err != nil {
|
||||
return gojq.NewIter(err)
|
||||
}
|
||||
|
@ -18,13 +18,6 @@ def hexdump: hexdump({display_bytes: 0});
|
||||
def hd($opts): hexdump($opts);
|
||||
def hd: hexdump;
|
||||
|
||||
# overload match to support buffers
|
||||
def _orig_match($val): match($val);
|
||||
def _orig_match($regex; $flags): match($regex; $flags);
|
||||
def _is_buffer: type == "buffer";
|
||||
def match($val): if _is_buffer then _bits_match($val) else _orig_match($val) end;
|
||||
def match($regex; $flags): if _is_buffer then _bits_match($regex; $flags) else _orig_match($regex; $flags) end;
|
||||
|
||||
def formats:
|
||||
_registry.formats;
|
||||
|
||||
|
@ -35,8 +35,9 @@ import (
|
||||
//go:embed interp.jq
|
||||
//go:embed internal.jq
|
||||
//go:embed options.jq
|
||||
//go:embed match.jq
|
||||
//go:embed value.jq
|
||||
//go:embed bufferrange.jq
|
||||
//go:embed buffer.jq
|
||||
//go:embed funcs.jq
|
||||
//go:embed grep.jq
|
||||
//go:embed args.jq
|
||||
@ -174,10 +175,6 @@ type Display interface {
|
||||
Display(w io.Writer, opts Options) error
|
||||
}
|
||||
|
||||
type ToBufferView interface {
|
||||
ToBufferView() (BufferRange, error)
|
||||
}
|
||||
|
||||
type JQValueEx interface {
|
||||
JQValueToGoJQEx(optsFn func() Options) interface{}
|
||||
}
|
||||
@ -264,7 +261,7 @@ func toBigInt(v interface{}) (*big.Int, error) {
|
||||
func toBytes(v interface{}) ([]byte, error) {
|
||||
switch v := v.(type) {
|
||||
default:
|
||||
bb, err := toBuffer(v)
|
||||
bb, err := toBitBuf(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("value is not bytes")
|
||||
}
|
||||
@ -277,83 +274,6 @@ func toBytes(v interface{}) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func toBuffer(v interface{}) (*bitio.Buffer, error) {
|
||||
return toBufferEx(v, false)
|
||||
}
|
||||
|
||||
func toBufferEx(v interface{}, inArray bool) (*bitio.Buffer, error) {
|
||||
switch vv := v.(type) {
|
||||
case ToBufferView:
|
||||
bv, err := vv.ToBufferView()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bv.bb.BitBufRange(bv.r.Start, bv.r.Len)
|
||||
case string:
|
||||
return bitio.NewBufferFromBytes([]byte(vv), -1), nil
|
||||
case int, float64, *big.Int:
|
||||
bi, err := toBigInt(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if inArray {
|
||||
if bi.Cmp(big.NewInt(255)) > 0 || bi.Cmp(big.NewInt(0)) < 0 {
|
||||
return nil, fmt.Errorf("buffer byte list must be bytes (0-255) got %v", bi)
|
||||
}
|
||||
n := bi.Uint64()
|
||||
b := [1]byte{byte(n)}
|
||||
return bitio.NewBufferFromBytes(b[:], -1), nil
|
||||
}
|
||||
|
||||
// TODO: how should this work? "0xf | tobytes" 4bits or 8bits? now 4
|
||||
//padBefore := (8 - (bi.BitLen() % 8)) % 8
|
||||
padBefore := 0
|
||||
bb, err := bitio.NewBufferFromBytes(bi.Bytes(), -1).BitBufRange(int64(padBefore), int64(bi.BitLen()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bb, nil
|
||||
case []interface{}:
|
||||
var rr []bitio.BitReadAtSeeker
|
||||
// TODO: optimize byte array case, flatten into one slice
|
||||
for _, e := range vv {
|
||||
eBB, eErr := toBufferEx(e, true)
|
||||
if eErr != nil {
|
||||
return nil, eErr
|
||||
}
|
||||
rr = append(rr, eBB)
|
||||
}
|
||||
|
||||
mb, err := bitio.NewMultiBitReader(rr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bb, err := bitio.NewBufferFromBitReadSeeker(mb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bb, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("value can't be a buffer")
|
||||
}
|
||||
}
|
||||
|
||||
func toBufferView(v interface{}) (BufferRange, error) {
|
||||
switch vv := v.(type) {
|
||||
case ToBufferView:
|
||||
return vv.ToBufferView()
|
||||
default:
|
||||
bb, err := toBuffer(v)
|
||||
if err != nil {
|
||||
return BufferRange{}, err
|
||||
}
|
||||
return newBufferRangeFromBuffer(bb, 8), nil
|
||||
}
|
||||
}
|
||||
|
||||
func queryErrorPosition(src string, v error) pos.Pos {
|
||||
var offset int
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
include "internal";
|
||||
include "options";
|
||||
include "match";
|
||||
include "value";
|
||||
include "bufferrange";
|
||||
include "buffer";
|
||||
include "funcs";
|
||||
include "grep";
|
||||
include "args";
|
||||
|
@ -13,15 +13,15 @@ import (
|
||||
func init() {
|
||||
functionRegisterFns = append(functionRegisterFns, func(i *Interp) []Function {
|
||||
return []Function{
|
||||
{"_bits_match", 1, 2, nil, i._bitsMatch},
|
||||
{"_match_buffer", 1, 2, nil, i._bufferMatch},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (i *Interp) _bitsMatch(c interface{}, a []interface{}) gojq.Iter {
|
||||
func (i *Interp) _bufferMatch(c interface{}, a []interface{}) gojq.Iter {
|
||||
var ok bool
|
||||
|
||||
bv, err := toBufferView(c)
|
||||
bv, err := toBuffer(c)
|
||||
if err != nil {
|
||||
return gojq.NewIter(err)
|
||||
}
|
||||
@ -97,7 +97,7 @@ func (i *Interp) _bitsMatch(c interface{}, a []interface{}) gojq.Iter {
|
||||
}
|
||||
|
||||
matchBitOff := (off + int64(l[0])) * 8
|
||||
bbo := BufferRange{
|
||||
bbo := Buffer{
|
||||
bb: bv.bb,
|
||||
r: ranges.Range{
|
||||
Start: bv.r.Start + matchBitOff,
|
5
pkg/interp/match.jq
Normal file
5
pkg/interp/match.jq
Normal file
@ -0,0 +1,5 @@
|
||||
# overload match to support buffer
|
||||
def _orig_match($val): match($val);
|
||||
def _orig_match($regex; $flags): match($regex; $flags);
|
||||
def match($val): if _is_buffer then _match_buffer($val) else _orig_match($val) end;
|
||||
def match($regex; $flags): if _is_buffer then _match_buffer($regex; $flags) else _orig_match($regex; $flags) end;
|
20
pkg/interp/testdata/grep.fqtest
vendored
20
pkg/interp/testdata/grep.fqtest
vendored
@ -56,24 +56,4 @@ mp3> bgrep(44100, "ID", "^ID3$", "^ID.?$", "Info", "magic", "\u00ff", [0x49, 0x4
|
||||
mp3> "64ff65ff66" | hex | bgrep("\u00ff"; "b")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0|64 ff 65 ff 66| |d.e.f| |.: raw bits 0x0-0x4.7 (5)
|
||||
mp3> "64ff65ff66" | hex | match("\u00ff"; "b")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| ff 65 ff 66| | .e.f| |.: raw bits 0x1-0x4.7 (4)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| ff 66| | .f| |.: raw bits 0x3-0x4.7 (2)
|
||||
mp3> "aöaöa" | tobytes | match("ö")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: raw bits 0x1-0x6.7 (6)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61| | ..a| |.: raw bits 0x4-0x6.7 (3)
|
||||
mp3> "aöaöa" | tobytes | match("\u00c3"; "b")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: raw bits 0x1-0x6.7 (6)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61| | ..a| |.: raw bits 0x4-0x6.7 (3)
|
||||
mp3> "aöaöa" | tobytes | match([0xc3])
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: raw bits 0x1-0x6.7 (6)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61| | ..a| |.: raw bits 0x4-0x6.7 (3)
|
||||
mp3> ^D
|
||||
|
32
pkg/interp/testdata/match.fqtest
vendored
Normal file
32
pkg/interp/testdata/match.fqtest
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
$ fq -i -d mp3 . /test.mp3
|
||||
mp3> "64ff65ff66" | hex | match("\u00ff"; "b")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| ff 65 ff 66| | .e.f| |.: raw bits 0x1-0x4.7 (4)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| ff 66| | .f| |.: raw bits 0x3-0x4.7 (2)
|
||||
mp3> "aöaöa" | tobytes | match("ö")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: raw bits 0x1-0x6.7 (6)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61| | ..a| |.: raw bits 0x4-0x6.7 (3)
|
||||
mp3> "aöaöa" | tobytes | match("\u00c3"; "b")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: raw bits 0x1-0x6.7 (6)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61| | ..a| |.: raw bits 0x4-0x6.7 (3)
|
||||
mp3> "aöaöa" | tobytes | match([0xc3])
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: raw bits 0x1-0x6.7 (6)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0| c3 b6 61| | ..a| |.: raw bits 0x4-0x6.7 (3)
|
||||
mp3> .frames[1].data | match("3\u0085"; "b")
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0f0| 33 85 d3 64| 3..d|.: raw bits 0xfc-0x1b2.7 (183)
|
||||
0x100|f1 a1 c1 08 1c 58 1f 5e 1f 18 1c 46 04 1e 89 e5|.....X.^...F....|
|
||||
* |until 0x1b2.7 (183) | |
|
||||
mp3> .frames[1].data | match([0x33, 0x85])
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0f0| 33 85 d3 64| 3..d|.: raw bits 0xfc-0x1b2.7 (183)
|
||||
0x100|f1 a1 c1 08 1c 58 1f 5e 1f 18 1c 46 04 1e 89 e5|.....X.^...F....|
|
||||
* |until 0x1b2.7 (183) | |
|
||||
mp3> ^D
|
@ -49,7 +49,7 @@ func (err notUpdateableError) Error() string {
|
||||
// used by _isDecodeValue
|
||||
type DecodeValue interface {
|
||||
Value
|
||||
ToBufferView
|
||||
ToBuffer
|
||||
|
||||
DecodeValue() *decode.Value
|
||||
}
|
||||
@ -112,7 +112,7 @@ func (i *Interp) _decode(c interface{}, a []interface{}) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
bv, err := toBufferView(c)
|
||||
bv, err := toBuffer(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -279,8 +279,8 @@ func (dvb decodeValueBase) DecodeValue() *decode.Value {
|
||||
}
|
||||
|
||||
func (dvb decodeValueBase) Display(w io.Writer, opts Options) error { return dump(dvb.dv, w, opts) }
|
||||
func (dvb decodeValueBase) ToBufferView() (BufferRange, error) {
|
||||
return BufferRange{bb: dvb.dv.RootBitBuf, r: dvb.dv.InnerRange(), unit: 8}, nil
|
||||
func (dvb decodeValueBase) ToBuffer() (Buffer, error) {
|
||||
return Buffer{bb: dvb.dv.RootBitBuf, r: dvb.dv.InnerRange(), unit: 8}, nil
|
||||
}
|
||||
func (dvb decodeValueBase) ExtKeys() []string {
|
||||
kv := []string{
|
||||
@ -388,13 +388,13 @@ func (dvb decodeValueBase) JQValueKey(name string) interface{} {
|
||||
return nil
|
||||
}
|
||||
case "_bits":
|
||||
return BufferRange{
|
||||
return Buffer{
|
||||
bb: dv.RootBitBuf,
|
||||
r: dv.Range,
|
||||
unit: 1,
|
||||
}
|
||||
case "_bytes":
|
||||
return BufferRange{
|
||||
return Buffer{
|
||||
bb: dv.RootBitBuf,
|
||||
r: dv.Range,
|
||||
unit: 8,
|
||||
@ -448,7 +448,7 @@ func (v decodeValue) JQValueToGoJQEx(optsFn func() Options) interface{} {
|
||||
return v.JQValueToGoJQ()
|
||||
}
|
||||
|
||||
bv, err := v.decodeValueBase.ToBufferView()
|
||||
bv, err := v.decodeValueBase.ToBuffer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user