-
Notifications
You must be signed in to change notification settings - Fork 22
/
real.go
45 lines (36 loc) · 914 Bytes
/
real.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
package asn1
import (
"bytes"
"math"
"strconv"
)
type realEncoder float64
func (b realEncoder) length() int {
return 2
}
func (e realEncoder) encode() ([]byte, error) {
// ECMA-63
// https://www.ecma-international.org/wp-content/uploads/ECMA-63_1st_edition_september_1980.pdf
n := float64(e)
switch {
case math.IsInf(n, 1):
return []byte{0x40}, nil
case math.IsInf(n, -1):
return []byte{0x41}, nil
case math.IsNaN(n):
return []byte{0x42}, nil
case n == 0.0:
if math.Signbit(n) {
return []byte{0x43}, nil
}
}
nString := []byte(strconv.FormatFloat(n, 'G', -1, 64))
var buf []byte
if bytes.Contains(nString, []byte{'E'}) {
buf = []byte{0x03}
} else {
buf = []byte{0x02}
}
buf = append(buf, nString...)
return buf, nil
}