MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / EncodeNonsortingDecimal

Function EncodeNonsortingDecimal

pkg/util/encoding/decimal.go:525–590  ·  view source on GitHub ↗

EncodeNonsortingDecimal returns the resulting byte slice with the encoded decimal appended to b. The encoding is limited compared to standard encodings in this package in that - It will not sort lexicographically - It does not encode its length or terminate itself, so decoding functions must be prov

(b []byte, d *apd.Decimal)

Source from the content-addressed store, hash-verified

523// decimalInfinity -> decimalInfinity
524// decimalNaNDesc -> decimalNaNDesc
525func EncodeNonsortingDecimal(b []byte, d *apd.Decimal) []byte {
526 neg := d.Negative
527 switch d.Form {
528 case apd.Finite:
529 // ignore
530 case apd.Infinite:
531 if neg {
532 return append(b, decimalNegativeInfinity)
533 }
534 return append(b, decimalInfinity)
535 case apd.NaN:
536 return append(b, decimalNaN)
537 default:
538 panic(errors.Errorf("unknown form: %s", d.Form))
539 }
540
541 // We only encode "0" as decimalZero. All others ("0.0", "-0", etc) are
542 // encoded like normal values.
543 if d.IsZero() && !neg && d.Exponent == 0 {
544 return append(b, decimalZero)
545 }
546
547 // Determine the exponent of the decimal, with the
548 // exponent defined as .xyz * 10^exp.
549 nDigits := int(d.NumDigits())
550 e := nDigits + int(d.Exponent)
551
552 bNat := d.Coeff.Bits()
553
554 var buf []byte
555 if n := UpperBoundNonsortingDecimalSize(d); n <= cap(b)-len(b) {
556 // We append the marker directly to the input buffer b below, so
557 // we are off by 1 for each of these, which explains the adjustments.
558 buf = b[len(b)+1 : len(b)+1]
559 } else {
560 buf = make([]byte, 0, n-1)
561 }
562
563 switch {
564 case neg && e > 0:
565 b = append(b, decimalNegLarge)
566 buf = encodeNonsortingDecimalValue(uint64(e), bNat, buf)
567 return append(b, buf...)
568 case neg && e == 0:
569 b = append(b, decimalNegMedium)
570 buf = encodeNonsortingDecimalValueWithoutExp(bNat, buf)
571 return append(b, buf...)
572 case neg && e < 0:
573 b = append(b, decimalNegSmall)
574 buf = encodeNonsortingDecimalValue(uint64(-e), bNat, buf)
575 return append(b, buf...)
576 case !neg && e < 0:
577 b = append(b, decimalPosSmall)
578 buf = encodeNonsortingDecimalValue(uint64(-e), bNat, buf)
579 return append(b, buf...)
580 case !neg && e == 0:
581 b = append(b, decimalPosMedium)
582 buf = encodeNonsortingDecimalValueWithoutExp(bNat, buf)

Callers 1

Calls 4

ErrorfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…