forked from ohler55/ojg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffloat64.go
62 lines (51 loc) · 1.92 KB
/
ffloat64.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
// Copyright (c) 2021, Peter Ohler, All rights reserved.
package alt
import (
"reflect"
"strconv"
"unsafe"
)
var float64ValFuncs = [8]valFunc{
valFloat64,
valFloat64AsString,
valFloat64NotEmpty,
valFloat64NotEmptyAsString,
ivalFloat64,
ivalFloat64AsString,
ivalFloat64NotEmpty,
ivalFloat64NotEmptyAsString,
}
func valFloat64(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
return *(*float64)(unsafe.Pointer(addr + fi.offset)), nilValue, false
}
func valFloat64AsString(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
return strconv.FormatFloat(*(*float64)(unsafe.Pointer(addr + fi.offset)), 'g', -1, 64), nilValue, false
}
func valFloat64NotEmpty(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
v := *(*float64)(unsafe.Pointer(addr + fi.offset))
return v, nilValue, v == 0.0
}
func valFloat64NotEmptyAsString(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
v := *(*float64)(unsafe.Pointer(addr + fi.offset))
if v == 0.0 {
return nil, nilValue, true
}
return strconv.FormatFloat(v, 'g', -1, 64), nilValue, false
}
func ivalFloat64(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
return rv.FieldByIndex(fi.index).Interface().(float64), nilValue, false
}
func ivalFloat64AsString(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
return strconv.FormatFloat(rv.FieldByIndex(fi.index).Interface().(float64), 'g', -1, 64), nilValue, false
}
func ivalFloat64NotEmpty(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
v := rv.FieldByIndex(fi.index).Interface().(float64)
return v, nilValue, v == 0.0
}
func ivalFloat64NotEmptyAsString(fi *finfo, rv reflect.Value, addr uintptr) (any, reflect.Value, bool) {
v := rv.FieldByIndex(fi.index).Interface().(float64)
if v == 0.0 {
return nil, nilValue, true
}
return strconv.FormatFloat(v, 'g', -1, 64), nilValue, false
}