Skip to content

Commit 7013bd5

Browse files
committed
tmp
1 parent 57f256f commit 7013bd5

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

decimal/bcd.go

+38-22
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// = 0x = the 1st digit
1414
//
1515
// (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
16+
// digits). The last byte of the BCD array contains the last digit of the
1717
// number and the final nibble, represented as follows:
1818
//
1919
// | 4 bits | 4 bits |
@@ -31,7 +31,8 @@ import (
3131
"errors"
3232
"fmt"
3333
"strconv"
34-
_ "strings"
34+
"strings"
35+
"unicode"
3536
)
3637

3738
var (
@@ -77,56 +78,71 @@ func lowNibble(b byte) byte {
7778
return b & 0x0f
7879
}
7980

80-
// TODO: The first nibble contains 0 if the decimal number has an even number of digits.
8181
func MPEncodeStringToBCD(buf string) ([]byte, error) {
8282
sign := '+' // By default number is positive.
83-
/*
84-
if res := strings.HasPrefix(buf, "-"); res == true {
85-
sign = '-'
86-
}
87-
*/
88-
scale := 0 // By default number is integer.
89-
nibbleIdx := 2 // Number of a current nibble in a byte buffer.
83+
84+
c := len(buf)
85+
if res := strings.HasPrefix(buf, "-"); res == true {
86+
sign = '-'
87+
c = -1
88+
}
89+
if res := strings.HasPrefix(buf, "+"); res == true {
90+
c = -1
91+
}
92+
if res := strings.Contains(buf, "."); res == true {
93+
c = -1
94+
}
95+
96+
nibbleIdx := 0 // Number of a current nibble in a byte buffer.
97+
// The first nibble should contain 0, if the decimal number has
98+
// an even number of digits.
99+
if c%2 == 0 {
100+
nibbleIdx = 1
101+
}
102+
103+
scale := 0 // By default number is integer.
90104
byteBuf := make([]byte, 1)
91105
for i, ch := range buf {
92106
fmt.Printf("DEBUG: ch = %c\n", ch)
93-
// Check for sign in a first symbol.
94107
if (i == 0) && (ch == '-' || ch == '+') {
95-
sign = ch
96-
fmt.Printf("DEBUG: Sign is %c\n", sign)
97108
continue
98109
}
99-
100110
// Calculate a number of digits after the decimal point.
101111
if ch == '.' {
102112
scale = len(buf) - i - 1
103113
fmt.Printf("DEBUG: Scale is %d\n", scale)
104114
continue
105115
}
116+
if !unicode.IsDigit(ch) {
117+
return nil, fmt.Errorf("Symbol in position %d is not a digit: %c", i, ch)
118+
}
106119

107120
digit := hexDigit[ch]
108-
lowByte := len(byteBuf) - 1
109121
if nibbleIdx%2 != 0 {
122+
// High nibble.
110123
digit = digit << 4
111124
byteBuf = append(byteBuf, digit)
112125
} else {
113-
if nibbleIdx == 2 {
114-
byteBuf[0] = digit
115-
} else {
116-
byteBuf[lowByte] = byteBuf[lowByte] | digit
117-
}
126+
// Low nibble.
127+
lowByte := len(byteBuf) - 1
128+
fmt.Printf("DEBUG: lowByte %x\n", lowByte)
129+
byteBuf[lowByte] = byteBuf[lowByte] | digit
118130
}
119131
fmt.Printf("DEBUG: idx == %d, buf %x\n", i, byteBuf)
120132
nibbleIdx += 1
121133
}
122-
fmt.Printf("DEBUG: final buf %x\n", byteBuf)
134+
fmt.Printf("DEBUG: Final buf %x\n", byteBuf)
123135
if nibbleIdx%2 != 0 {
124136
byteBuf = append(byteBuf, mpDecimalSign[sign])
137+
fmt.Printf("DEBUG: mpDecimalSign %x\n", mpDecimalSign[sign])
125138
} else {
126139
lowByte := len(byteBuf) - 1
140+
fmt.Printf("DEBUG: lowByte == %x\n", lowByte)
127141
byteBuf[lowByte] = byteBuf[lowByte] | mpDecimalSign[sign]
128142
}
129-
byteBuf = append([]byte{byte(scale)}, byteBuf...)
143+
if byteBuf[0] != 0 {
144+
byteBuf = append([]byte{byte(scale)}, byteBuf...)
145+
}
130146
fmt.Printf("DEBUG: Encoded byteBuf %x\n", byteBuf)
131147

132148
return byteBuf, nil

decimal/decimal.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func encodeDecimal(e *msgpack.Encoder, v reflect.Value) error {
4141
}
4242

4343
func decodeDecimal(d *msgpack.Decoder, v reflect.Value) error {
44-
var bytesCount int = 4 // FIXME
44+
var bytesCount int = 6 // FIXME
4545
b := make([]byte, bytesCount)
4646

4747
fmt.Printf("DEBUG: decodeDecimal buffer %x\n", b)

decimal/decimal_test.go

+39-37
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,30 @@ var decimalBCD = []struct {
143143
bcdBuf []byte
144144
numString string
145145
}{
146-
{[]byte{0x02, 0x01, 0x23, 0x4b}, "-12.34"},
147-
//{[]byte{0x0, 0xa}, "0"}, // WRONG 00000a
148-
//{[]byte{0x0, 0xb}, "-0"}, // WRONG 00000b
149-
{[]byte{0x0, 0x1a}, "1"}, // WRONG 00010a
150-
//{[]byte{0x0, 0x1b}, "-1"}, // WRONG 00010b
151-
//{[]byte{0x1, 0x1a}, "0.1"}, // WRONG 00010a
152-
//{[]byte{0x1, 0x1b}, "-0.1"}, // WRONG 00010b
153-
//{[]byte{0x25, 0x1a}, "0.0000000000000000000000000000000000001"}, // WRONG 25000000000000000000000000000000000000001a
154-
//{[]byte{0x25, 0x1b}, "-0.0000000000000000000000000000000000001"}, // WRONG 25000000000000000000000000000000000000001b
155-
//{[]byte{0x26, 0x1a}, "0.00000000000000000000000000000000000001"}, // WRONG 2600000000000000000000000000000000000000010a
156-
//{[]byte{0x26, 0x1b}, "-0.00000000000000000000000000000000000001"}, // WRONG 2600000000000000000000000000000000000000010b
157-
158-
//{[]byte{0x03, 0x01, 0x24, 0x01, 0x0c}, "0.000000000000000000000000000000000010"},
159-
// {[]byte{0xc70b010f0123456789000000000c}, "123.456789000000000"},
160-
// {[]byte{0xc70a010f02718281828459045c}, "2.718281828459045"},
161-
// {[]byte{0xc70a010f02718281828459045d}, "-2.718281828459045"},
162-
// {[]byte{0xc70a010f03141592653589793c}, "3.141592653589793"},
163-
// {[]byte{0xc70a010f03141592653589793d}, "-3.141592653589793"},
164-
// {[]byte{0xc7150100099999999999999999999999999999999999999c}, "99999999999999999999999999999999999999"},
165-
// {[]byte{0xc7150100099999999999999999999999999999999999999d}, "-99999999999999999999999999999999999999"},
166-
// {[]byte{0xc7150113012345678912345678900987654321987654321c}, "1234567891234567890.0987654321987654321"},
167-
// {[]byte{0xc7150113012345678912345678900987654321987654321d}, "-1234567891234567890.0987654321987654321"},
146+
//{[]byte{0x0, 0x1a}, "1"}, // d501001c
147+
//{[]byte{0x0, 0x1b}, "-1"}, // d501001d
148+
//{[]byte{0x1, 0x1a}, "0.1"}, // d501011c
149+
//{[]byte{0x1, 0x1b}, "-0.1"}, // d501011d
150+
//{[]byte{0x25, 0x1a}, "0.0000000000000000000000000000000000001"}, // d501251c
151+
//{[]byte{0x25, 0x1b}, "-0.0000000000000000000000000000000000001"}, // d501251d
152+
//{[]byte{0x26, 0x1a}, "0.00000000000000000000000000000000000001"}, // d501261c
153+
//{[]byte{0x26, 0x1b}, "-0.00000000000000000000000000000000000001"}, // d501261d
154+
//{[]byte{0x03, 0x01, 0x24, 0x01, 0x0c}, "0.000000000000000000000000000000000010"}, // c7030124010c
155+
//{[]byte{0x00, 0x09, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9c}, "99999999999999999999999999999999999999"}, // c7150100099999999999999999999999999999999999999c
156+
//{[]byte{0x00, 0x09, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9d}, "-99999999999999999999999999999999999999"}, // c7150100099999999999999999999999999999999999999d
157+
158+
{[]byte{0x02, 0x01, 0x23, 0x4b}, "-12.34"}, // d6010201234d
159+
{[]byte{0x02, 0x01, 0x23, 0x4a}, "12.34"}, // d6010201234c
160+
{[]byte{0x01, 0x01, 0x4a}, "1.4"}, // c7030101014c
161+
{[]byte{0x0, 0xa}, "0"}, // d501000c
162+
{[]byte{0x0, 0xb}, "-0"}, // d501000d
163+
{[]byte{0x0f, 0x01, 0x23, 0x45, 0x67, 0x89, 0x00, 0x00, 0x00, 0x00, 0x0a}, "123.456789000000000"}, // c70b010f0123456789000000000c
164+
{[]byte{0x0f, 0x02, 0x71, 0x82, 0x81, 0x82, 0x84, 0x59, 0x04, 0x5a}, "2.718281828459045"}, // c70a010f02718281828459045c
165+
{[]byte{0x0f, 0x02, 0x71, 0x82, 0x81, 0x82, 0x84, 0x59, 0x04, 0x5b}, "-2.718281828459045"}, // c70a010f02718281828459045d
166+
{[]byte{0x0f, 0x03, 0x14, 0x15, 0x92, 0x65, 0x35, 0x89, 0x79, 0x3a}, "3.141592653589793"}, // c70a010f03141592653589793c
167+
{[]byte{0x0f, 0x03, 0x14, 0x15, 0x92, 0x65, 0x35, 0x89, 0x79, 0x3b}, "-3.141592653589793"}, // c70a010f03141592653589793d
168+
{[]byte{0x13, 0x01, 0x23, 0x45, 0x67, 0x89, 0x12, 0x34, 0x56, 0x78, 0x90, 0x09, 0x87, 0x65, 0x43, 0x21, 0x98, 0x76, 0x54, 0x32, 0x1a}, "1234567891234567890.0987654321987654321"}, // c7150113012345678912345678900987654321987654321c
169+
{[]byte{0x13, 0x01, 0x23, 0x45, 0x67, 0x89, 0x12, 0x34, 0x56, 0x78, 0x90, 0x09, 0x87, 0x65, 0x43, 0x21, 0x98, 0x76, 0x54, 0x32, 0x1b}, "-1234567891234567890.0987654321987654321"}, // c7150113012345678912345678900987654321987654321d
168170
}
169171

170172
func TestMPEncodeStringToBCD(t *testing.T) {
@@ -176,7 +178,9 @@ func TestMPEncodeStringToBCD(t *testing.T) {
176178

177179
}
178180
if reflect.DeepEqual(buf, testcase.bcdBuf) != true {
179-
t.Fatalf("Failed to encode string with decimal '%s' to BCD (actual: '%x', expected '%x')", testcase.numString, buf, testcase.bcdBuf)
181+
fmt.Printf("Actual: '%x'\n", buf)
182+
fmt.Printf("Expected: '%x'\n", testcase.bcdBuf)
183+
t.Fatalf("Failed to encode string with decimal '%s' to BCD", testcase.numString)
180184
}
181185
})
182186
}
@@ -190,20 +194,18 @@ func TestMPDecodeStringFromBCD(t *testing.T) {
190194
t.Fatalf("Failed to decode BCD ('%x') to string with decimal (actual: '%s', expected '%s')", testcase.bcdBuf, s, testcase.numString)
191195
}
192196

193-
/*
194-
descStr := strings.Join(s, "")
195-
decActual, err := decimal.NewFromString()
196-
if err != nil {
197-
t.Fatalf("Failed to encode string ('%s') to decimal", decStr)
198-
}
199-
decExpected, err := decimal.NewFromString(testcase.numString)
200-
if err != nil {
201-
t.Fatalf("Failed to encode string to decimal")
202-
}
203-
if !decExpected.Equal(decActual) {
204-
t.Fatalf("Failed to decode decimal (%s) from BCD (%x) - '%x'", testcase.numString, testcase.bcdBuf, s)
205-
}
206-
*/
197+
descStr := strings.Join(s, "")
198+
decActual, err := decimal.NewFromString()
199+
if err != nil {
200+
t.Fatalf("Failed to encode string ('%s') to decimal", decStr)
201+
}
202+
decExpected, err := decimal.NewFromString(testcase.numString)
203+
if err != nil {
204+
t.Fatalf("Failed to encode string to decimal")
205+
}
206+
if !decExpected.Equal(decActual) {
207+
t.Fatalf("Failed to decode decimal (%s) from BCD (%x) - '%x'", testcase.numString, testcase.bcdBuf, s)
208+
}
207209
})
208210
}
209211
}

0 commit comments

Comments
 (0)