1
1
mirror of https://github.com/wader/fq.git synced 2024-12-01 02:30:32 +03:00
fq/pkg/interp/preview.go
Mattias Wadman 7c5215347d bitio,decode: Refactor bitio usage and make buffer slicing more correct
Remove bitio.Buffer layer. bitio.Buffer was a kitchen sink layer with helpers
now it's just a buffer and most functions have been moved to decode instead.

bitio package now only have primitive types and functions simialar to standard
library io and bytes packages.

Make nearly eveything internally use bitio.Bit* interfaces so that slicing work
correctly this will also make it possible to start experimenting with more
complicated silcing helpers, ex things like:
breplace(.header.bitrate; 123) to get a new buffer with bitrate changed.
2022-02-04 21:41:53 +01:00

46 lines
1.0 KiB
Go

package interp
import (
"fmt"
"math/big"
"strconv"
"github.com/wader/fq/internal/mathextra"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/scalar"
)
func previewValue(v interface{}, df scalar.DisplayFormat) string {
switch vv := v.(type) {
case bool:
if vv {
return "true"
}
return "false"
case int:
// TODO: DisplayFormat is weird
return mathextra.PadFormatInt(int64(vv), df.FormatBase(), true, 0)
case int64:
// TODO: DisplayFormat is weird
return mathextra.PadFormatInt(vv, df.FormatBase(), true, 0)
case uint64:
return mathextra.PadFormatUint(vv, df.FormatBase(), true, 0)
case float64:
// TODO: float32? better truncated to significant digits?
return strconv.FormatFloat(vv, 'g', -1, 64)
case string:
if len(vv) > 50 {
return fmt.Sprintf("%q", vv[0:50]) + "..."
}
return fmt.Sprintf("%q", vv)
case nil:
return "null"
case bitio.Reader:
return "raw bits"
case *big.Int:
return mathextra.PadFormatBigInt(vv, df.FormatBase(), true, 0)
default:
panic("unreachable")
}
}