-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_string.go
167 lines (128 loc) · 3.28 KB
/
to_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gost
import (
"math/big"
"reflect"
"strconv"
)
// A trait for converting a value to a String.
type ToString[T any] interface {
ToString() String
}
func castToToString[T any](value T) Option[ToString[T]] {
reflectedValue := reflect.ValueOf(value)
if casted, ok := reflectedValue.Interface().(ToString[T]); ok {
return Some[ToString[T]](casted)
} else {
return None[ToString[T]]()
}
}
func (self ISize) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self I8) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self I16) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self I32) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self I64) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self I128) ToString() String {
isNegative := self.high < 0
if self.high == 0 {
return self.low.ToString()
} else {
if isNegative {
self = self.Neg()
}
high := self.high
low := self.low
binaryString := ""
for i := 0; i < 64; i++ {
binaryString = string((low & 1).ToString()[0]) + binaryString
low = low >> 1
}
for i := 0; i < 64; i++ {
binaryString = string((high & 1).ToString()[0]) + binaryString
high = high >> 1
}
return _ConvertBinaryStringToDecimalString(String(binaryString), Bool(isNegative))
}
}
func (self USize) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self U8) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self U16) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self U32) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self U64) ToString() String {
return String(strconv.FormatUint(uint64(self), 10))
}
func (self U128) ToString() String {
high := self.high
low := self.low
if high == 0 {
return low.ToString()
} else {
binaryString := ""
for i := 0; i < 64; i++ {
binaryString = string((low & 1).ToString()[0]) + binaryString
low = low >> 1
}
for i := 0; i < 64; i++ {
binaryString = string((high & 1).ToString()[0]) + binaryString
high = high >> 1
}
return _ConvertBinaryStringToDecimalString(String(binaryString), false)
}
}
func (self F32) ToString() String {
return String(strconv.FormatFloat(float64(self), 'f', -1, 32))
}
func (self F64) ToString() String {
return String(strconv.FormatFloat(float64(self), 'f', -1, 64))
}
func (self Byte) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self Char) ToString() String {
return String(strconv.Itoa(int(self)))
}
func (self String) ToString() String {
return String(self)
}
func (self Bool) ToString() String {
return String(strconv.FormatBool(bool(self)))
}
func (self Complex64) ToString() String {
return String(strconv.FormatComplex(complex128(self), 'f', -1, 64))
}
func (self Complex128) ToString() String {
return String(strconv.FormatComplex(complex128(self), 'f', -1, 128))
}
func _ConvertBinaryStringToDecimalString(binaryString String, isNegative Bool) String {
decimal := big.NewInt(0)
i := len(binaryString) - 1
digit := big.NewInt(1)
for i >= 0 {
if binaryString[i] == '1' {
decimal.Add(decimal, digit)
}
digit = digit.Mul(digit, big.NewInt(2))
i--
}
if isNegative {
decimal.Neg(decimal)
}
return String(decimal.String())
}