Skip to content

Commit

Permalink
fix cbor Neg
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Aug 19, 2024
1 parent 50fdacf commit c587f8f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
5 changes: 2 additions & 3 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"time"
"unsafe"

"golang.org/x/term"
"nikand.dev/go/hacked/hfmt"
"nikand.dev/go/hacked/htime"
"nikand.dev/go/hacked/low"
"golang.org/x/term"
"tlog.app/go/errors"
"tlog.app/go/loc"

Expand Down Expand Up @@ -165,7 +165,7 @@ func NewConsoleWriter(w io.Writer, f int) *ConsoleWriter {
MaxValPad: 24,

TimeFormat: "2006-01-02_15:04:05.000Z0700",
//DurationFormat: "%v",
// DurationFormat: "%v",
FloatChar: 'f',
FloatPrecision: 5,
CallerFormat: "%v",
Expand Down Expand Up @@ -664,7 +664,6 @@ func (w *ConsoleWriter) ConvertValue(b, p []byte, st, ff int) (_ []byte, i int)

base := 10
if tag == tlwire.Neg {
v--
b = append(b, '-')
}

Expand Down
2 changes: 1 addition & 1 deletion convert/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (w *JSON) ConvertValue(b, p []byte, st int) (_ []byte, i int) {
case tlwire.Int:
b = strconv.AppendUint(b, uint64(sub), 10)
case tlwire.Neg:
b = strconv.AppendInt(b, 1-sub, 10)
b = strconv.AppendInt(b, -sub-1, 10)
case tlwire.Bytes:
b = append(b, '"')

Expand Down
2 changes: 1 addition & 1 deletion convert/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"testing"
"time"

"nikand.dev/go/hacked/low"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"nikand.dev/go/hacked/low"

"tlog.app/go/tlog"
"tlog.app/go/tlog/tlwire"
Expand Down
4 changes: 2 additions & 2 deletions convert/logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"strconv"
"time"

"golang.org/x/term"
"nikand.dev/go/hacked/hfmt"
"nikand.dev/go/hacked/low"
"golang.org/x/term"
"tlog.app/go/loc"

"tlog.app/go/tlog"
Expand Down Expand Up @@ -209,7 +209,7 @@ func (w *Logfmt) ConvertValue(b, p, k []byte, st int) (_ []byte, i int) {
case tlwire.Int:
b = strconv.AppendUint(b, uint64(sub), 10)
case tlwire.Neg:
b = strconv.AppendInt(b, 1-sub, 10)
b = strconv.AppendInt(b, -sub-1, 10)
case tlwire.Bytes, tlwire.String:
var s []byte
s, i = w.d.Bytes(p, st)
Expand Down
7 changes: 6 additions & 1 deletion tlwire/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,13 @@ func (d LowDecoder) Signed(b []byte, st int) (v int64, i int) {
return int64(u), i
}

return 1 - int64(u), i
return int64(u) + 1, i
}

func (d LowDecoder) Unsigned(b []byte, st int) (v uint64, i int) {
i = st

tag := b[i] & TagMask
v = uint64(b[i]) & TagDetMask
i++

Expand All @@ -284,6 +285,10 @@ func (d LowDecoder) Unsigned(b []byte, st int) (v uint64, i int) {
panic("malformed message")
}

if tag == Neg {
v++
}

return
}

Expand Down
8 changes: 6 additions & 2 deletions tlwire/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (e LowEncoder) AppendTagBytes(b []byte, tag byte, s []byte) []byte {

func (e LowEncoder) AppendInt(b []byte, v int) []byte {
if v < 0 {
return e.AppendTag64(b, Neg, uint64(-v)+1)
return e.AppendTag64(b, Neg, uint64(-v)-1)
}

return e.AppendTag64(b, Int, uint64(v))
Expand All @@ -245,7 +245,7 @@ func (e LowEncoder) AppendUint(b []byte, v uint) []byte {

func (e LowEncoder) AppendInt64(b []byte, v int64) []byte {
if v < 0 {
return e.AppendTag64(b, Neg, uint64(-v)+1)
return e.AppendTag64(b, Neg, uint64(-v)-1)
}

return e.AppendTag64(b, Int, uint64(v))
Expand All @@ -255,6 +255,10 @@ func (e LowEncoder) AppendUint64(b []byte, v uint64) []byte {
return e.AppendTag64(b, Int, v)
}

func (e LowEncoder) AppendNegUint64(b []byte, v uint64) []byte {
return e.AppendTag64(b, Neg, v-1)
}

func (e LowEncoder) AppendFloat(b []byte, v float64) []byte {
if q := int8(v); float64(q) == v {
return append(b, Special|Float8, byte(q))
Expand Down

0 comments on commit c587f8f

Please sign in to comment.