|
| 1 | +// Package decimal implements methods to encode and decode BCD. |
| 2 | +// |
| 3 | +// BCD (Binary-Coded Decimal) is a sequence of bytes representing decimal |
| 4 | +// digits of the encoded number (each byte has two decimal digits each encoded |
| 5 | +// using 4-bit nibbles), so byte >> 4 is the first digit and byte & 0x0f is the |
| 6 | +// second digit. The leftmost digit in the array is the most significant. The |
| 7 | +// rightmost digit in the array is the least significant. |
| 8 | +// |
| 9 | +// The first byte of the BCD array contains the first digit of the number, |
| 10 | +// represented as follows: |
| 11 | +// |
| 12 | +// | 4 bits | 4 bits | |
| 13 | +// = 0x = the 1st digit |
| 14 | +// |
| 15 | +// (The first nibble contains 0 if the decimal number has an even number of |
| 16 | +// digits). The last byte of the BCD array contains the last digit of the |
| 17 | +// number and the final nibble, represented as follows: |
| 18 | +// |
| 19 | +// | 4 bits | 4 bits | |
| 20 | +// = the last digit = nibble |
| 21 | +// |
| 22 | +// The final nibble represents the number's sign: 0x0a, 0x0c, 0x0e, 0x0f stand |
| 23 | +// for plus, 0x0b and 0x0d stand for minus. |
| 24 | +// |
| 25 | +// Examples: |
| 26 | +// |
| 27 | +// The decimal -12.34 will be encoded as 0xd6, 0x01, 0x02, 0x01, 0x23, 0x4d: |
| 28 | +// |
| 29 | +// | MP_EXT (fixext 4) | MP_DECIMAL | scale | 1 | 2,3 | 4 (minus) | |
| 30 | +// | 0xd6 | 0x01 | 0x02 | 0x01 | 0x23 | 0x4d | |
| 31 | +// |
| 32 | +// The decimal 0.000000000000000000000000000000000010 will be encoded as |
| 33 | +// 0xc7, 0x03, 0x01, 0x24, 0x01, 0x0c: |
| 34 | +// |
| 35 | +// | MP_EXT (ext 8) | length | MP_DECIMAL | scale | 1 | 0 (plus) | |
| 36 | +// | 0xc7 | 0x03 | 0x01 | 0x24 | 0x01 | 0x0c | |
| 37 | +// |
| 38 | +// See also: |
| 39 | +// |
| 40 | +// * MessagePack extensions https://www.tarantool.io/en/doc/latest/dev_guide/internals/msgpack_extensions/ |
| 41 | +// |
| 42 | +// * An implementation in C language https://github.com/tarantool/decNumber/blob/master/decPacked.c |
| 43 | +package decimal |
| 44 | + |
| 45 | +import ( |
| 46 | + "fmt" |
| 47 | + "strings" |
| 48 | +) |
| 49 | + |
| 50 | +const ( |
| 51 | + bytePlus = byte(0x0c) |
| 52 | + byteMinus = byte(0x0d) |
| 53 | +) |
| 54 | + |
| 55 | +var isNegative = [256]bool{ |
| 56 | + 0x0a: false, |
| 57 | + 0x0b: true, |
| 58 | + 0x0c: false, |
| 59 | + 0x0d: true, |
| 60 | + 0x0e: false, |
| 61 | + 0x0f: false, |
| 62 | +} |
| 63 | + |
| 64 | +// Calculate a number of digits in a buffer with decimal number. |
| 65 | +// |
| 66 | +// Plus, minus, point and leading zeroes do not count. |
| 67 | +// Contains a quirk for a zero - returns 1. |
| 68 | +// |
| 69 | +// Examples (see more examples in tests): |
| 70 | +// |
| 71 | +// - 0.0000000000000001 - 1 digit |
| 72 | +// |
| 73 | +// - 00012.34 - 4 digits |
| 74 | +// |
| 75 | +// - 0.340 - 3 digits |
| 76 | +// |
| 77 | +// - 0 - 1 digit |
| 78 | +func getNumberLength(buf string) int { |
| 79 | + if len(buf) == 0 { |
| 80 | + return 0 |
| 81 | + } |
| 82 | + n := 0 |
| 83 | + for _, ch := range []byte(buf) { |
| 84 | + if ch >= '1' && ch <= '9' { |
| 85 | + n += 1 |
| 86 | + } else if ch == '0' && n != 0 { |
| 87 | + n += 1 |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Fix a case with a single 0. |
| 92 | + if n == 0 { |
| 93 | + n = 1 |
| 94 | + } |
| 95 | + |
| 96 | + return n |
| 97 | +} |
| 98 | + |
| 99 | +// encodeStringToBCD converts a string buffer to BCD Packed Decimal. |
| 100 | +// |
| 101 | +// The number is converted to a BCD packed decimal byte array, right aligned in |
| 102 | +// the BCD array, whose length is indicated by the second parameter. The final |
| 103 | +// 4-bit nibble in the array will be a sign nibble, 0x0c for "+" and 0x0d for |
| 104 | +// "-". Unused bytes and nibbles to the left of the number are set to 0. scale |
| 105 | +// is set to the scale of the number (this is the exponent, negated). |
| 106 | +func encodeStringToBCD(buf string) ([]byte, error) { |
| 107 | + if len(buf) == 0 { |
| 108 | + return nil, fmt.Errorf("Length of number is zero") |
| 109 | + } |
| 110 | + signByte := bytePlus // By default number is positive. |
| 111 | + if buf[0] == '-' { |
| 112 | + signByte = byteMinus |
| 113 | + } |
| 114 | + |
| 115 | + // The first nibble should contain 0, if the decimal number has an even |
| 116 | + // number of digits. Therefore highNibble is false when decimal number |
| 117 | + // is even. |
| 118 | + highNibble := true |
| 119 | + l := GetNumberLength(buf) |
| 120 | + if l%2 == 0 { |
| 121 | + highNibble = false |
| 122 | + } |
| 123 | + scale := 0 // By default decimal number is integer. |
| 124 | + var byteBuf []byte |
| 125 | + for i, ch := range []byte(buf) { |
| 126 | + // Skip leading zeroes. |
| 127 | + if (len(byteBuf) == 0) && ch == '0' { |
| 128 | + continue |
| 129 | + } |
| 130 | + if (i == 0) && (ch == '-' || ch == '+') { |
| 131 | + continue |
| 132 | + } |
| 133 | + // Calculate a number of digits after the decimal point. |
| 134 | + if ch == '.' { |
| 135 | + if scale != 0 { |
| 136 | + return nil, fmt.Errorf("Number contains more than one point") |
| 137 | + } |
| 138 | + scale = len(buf) - i - 1 |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + if ch < '0' || ch > '9' { |
| 143 | + return nil, fmt.Errorf("Failed to convert symbol '%c' to a digit", ch) |
| 144 | + } |
| 145 | + digit := byte(ch - '0') |
| 146 | + if highNibble { |
| 147 | + // Add a digit to a high nibble. |
| 148 | + digit = digit << 4 |
| 149 | + byteBuf = append(byteBuf, digit) |
| 150 | + highNibble = false |
| 151 | + } else { |
| 152 | + if len(byteBuf) == 0 { |
| 153 | + byteBuf = make([]byte, 1) |
| 154 | + } |
| 155 | + // Add a digit to a low nibble. |
| 156 | + lowByteIdx := len(byteBuf) - 1 |
| 157 | + byteBuf[lowByteIdx] = byteBuf[lowByteIdx] | digit |
| 158 | + highNibble = true |
| 159 | + } |
| 160 | + } |
| 161 | + if len(byteBuf) == 0 { |
| 162 | + // a special case: -0 |
| 163 | + signByte = bytePlus |
| 164 | + } |
| 165 | + if highNibble { |
| 166 | + // Put a sign to a high nibble. |
| 167 | + byteBuf = append(byteBuf, signByte) |
| 168 | + } else { |
| 169 | + // Put a sign to a low nibble. |
| 170 | + lowByteIdx := len(byteBuf) - 1 |
| 171 | + byteBuf[lowByteIdx] = byteBuf[lowByteIdx] | signByte |
| 172 | + } |
| 173 | + byteBuf = append([]byte{byte(scale)}, byteBuf...) |
| 174 | + |
| 175 | + return byteBuf, nil |
| 176 | +} |
| 177 | + |
| 178 | +// decodeStringFromBCD converts a BCD Packed Decimal to a string buffer. |
| 179 | +// |
| 180 | +// The BCD packed decimal byte array, together with an associated scale, is |
| 181 | +// converted to a string. The BCD array is assumed full of digits, and must be |
| 182 | +// ended by a 4-bit sign nibble in the least significant four bits of the final |
| 183 | +// byte. The scale is used (negated) as the exponent of the decimal number. |
| 184 | +// Note that zeroes may have a sign and/or a scale. |
| 185 | +func decodeStringFromBCD(bcdBuf []byte) (string, error) { |
| 186 | + // Index of a byte with scale. |
| 187 | + const scaleIdx = 0 |
| 188 | + scale := int(bcdBuf[scaleIdx]) |
| 189 | + |
| 190 | + // Get a BCD buffer without scale. |
| 191 | + bcdBuf = bcdBuf[scaleIdx+1:] |
| 192 | + bufLen := len(bcdBuf) |
| 193 | + |
| 194 | + // Every nibble contains a digit, and the last low nibble contains a |
| 195 | + // sign. |
| 196 | + ndigits := bufLen*2 - 1 |
| 197 | + |
| 198 | + // The first nibble contains 0 if the decimal number has an even number of |
| 199 | + // digits. Decrease a number of digits if so. |
| 200 | + if bcdBuf[0]&0xf0 == 0 { |
| 201 | + ndigits -= 1 |
| 202 | + } |
| 203 | + |
| 204 | + // Reserve bytes for dot and sign. |
| 205 | + numLen := ndigits + 2 |
| 206 | + // Reserve bytes for zeroes. |
| 207 | + if scale >= ndigits { |
| 208 | + numLen += scale - ndigits |
| 209 | + } |
| 210 | + |
| 211 | + var bld strings.Builder |
| 212 | + bld.Grow(numLen) |
| 213 | + |
| 214 | + // Add a sign, it is encoded in a low nibble of a last byte. |
| 215 | + lastByte := bcdBuf[bufLen-1] |
| 216 | + sign := lastByte & 0x0f |
| 217 | + if isNegative[sign] { |
| 218 | + bld.WriteByte('-') |
| 219 | + } |
| 220 | + |
| 221 | + // Add missing zeroes to the left side when scale is bigger than a |
| 222 | + // number of digits and a single missed zero to the right side when |
| 223 | + // equal. |
| 224 | + if scale > ndigits { |
| 225 | + bld.WriteByte('0') |
| 226 | + bld.WriteByte('.') |
| 227 | + for diff := scale - ndigits; diff > 0; diff-- { |
| 228 | + bld.WriteByte('0') |
| 229 | + } |
| 230 | + } else if scale == ndigits { |
| 231 | + bld.WriteByte('0') |
| 232 | + } |
| 233 | + |
| 234 | + const MaxDigit = 0x09 |
| 235 | + // Builds a buffer with symbols of decimal number (digits, dot and sign). |
| 236 | + processNibble := func(nibble byte) { |
| 237 | + if nibble <= MaxDigit { |
| 238 | + if ndigits == scale { |
| 239 | + bld.WriteByte('.') |
| 240 | + } |
| 241 | + bld.WriteByte(nibble + '0') |
| 242 | + ndigits-- |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + for i, bcdByte := range bcdBuf { |
| 247 | + highNibble := bcdByte >> 4 |
| 248 | + lowNibble := bcdByte & 0x0f |
| 249 | + // Skip a first high nibble as no digit there. |
| 250 | + if i != 0 || highNibble != 0 { |
| 251 | + processNibble(highNibble) |
| 252 | + } |
| 253 | + processNibble(lowNibble) |
| 254 | + } |
| 255 | + |
| 256 | + return bld.String(), nil |
| 257 | +} |
0 commit comments