forked from go-gota/gota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-string.go
147 lines (130 loc) · 2.41 KB
/
type-string.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
package series
import (
"fmt"
"math"
"strconv"
"strings"
)
type stringElement struct {
e string
nan bool
}
func (e *stringElement) Set(value interface{}) {
e.nan = false
switch value.(type) {
case string:
e.e = string(value.(string))
if e.e == "NaN" {
e.nan = true
return
}
case int:
e.e = strconv.Itoa(value.(int))
case float64:
e.e = strconv.FormatFloat(value.(float64), 'f', 6, 64)
case bool:
b := value.(bool)
if b {
e.e = "true"
} else {
e.e = "false"
}
case Element:
e.e = value.(Element).String()
default:
e.nan = true
return
}
return
}
func (e stringElement) Copy() Element {
if e.IsNA() {
return &stringElement{"", true}
}
return &stringElement{e.e, false}
}
func (e stringElement) IsNA() bool {
if e.nan {
return true
}
return false
}
func (e stringElement) Type() Type {
return String
}
func (e stringElement) Val() ElementValue {
if e.IsNA() {
return nil
}
return string(e.e)
}
func (e stringElement) String() string {
if e.IsNA() {
return "NaN"
}
return string(e.e)
}
func (e stringElement) Int() (int, error) {
if e.IsNA() {
return 0, fmt.Errorf("can't convert NaN to int")
}
return strconv.Atoi(e.e)
}
func (e stringElement) Float() float64 {
if e.IsNA() {
return math.NaN()
}
f, err := strconv.ParseFloat(e.e, 64)
if err != nil {
return math.NaN()
}
return f
}
func (e stringElement) Bool() (bool, error) {
if e.IsNA() {
return false, fmt.Errorf("can't convert NaN to bool")
}
switch strings.ToLower(e.e) {
case "true", "t", "1":
return true, nil
case "false", "f", "0":
return false, nil
}
return false, fmt.Errorf("can't convert String \"%v\" to bool", e.e)
}
func (e stringElement) Eq(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return e.e == elem.String()
}
func (e stringElement) Neq(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return e.e != elem.String()
}
func (e stringElement) Less(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return e.e < elem.String()
}
func (e stringElement) LessEq(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return e.e <= elem.String()
}
func (e stringElement) Greater(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return e.e > elem.String()
}
func (e stringElement) GreaterEq(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return e.e >= elem.String()
}