Decompose returns the internal decimal state into parts. If the provided buf has sufficient capacity, buf may be returned as the coefficient with the value set and length set as appropriate. Note that it does not act like Append-like functions and does not fill necessarily from the beginning of the
(buf []byte)
| 56 | // Append-like functions and does not fill necessarily from the beginning of the |
| 57 | // buffer. |
| 58 | func (d *Decimal) Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32) { |
| 59 | switch d.Form { |
| 60 | default: |
| 61 | panic(fmt.Errorf("unknown Form: %v", d.Form)) |
| 62 | case Finite: |
| 63 | // Nothing, continue on. |
| 64 | case Infinite: |
| 65 | negative = d.Negative |
| 66 | form = 1 |
| 67 | return |
| 68 | case NaNSignaling, NaN: |
| 69 | negative = d.Negative |
| 70 | form = 2 |
| 71 | return |
| 72 | } |
| 73 | // Finite form. |
| 74 | negative = d.Negative |
| 75 | exponent = d.Exponent |
| 76 | |
| 77 | sizeInBytes := (d.Coeff.BitLen() + 8 - 1) / 8 // math.Ceil(d.Coeff.BitLen()/8.0) |
| 78 | if cap(buf) >= sizeInBytes { |
| 79 | // It extends the buffer as the filling of bytes expects an already |
| 80 | // allocated slice. |
| 81 | buf = buf[:sizeInBytes] |
| 82 | |
| 83 | // We can fit the coefficient in the given buffer which prevents an |
| 84 | // allocation. |
| 85 | coefficient = d.Coeff.FillBytes(buf) |
| 86 | } else { |
| 87 | coefficient = d.Coeff.Bytes() |
| 88 | } |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | // Compose sets the internal decimal value from parts. If the value cannot be |
| 93 | // represented then an error should be returned. |