|
| 1 | +// Package implements methods to encode and decode BCD. |
| 2 | +// BCD is a sequence of bytes representing decimal digits of the encoded number |
| 3 | +// (each byte has two decimal digits each encoded using 4-bit nibbles), |
| 4 | +// so byte >> 4 is the first digit and byte & 0x0f is the second digit. The |
| 5 | +// leftmost digit in the array is the most significant. The rightmost digit in |
| 6 | +// the array is the least significant. |
| 7 | +// The first byte of the BCD array contains the first digit of the number, |
| 8 | +// represented as follows: |
| 9 | +// |
| 10 | +// | 4 bits | 4 bits | |
| 11 | +// = 0x = the 1st digit |
| 12 | +// |
| 13 | +// (The first nibble contains 0 if the decimal number has an even number of |
| 14 | +// digits.) The last byte of the BCD array contains the last digit of the |
| 15 | +// number and the final nibble, represented as follows: |
| 16 | +// |
| 17 | +// | 4 bits | 4 bits | |
| 18 | +// = the last digit = nibble |
| 19 | +// |
| 20 | +// TODO: BitReader https://go.dev/play/p/Wyr_K9YAro |
| 21 | +package decimal |
| 22 | + |
| 23 | +import ( |
| 24 | + "errors" |
| 25 | + _ "fmt" |
| 26 | + "strconv" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + ErrNumberIsNotADecimal = errors.New("Number is not a decimal.") |
| 31 | + ErrWrongExponentaRange = errors.New("Exponenta has a wrong range.") |
| 32 | +) |
| 33 | + |
| 34 | +const decimalMaxDigits = 38 // Maximum decimal digits taken by a decimal representation. |
| 35 | +const mpScaleIdx = 0 |
| 36 | + |
| 37 | +var mpDecimalSign = map[rune]byte{ |
| 38 | + '+': 0x0a, |
| 39 | + '-': 0x0b, |
| 40 | +} |
| 41 | + |
| 42 | +var mpIsDecimalNegative = map[byte]bool{ |
| 43 | + 0x0a: false, |
| 44 | + 0x0b: true, |
| 45 | + 0x0c: false, |
| 46 | + 0x0d: true, |
| 47 | + 0x0e: false, |
| 48 | + 0x0f: false, |
| 49 | +} |
| 50 | + |
| 51 | +var hex_digit = map[rune]byte{ |
| 52 | + '1': 0x1, |
| 53 | + '2': 0x2, |
| 54 | + '3': 0x3, |
| 55 | + '4': 0x4, |
| 56 | + '5': 0x5, |
| 57 | + '6': 0x6, |
| 58 | + '7': 0x7, |
| 59 | + '8': 0x8, |
| 60 | + '9': 0x9, |
| 61 | + '0': 0x0, |
| 62 | +} |
| 63 | + |
| 64 | +func MPEncodeNumberToBCD(buf string) []byte { |
| 65 | + scale := 0 |
| 66 | + sign := '+' |
| 67 | + // TODO: The first nibble contains 0 if the decimal number has an even number of digits. |
| 68 | + nibbleIdx := 2 /* First nibble is for sign */ |
| 69 | + byteBuf := make([]byte, 1) |
| 70 | + for i, ch := range buf { |
| 71 | + // TODO: ignore leading zeroes |
| 72 | + // Check for sign in a first nibble. |
| 73 | + if (i == 0) && (ch == '-' || ch == '+') { |
| 74 | + sign = ch |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + // Remember a number of digits after the decimal point. |
| 79 | + if ch == '.' { |
| 80 | + scale = len(buf) - i - 1 |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + //digit := byte(ch) // TODO |
| 85 | + digit := hex_digit[ch] |
| 86 | + highNibble := nibbleIdx%2 != 0 |
| 87 | + lowByte := len(byteBuf) - 1 |
| 88 | + if highNibble { |
| 89 | + digit = digit << 4 |
| 90 | + byteBuf = append(byteBuf, digit) |
| 91 | + } else { |
| 92 | + if nibbleIdx == 2 { |
| 93 | + byteBuf[0] = digit |
| 94 | + } else { |
| 95 | + byteBuf[lowByte] = byteBuf[lowByte] | digit |
| 96 | + } |
| 97 | + } |
| 98 | + //fmt.Printf("DEBUG: %x\n", byteBuf) |
| 99 | + nibbleIdx += 1 |
| 100 | + } |
| 101 | + if nibbleIdx%2 != 0 { |
| 102 | + byteBuf = append(byteBuf, mpDecimalSign[sign]) |
| 103 | + } else { |
| 104 | + lowByte := len(byteBuf) - 1 |
| 105 | + byteBuf[lowByte] = byteBuf[lowByte] | mpDecimalSign[sign] |
| 106 | + } |
| 107 | + byteBuf = append([]byte{byte(scale)}, byteBuf...) |
| 108 | + //fmt.Printf("DEBUG: Encoded byteBuf %x\n", byteBuf) |
| 109 | + |
| 110 | + return byteBuf |
| 111 | +} |
| 112 | + |
| 113 | +func highNibble(b byte) byte { |
| 114 | + return b >> 4 |
| 115 | +} |
| 116 | + |
| 117 | +func lowNibble(b byte) byte { |
| 118 | + return b & 0x0f |
| 119 | +} |
| 120 | + |
| 121 | +func MPDecodeNumberFromBCD(bcdBuf []byte) ([]string, error) { |
| 122 | + // TODO: move scale processing to outside of BCD package, it is not a |
| 123 | + // part of BCD. Or rename package to PackedDecimal. |
| 124 | + scale := int32(bcdBuf[mpScaleIdx]) |
| 125 | + //fmt.Printf("DEBUG: SCALE %d\n", scale) |
| 126 | + // decimal scale is equal to -exponent and the exponent must be in |
| 127 | + // range [ -decimalMaxDigits; decimalMaxDigits ) |
| 128 | + if scale < -decimalMaxDigits || scale >= decimalMaxDigits { |
| 129 | + return nil, ErrWrongExponentaRange |
| 130 | + } |
| 131 | + |
| 132 | + // BCD buffer without a byte with scale. |
| 133 | + bcdBuf = bcdBuf[mpScaleIdx+1:] |
| 134 | + //fmt.Printf("DEBUG: BCDbuf without byte with scale %x\n", bcdBuf) |
| 135 | + length := len(bcdBuf) |
| 136 | + var digits []string |
| 137 | + for i, bcd_byte := range bcdBuf { |
| 138 | + // Skip leading zeros. |
| 139 | + if len(digits) == 0 && int(bcd_byte) == 0 { |
| 140 | + continue |
| 141 | + } |
| 142 | + if high := highNibble(bcd_byte); high != 0 { |
| 143 | + digit := strconv.Itoa(int(high)) |
| 144 | + digits = append(digits, digit) |
| 145 | + } |
| 146 | + low := lowNibble(bcd_byte) |
| 147 | + if int(i) != length-1 { |
| 148 | + digit := strconv.Itoa(int(low)) |
| 149 | + digits = append(digits, digit) |
| 150 | + } |
| 151 | + /* TODO: Make sure every digit is less than 9 and bigger than 0 */ |
| 152 | + } |
| 153 | + |
| 154 | + digits = append(digits[:scale+1], digits[scale:]...) |
| 155 | + digits[scale] = "." |
| 156 | + last_byte := bcdBuf[length-1] |
| 157 | + sign := lowNibble(last_byte) |
| 158 | + if mpIsDecimalNegative[sign] { |
| 159 | + digits = append([]string{"-"}, digits...) |
| 160 | + } |
| 161 | + |
| 162 | + return digits, nil |
| 163 | +} |
0 commit comments