1
1
mirror of https://github.com/wader/fq.git synced 2025-01-08 23:59:50 +03:00

Cleanup and rename s/BufferView/BufferRange

This commit is contained in:
Mattias Wadman 2021-10-21 00:46:21 +02:00
parent eedfd16b40
commit 95ec5e1077
8 changed files with 53 additions and 89 deletions

View File

@ -3,32 +3,31 @@ package ansi_test
import (
"bytes"
"fmt"
"log"
"testing"
"github.com/wader/fq/internal/ansi"
)
func Test(t *testing.T) {
func TestF(t *testing.T) {
c := ansi.FromString("blue")
b := &bytes.Buffer{}
fmt.Fprintf(b, "%s", c.F("bla"))
log.Printf("b.String(): %#+v\n", b.String())
fmt.Fprintf(b, "%s", c.F("test"))
actual := b.String()
expected := "\x1b[34mtest\x1b[39m"
if expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
}
func Test2(t *testing.T) {
func TestW(t *testing.T) {
c := ansi.FromString("blue+underline")
b := &bytes.Buffer{}
_, _ = c.W(b).Write([]byte("bla"))
log.Printf("b.String(): %#+v\n", b.String())
_, _ = c.W(b).Write([]byte("test"))
actual := b.String()
expected := "\x1b[34;4mtest\x1b[39;24m"
if expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
}
func TestLen(t *testing.T) {

View File

@ -34,41 +34,6 @@ type BitWriter interface {
WriteBits(p []byte, nBits int) (n int, err error)
}
type AlignBitWriter struct {
W BitWriter
N int
c int64
}
func (a *AlignBitWriter) WriteBits(p []byte, nBits int) (n int, err error) {
n, err = a.W.WriteBits(p, nBits)
a.c += int64(n)
return n, err
}
func (a *AlignBitWriter) Close() error {
n := int64(a.N)
r := int((n - a.c%n) % n)
if r == 0 {
return nil
}
b := make([]byte, a.N/8+1)
_, err := a.W.WriteBits(b, r)
return err
}
type AlignBitReader struct {
R BitReaderAt
N int
c int64
}
func (a *AlignBitReader) ReadBitsAt(p []byte, nBits int, bitOff int64) (n int, err error) {
n, err = a.R.ReadBitsAt(p, nBits, bitOff)
a.c += int64(n)
return n, err
}
func CopyBuffer(dst BitWriter, src BitReader, buf []byte) (n int64, err error) {
// same default size as io.Copy
if buf == nil {

View File

@ -66,10 +66,10 @@ func (d *D) FieldF{{$bits}}{{$e.short}}(name string) float64 { return d.FieldFE(
{{ range $e :=$.endians }}
// FP{{$e.short}} read a nBits {{$e.desc}} fBits fraction bits fixed-point number
// FP{{$e.short}} read a nBits {{$e.desc}} fBits fraction bits fixed-point number
func (d *D) FP{{$e.short}}(nBits int, fBits int64) float64 { return d.FPE(nBits, fBits, {{$e.var}}) }
// TryFP{{$e.short}} try read a nBits {{$e.desc}} fBits fraction bits fixed-point number
// TryFP{{$e.short}} try read a nBits {{$e.desc}} fBits fraction bits fixed-point number
func (d *D) TryFP{{$e.short}}(nBits int, fBits int64) (float64, error) { return d.TryFPE(nBits, fBits, {{$e.var}}) }
{{- range $fp := $.fpbits }}

View File

@ -11,24 +11,24 @@ import (
"github.com/wader/fq/pkg/ranges"
)
var _ Value = BufferView{}
var _ ToBufferView = BufferView{}
var _ Value = BufferRange{}
var _ ToBufferView = BufferRange{}
type BufferView struct {
type BufferRange struct {
bb *bitio.Buffer
r ranges.Range
unit int
}
func bufferViewFromBuffer(bb *bitio.Buffer, unit int) BufferView {
return BufferView{
func bufferViewFromBuffer(bb *bitio.Buffer, unit int) BufferRange {
return BufferRange{
bb: bb,
r: ranges.Range{Start: 0, Len: bb.Len()},
unit: unit,
}
}
func (bv BufferView) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
func (bv BufferRange) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
bb, err := bv.bb.BitBufRange(r.Start, r.Len)
if err != nil {
return nil, err
@ -40,8 +40,8 @@ func (bv BufferView) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
return buf, nil
}
func (BufferView) DisplayName() string { return "buffer" }
func (BufferView) ExtKeys() []string {
func (BufferRange) DisplayName() string { return "buffer" }
func (BufferRange) ExtKeys() []string {
return []string{
"size",
"start",
@ -51,18 +51,18 @@ func (BufferView) ExtKeys() []string {
}
}
func (bv BufferView) ToBufferView() (BufferView, error) {
func (bv BufferRange) ToBufferView() (BufferRange, error) {
return bv, nil
}
func (bv BufferView) JQValueLength() interface{} {
func (bv BufferRange) JQValueLength() interface{} {
return int(bv.r.Len / int64(bv.unit))
}
func (bv BufferView) JQValueSliceLen() interface{} {
func (bv BufferRange) JQValueSliceLen() interface{} {
return bv.JQValueLength()
}
func (bv BufferView) JQValueIndex(index int) interface{} {
func (bv BufferRange) JQValueIndex(index int) interface{} {
if index < 0 {
return nil
}
@ -75,17 +75,17 @@ func (bv BufferView) 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 BufferView) JQValueSlice(start int, end int) interface{} {
func (bv BufferRange) JQValueSlice(start int, end int) interface{} {
rStart := int64(start * bv.unit)
rLen := int64((end - start) * bv.unit)
return BufferView{
return BufferRange{
bb: bv.bb,
r: ranges.Range{Start: bv.r.Start + rStart, Len: rLen},
unit: bv.unit,
}
}
func (bv BufferView) JQValueKey(name string) interface{} {
func (bv BufferRange) JQValueKey(name string) interface{} {
switch name {
case "size":
return new(big.Int).SetInt64(bv.r.Len / int64(bv.unit))
@ -102,28 +102,28 @@ func (bv BufferView) JQValueKey(name string) interface{} {
if bv.unit == 1 {
return bv
}
return BufferView{bb: bv.bb, r: bv.r, unit: 1}
return BufferRange{bb: bv.bb, r: bv.r, unit: 1}
case "bytes":
if bv.unit == 8 {
return bv
}
return BufferView{bb: bv.bb, r: bv.r, unit: 8}
return BufferRange{bb: bv.bb, r: bv.r, unit: 8}
}
return nil
}
func (bv BufferView) JQValueEach() interface{} {
func (bv BufferRange) JQValueEach() interface{} {
return nil
}
func (bv BufferView) JQValueType() string {
func (bv BufferRange) JQValueType() string {
return "buffer"
}
func (bv BufferView) JQValueKeys() interface{} {
func (bv BufferRange) JQValueKeys() interface{} {
return gojqextra.FuncTypeNameError{Name: "keys", Typ: "buffer"}
}
func (bv BufferView) JQValueHas(key interface{}) interface{} {
func (bv BufferRange) JQValueHas(key interface{}) interface{} {
return gojqextra.HasKeyTypeError{L: "buffer", R: fmt.Sprintf("%v", key)}
}
func (bv BufferView) JQValueToNumber() interface{} {
func (bv BufferRange) JQValueToNumber() interface{} {
buf, err := bv.toBytesBuffer(bv.r)
if err != nil {
return err
@ -131,21 +131,21 @@ func (bv BufferView) JQValueToNumber() interface{} {
extraBits := uint((8 - bv.r.Len%8) % 8)
return new(big.Int).Rsh(new(big.Int).SetBytes(buf.Bytes()), extraBits)
}
func (bv BufferView) JQValueToString() interface{} {
func (bv BufferRange) JQValueToString() interface{} {
return bv.JQValueToGoJQ()
}
func (bv BufferView) JQValueToGoJQ() interface{} {
func (bv BufferRange) JQValueToGoJQ() interface{} {
buf, err := bv.toBytesBuffer(bv.r)
if err != nil {
return err
}
return buf.String()
}
func (bv BufferView) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
func (bv BufferRange) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "buffer"}
}
func (bv BufferView) Display(w io.Writer, opts Options) error {
func (bv BufferRange) Display(w io.Writer, opts Options) error {
if opts.RawOutput {
bb, err := bv.toBuffer()
if err != nil {
@ -160,6 +160,6 @@ func (bv BufferView) Display(w io.Writer, opts Options) error {
return hexdump(w, bv, opts)
}
func (bv BufferView) toBuffer() (*bitio.Buffer, error) {
func (bv BufferRange) toBuffer() (*bitio.Buffer, error) {
return bv.bb.BitBufRange(bv.r.Start, bv.r.Len)
}

View File

@ -350,7 +350,7 @@ func dump(v *decode.Value, w io.Writer, opts Options) error {
}))
}
func hexdump(w io.Writer, bv BufferView, opts Options) error {
func hexdump(w io.Writer, bv BufferRange, opts Options) error {
// TODO: hack
opts.Verbose = true
return dump(

View File

@ -498,7 +498,7 @@ func (bbf *bitBufFile) Display(w io.Writer, opts Options) error {
return err
}
func (bbf *bitBufFile) ToBufferView() (BufferView, error) {
func (bbf *bitBufFile) ToBufferView() (BufferRange, error) {
return bufferViewFromBuffer(bbf.bb, 8), nil
}
@ -914,7 +914,7 @@ func (i *Interp) _bitsMatch(c interface{}, a []interface{}) gojq.Iter {
}
matchBitOff := (off + int64(l[0])) * 8
bbo := BufferView{
bbo := BufferRange{
bb: bv.bb,
r: ranges.Range{
Start: bv.r.Start + matchBitOff,

View File

@ -181,7 +181,7 @@ type Display interface {
}
type ToBufferView interface {
ToBufferView() (BufferView, error)
ToBufferView() (BufferRange, error)
}
type JQValueEx interface {
@ -350,14 +350,14 @@ func toBufferEx(v interface{}, inArray bool) (*bitio.Buffer, error) {
}
}
func toBufferView(v interface{}) (BufferView, error) {
func toBufferView(v interface{}) (BufferRange, error) {
switch vv := v.(type) {
case ToBufferView:
return vv.ToBufferView()
default:
bb, err := toBuffer(v)
if err != nil {
return BufferView{}, err
return BufferRange{}, err
}
return bufferViewFromBuffer(bb, 8), nil
}

View File

@ -148,8 +148,8 @@ func (dvb decodeValueBase) DisplayName() string {
}
func (dvb decodeValueBase) Display(w io.Writer, opts Options) error { return dump(dvb.dv, w, opts) }
func (dvb decodeValueBase) ToBufferView() (BufferView, error) {
return BufferView{bb: dvb.dv.RootBitBuf, r: dvb.dv.Range, unit: 8}, nil
func (dvb decodeValueBase) ToBufferView() (BufferRange, error) {
return BufferRange{bb: dvb.dv.RootBitBuf, r: dvb.dv.Range, unit: 8}, nil
}
func (dvb decodeValueBase) ExtKeys() []string {
kv := []string{
@ -217,13 +217,13 @@ func (dvb decodeValueBase) JQValueKey(name string) interface{} {
return dv.Err
case "_bits":
return BufferView{
return BufferRange{
bb: dv.RootBitBuf,
r: dv.Range,
unit: 1,
}
case "_bytes":
return BufferView{
return BufferRange{
bb: dv.RootBitBuf,
r: dv.Range,
unit: 8,