-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocinum.go
255 lines (236 loc) · 6.7 KB
/
ocinum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2016 Tamás Gulácsi. All rights reserved.
// Use of this source code is governed by The MIT License
// found in the accompanying LICENSE file.
package num
/*
#include <oci.h>
#include <stdlib.h>
*/
//import "C"
import (
"bytes"
"strconv"
"strings"
"sync"
"github.com/pkg/errors"
)
// OCINum is an OCINumber
//
// SQLT_VNU: 22 bytes, at max.
// SQLT_NUM: 21 bytes
//
// http://docs.oracle.com/cd/B28359_01/appdev.111/b28395/oci03typ.htm#sthref365
//
/*
Oracle stores values of the NUMBER datatype in a variable-length format. The first byte is the exponent and is followed by 1 to 20 mantissa bytes. The high-order bit of the exponent byte is the sign bit; it is set for positive numbers and it is cleared for negative numbers. The lower 7 bits represent the exponent, which is a base-100 digit with an offset of 65.
To calculate the decimal exponent, add 65 to the base-100 exponent and add another 128 if the number is positive. If the number is negative, you do the same, but subsequently the bits are inverted. For example, -5 has a base-100 exponent = 62 (0x3e). The decimal exponent is thus (~0x3e) -128 - 65 = 0xc1 -128 -65 = 193 -128 -65 = 0.
Each mantissa byte is a base-100 digit, in the range 1..100. For positive numbers, the digit has 1 added to it. So, the mantissa digit for the value 5 is 6. For negative numbers, instead of adding 1, the digit is subtracted from 101. So, the mantissa digit for the number -5 is 96 (101 - 5). Negative numbers have a byte containing 102 appended to the data bytes. However, negative numbers that have 20 mantissa bytes do not have the trailing 102 byte. Because the mantissa digits are stored in base 100, each byte can represent 2 decimal digits. The mantissa is normalized; leading zeroes are not stored.
Up to 20 data bytes can represent the mantissa. However, only 19 are guaranteed to be accurate. The 19 data bytes, each representing a base-100 digit, yield a maximum precision of 38 digits for an Oracle NUMBER.
If you specify the datatype code 2 in the dty parameter of an OCIDefineByPos() call, your program receives numeric data in this Oracle internal format. The output variable should be a 21-byte array to accommodate the largest possible number. Note that only the bytes that represent the number are returned. There is no blank padding or NULL termination. If you need to know the number of bytes returned, use the VARNUM external datatype instead of NUMBER.
*/
//
// So the number is stored as sign * significand * 100^exponent where significand is in 1.xxx format.
type OCINum []byte
var (
ErrTooLong = errors.New("input string too long")
ErrNoDigit = errors.New("no digit found")
ErrBadCharacter = errors.New("bad character")
)
// IsNull returns whether the underlying number is NULL.
func (num OCINum) IsNull() bool { return len(num) < 2 }
// Print the number into the given byte slice.
//
//
func (num OCINum) Print(buf []byte) []byte {
if len(num) == 0 {
return buf[:0]
}
res := buf[:0]
if bytes.Equal(num, []byte{128}) {
return append(res, '0')
}
if len(num) < 2 {
return buf[:0]
}
b, num := num[0], num[1:]
negative := b&(1<<7) == 0
exp := int(b) & 0x7f
D := func(b byte) int64 { return int64(b - 1) }
if negative {
D = func(b byte) int64 { return int64(101 - b) }
res = append(res, '-')
exp = int((^b) & 0x7f)
if num[len(num)-1] == 102 {
num = num[:len(num)-1]
}
}
exp -= 65
var dotWritten bool
digits := bytesPool.Get().([]byte)[:0]
for i, b := range num {
j := D(b)
if j < 10 {
digits = append(digits, '0', '0'+(byte(j)))
} else {
digits = strconv.AppendInt(digits, j, 10)
}
if i == exp {
if len(digits) == 0 {
digits = append(digits, '0')
}
digits = append(digits, '.')
dotWritten = true
}
}
for i := len(num) - 1; i < exp; i++ {
digits = append(digits, '0', '0')
}
if !dotWritten {
if exp < 0 {
dexp := (-exp) << 1
digits = append(make([]byte, dexp, dexp+len(digits)), digits...)
digits[0] = '0'
digits[1] = '.'
for i := 2; i < dexp; i++ {
digits[i] = '0'
}
} else {
n := len(digits)
if cap(digits) < n+1 {
digits = append(digits, '0')
} else {
digits = digits[:n+1]
}
place := (exp + 1) << 1
copy(digits[place+1:], digits[place:n])
digits[place] = '.'
}
dotWritten = true
}
for len(digits) > 2 && digits[0] == '0' && digits[1] != '.' {
digits = digits[1:]
}
res = append(res, digits...)
bytesPool.Put(digits)
if dotWritten {
for res[len(res)-1] == '0' {
res = res[:len(res)-1]
}
if res[len(res)-1] == '.' {
res = res[:len(res)-1]
}
}
// 1 = 1 * 100^0
// 10 = 10 * 100^0
// 100 = 1 * 100^1
// 1000 = 10 * 100^1
// 0.1 = 10 * 100^-1
// 0.01 = 1 * 100^-1
// 0.001 = 10 * 100^-2
// 0.0001 = 1 * 100^-2
return res
}
var bytesPool = sync.Pool{New: func() interface{} { return make([]byte, 0, 42) }}
// String returns the string representation of the number.
func (num OCINum) String() string {
b := bytesPool.Get().([]byte)
s := string(num.Print(b))
bytesPool.Put(b)
return s
}
// SetString sets the OCINum to the number in s.
func (num *OCINum) SetString(s string) error {
s = strings.TrimSpace(s)
if len(s) == 0 || s == "0" {
*num = OCINum([]byte{128})
return nil
}
var (
dotSeen bool
nonZeros, numCount int
)
for i, r := range s {
if '0' <= r && r <= '9' {
numCount++
if numCount == 40 {
return errors.Wrapf(ErrTooLong, "got %d, max 39 (%q)", numCount, s)
}
if r != '0' {
nonZeros++
}
continue
}
if i == 0 && r == '-' {
continue
}
if !dotSeen && r == '.' {
dotSeen = true
continue
}
return errors.Wrapf(ErrBadCharacter, "%c in %q", r, s)
}
if numCount == 0 {
return errors.Wrap(ErrNoDigit, s)
}
if nonZeros == 0 {
*num = OCINum([]byte{128})
return nil
}
// x = b - 1 <=> b = x + 1
D := func(b byte) byte { return b + 1 }
var negative bool
if s[0] == '-' {
negative = true
s = s[1:]
// x = 101 - b <=> b = 101 - x
D = func(b byte) byte { return 101 - b }
}
i := len(s)
if j := strings.IndexByte(s, '.'); j >= 0 {
if j == 1 && s[0] == '0' {
s = s[2:]
i = 0
} else {
if j%2 != 0 {
s = "0" + s
j++
}
s = s[:j] + s[j+1:]
i = j
}
if len(s)%2 == 1 {
s = s + "0"
}
} else if len(s)%2 == 1 {
s = "0" + s
i = len(s)
}
for j := len(s) - 2; j > 0 && s[j] == '0' && s[j+1] == '0'; j -= 2 {
s = s[:j]
}
exp := (i >> 1) - 1
n := 1 + (len(s) >> 1) + 1
if n > 21 {
n = 21
}
if cap(*num) < n {
*num = make([]byte, 1, n)
} else {
*num = (*num)[:1]
}
for i := 0; i < len(s)-1; i += 2 {
b := 10*(s[i]-'0') + s[i+1] - '0'
*num = append(*num, D(b))
}
exp += 65
if negative {
exp = (^exp) & 0x7f
if n < 21 {
*num = append(*num, 102)
}
} else {
exp |= (1 << 7)
}
(*num)[0] = byte(exp)
return nil
}