2021-12-02 00:48:25 +03:00
|
|
|
package scalar
|
|
|
|
|
|
|
|
// TODO: d.Pos check err
|
|
|
|
// TODO: fn for actual?
|
|
|
|
// TODO: dsl import .? own scalar package?
|
|
|
|
// TODO: better IOError op names
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-01-28 19:18:30 +03:00
|
|
|
"strconv"
|
2021-12-02 00:48:25 +03:00
|
|
|
"strings"
|
2022-05-07 14:13:37 +03:00
|
|
|
"time"
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/bitioex"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate sh -c "cat scalar_gen.go.tmpl | go run ../../dev/tmpl.go ../decode/types.json | gofmt > scalar_gen.go"
|
|
|
|
|
|
|
|
type DisplayFormat int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NumberDecimal DisplayFormat = iota
|
|
|
|
NumberBinary
|
|
|
|
NumberOctal
|
|
|
|
NumberHex
|
|
|
|
)
|
|
|
|
|
|
|
|
func (df DisplayFormat) FormatBase() int {
|
|
|
|
switch df {
|
|
|
|
case NumberDecimal:
|
|
|
|
return 10
|
|
|
|
case NumberBinary:
|
|
|
|
return 2
|
|
|
|
case NumberOctal:
|
|
|
|
return 8
|
|
|
|
case NumberHex:
|
|
|
|
return 16
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
// TODO: todos
|
|
|
|
// rename raw?
|
|
|
|
// crc
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// d.FieldU2("emphasis").
|
|
|
|
// MapSym().
|
|
|
|
// Actual
|
|
|
|
// Sym
|
|
|
|
// Value()
|
|
|
|
// Scalar()
|
|
|
|
|
|
|
|
var UintBin = UintFn(func(s Uint) (Uint, error) { s.DisplayFormat = NumberBinary; return s, nil })
|
|
|
|
var UintOct = UintFn(func(s Uint) (Uint, error) { s.DisplayFormat = NumberOctal; return s, nil })
|
|
|
|
var UintDec = UintFn(func(s Uint) (Uint, error) { s.DisplayFormat = NumberDecimal; return s, nil })
|
|
|
|
var UintHex = UintFn(func(s Uint) (Uint, error) { s.DisplayFormat = NumberHex; return s, nil })
|
|
|
|
var SintBin = SintFn(func(s Sint) (Sint, error) { s.DisplayFormat = NumberBinary; return s, nil })
|
|
|
|
var SintOct = SintFn(func(s Sint) (Sint, error) { s.DisplayFormat = NumberOctal; return s, nil })
|
|
|
|
var SintDec = SintFn(func(s Sint) (Sint, error) { s.DisplayFormat = NumberDecimal; return s, nil })
|
|
|
|
var SintHex = SintFn(func(s Sint) (Sint, error) { s.DisplayFormat = NumberHex; return s, nil })
|
|
|
|
var BigIntBin = BigIntFn(func(s BigInt) (BigInt, error) { s.DisplayFormat = NumberBinary; return s, nil })
|
|
|
|
var BigIntOct = BigIntFn(func(s BigInt) (BigInt, error) { s.DisplayFormat = NumberOctal; return s, nil })
|
|
|
|
var BigIntDec = BigIntFn(func(s BigInt) (BigInt, error) { s.DisplayFormat = NumberDecimal; return s, nil })
|
|
|
|
var BigIntHex = BigIntFn(func(s BigInt) (BigInt, error) { s.DisplayFormat = NumberHex; return s, nil })
|
|
|
|
|
|
|
|
func UintActualAdd(n int) UintActualFn {
|
2022-05-07 13:46:34 +03:00
|
|
|
// TODO: use math.Add/Sub?
|
2022-09-30 14:58:23 +03:00
|
|
|
return UintActualFn(func(a uint64) uint64 { return uint64(int64(a) + int64(n)) })
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func SintActualAdd(n int) SintActualFn {
|
|
|
|
return SintActualFn(func(a int64) int64 { return a + int64(n) })
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func StrActualTrim(cutset string) StrActualFn {
|
|
|
|
return StrActualFn(func(a string) string { return strings.Trim(a, cutset) })
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
var ActualTrimSpace = StrActualFn(strings.TrimSpace)
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func strMapToSym(fn func(s string) (any, error), try bool) StrMapper {
|
|
|
|
return StrFn(func(s Str) (Str, error) {
|
|
|
|
ts := strings.TrimSpace(s.Actual)
|
2022-08-04 13:21:36 +03:00
|
|
|
n, err := fn(ts)
|
|
|
|
if err != nil {
|
2022-08-15 14:05:09 +03:00
|
|
|
if try {
|
|
|
|
return s, nil
|
|
|
|
}
|
2022-08-04 13:21:36 +03:00
|
|
|
return s, err
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
2022-08-04 13:21:36 +03:00
|
|
|
s.Sym = n
|
2022-01-28 19:18:30 +03:00
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func TryStrSymParseUint(base int) StrMapper {
|
2022-08-15 14:05:09 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseUint(s, base, 64) }, true)
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func TryStrSymParseInt(base int) StrMapper {
|
2022-08-15 14:05:09 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseInt(s, base, 64) }, true)
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func TryStrSymParseFloat(base int) StrMapper {
|
2022-08-15 14:05:09 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseFloat(s, base) }, true)
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func StrSymParseUint(base int) StrMapper {
|
2022-08-15 14:05:09 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseUint(s, base, 64) }, false)
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func StrSymParseInt(base int) StrMapper {
|
2022-08-15 14:05:09 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseInt(s, base, 64) }, false)
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func StrSymParseFloat(base int) StrMapper {
|
2022-08-15 14:05:09 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseFloat(s, base) }, false)
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:33:17 +03:00
|
|
|
type URangeEntry struct {
|
|
|
|
Range [2]uint64
|
2022-09-30 14:58:23 +03:00
|
|
|
S Uint
|
2021-12-06 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
// UintRangeToScalar maps uint64 ranges to a scalar, first in range is chosen
|
|
|
|
type UintRangeToScalar []URangeEntry
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func (rs UintRangeToScalar) MapUint(s Uint) (Uint, error) {
|
|
|
|
n := s.Actual
|
2021-12-06 21:33:17 +03:00
|
|
|
for _, re := range rs {
|
|
|
|
if n >= re.Range[0] && n <= re.Range[1] {
|
|
|
|
ns := re.S
|
2021-12-02 00:48:25 +03:00
|
|
|
ns.Actual = s.Actual
|
|
|
|
s = ns
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:33:17 +03:00
|
|
|
// SRangeToScalar maps ranges to a scalar, first in range is chosen
|
|
|
|
type SRangeEntry struct {
|
|
|
|
Range [2]int64
|
2022-09-30 14:58:23 +03:00
|
|
|
S Sint
|
2021-12-06 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SRangeToScalar maps sint64 ranges to a scalar, first in range is chosen
|
|
|
|
type SRangeToScalar []SRangeEntry
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func (rs SRangeToScalar) MapSint(s Sint) (Sint, error) {
|
|
|
|
n := s.Actual
|
2021-12-06 21:33:17 +03:00
|
|
|
for _, re := range rs {
|
|
|
|
if n >= re.Range[0] && n <= re.Range[1] {
|
|
|
|
ns := re.S
|
2021-12-02 00:48:25 +03:00
|
|
|
ns.Actual = s.Actual
|
|
|
|
s = ns
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func RawSym(s BitBuf, nBytes int, fn func(b []byte) string) (BitBuf, error) {
|
|
|
|
br := s.Actual
|
2022-08-12 16:27:51 +03:00
|
|
|
brLen, err := bitioex.Len(br)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
2022-09-30 14:58:23 +03:00
|
|
|
return BitBuf{}, err
|
2022-01-24 23:21:48 +03:00
|
|
|
}
|
|
|
|
if nBytes < 0 {
|
|
|
|
nBytes = int(brLen) / 8
|
|
|
|
if brLen%8 != 0 {
|
|
|
|
nBytes++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if brLen < int64(nBytes)*8 {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
// TODO: shared somehow?
|
|
|
|
b := make([]byte, nBytes)
|
|
|
|
if _, err := br.ReadBitsAt(b, int64(nBytes)*8, 0); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Sym = fn(b[0:nBytes])
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
var RawUUID = BitBufFn(func(s BitBuf) (BitBuf, error) {
|
2022-04-17 11:33:01 +03:00
|
|
|
return RawSym(s, -1, func(b []byte) string {
|
2022-01-24 23:21:48 +03:00
|
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
var RawHex = BitBufFn(func(s BitBuf) (BitBuf, error) {
|
2022-04-17 11:33:01 +03:00
|
|
|
return RawSym(s, -1, func(b []byte) string { return fmt.Sprintf("%x", b) })
|
2022-01-24 23:21:48 +03:00
|
|
|
})
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
type RawBytesMap []struct {
|
2021-12-02 00:48:25 +03:00
|
|
|
Bytes []byte
|
2022-09-30 14:58:23 +03:00
|
|
|
Scalar BitBuf
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func (m RawBytesMap) MapBitBuf(s BitBuf) (BitBuf, error) {
|
|
|
|
rc, err := bitio.CloneReader(s.Actual)
|
2022-05-28 14:04:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
bb := &bytes.Buffer{}
|
2022-08-12 16:27:51 +03:00
|
|
|
if _, err := bitioex.CopyBits(bb, rc); err != nil {
|
2021-12-02 00:48:25 +03:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
for _, bs := range m {
|
2022-01-24 23:21:48 +03:00
|
|
|
if bytes.Equal(bb.Bytes(), bs.Bytes) {
|
2021-12-02 00:48:25 +03:00
|
|
|
ns := bs.Scalar
|
|
|
|
ns.Actual = s.Actual
|
|
|
|
s = ns
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2022-05-07 14:13:37 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
var unixTimeEpochDate = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
|
2022-05-07 14:13:37 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func UintActualDate(epoch time.Time, format string) UintFn {
|
|
|
|
return UintFn(func(s Uint) (Uint, error) {
|
|
|
|
s.Description = epoch.Add(time.Duration(s.Actual) * time.Second).Format(format)
|
|
|
|
return s, nil
|
2022-05-07 14:13:37 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func UintActualUnixTime(format string) UintFn {
|
|
|
|
return UintActualDate(unixTimeEpochDate, format)
|
2022-05-07 14:13:37 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func FltActualDate(epoch time.Time, format string) FltFn {
|
|
|
|
return FltFn(func(s Flt) (Flt, error) {
|
|
|
|
s.Description = epoch.Add(time.Duration(s.Actual) * time.Second).Format(format)
|
|
|
|
return s, nil
|
2022-09-17 20:00:38 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
func FltActualUnixTime(format string) FltFn {
|
|
|
|
return FltActualDate(unixTimeEpochDate, format)
|
2022-10-04 17:59:50 +03:00
|
|
|
}
|