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