This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
strings.go
156 lines (130 loc) · 3.14 KB
/
strings.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
package tools
import (
"encoding/binary"
"github.com/ronaksoft/rony/pools"
"reflect"
"strconv"
"strings"
"unsafe"
)
/*
Creation Time: 2021 - Mar - 29
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
/*
Strings Builder helper functions
*/
func AppendStrInt(sb *strings.Builder, x int) {
b := pools.TinyBytes.GetLen(8)
binary.BigEndian.PutUint64(b, uint64(x))
sb.Write(b)
pools.TinyBytes.Put(b)
}
func AppendStrUInt(sb *strings.Builder, x uint) {
b := pools.TinyBytes.GetLen(8)
binary.BigEndian.PutUint64(b, uint64(x))
sb.Write(b)
pools.TinyBytes.Put(b)
}
func AppendStrInt64(sb *strings.Builder, x int64) {
b := pools.TinyBytes.GetLen(8)
binary.BigEndian.PutUint64(b, uint64(x))
sb.Write(b)
pools.TinyBytes.Put(b)
}
func AppendStrUInt64(sb *strings.Builder, x uint64) {
b := pools.TinyBytes.GetLen(8)
binary.BigEndian.PutUint64(b, x)
sb.Write(b)
pools.TinyBytes.Put(b)
}
func AppendStrInt32(sb *strings.Builder, x int32) {
b := pools.TinyBytes.GetLen(4)
binary.BigEndian.PutUint32(b, uint32(x))
sb.Write(b)
pools.TinyBytes.Put(b)
}
func AppendStrUInt32(sb *strings.Builder, x uint32) {
b := pools.TinyBytes.GetLen(4)
binary.BigEndian.PutUint32(b, x)
sb.Write(b)
pools.TinyBytes.Put(b)
}
/*
String Conversion helper functions
*/
func StrToFloat64(s string) float64 {
v, _ := strconv.ParseFloat(s, 64)
return v
}
func StrToFloat32(s string) float32 {
v, _ := strconv.ParseFloat(s, 32)
return float32(v)
}
func StrToInt64(s string) int64 {
v, _ := strconv.ParseInt(s, 10, 64)
return v
}
func StrToInt32(s string) int32 {
v, _ := strconv.ParseInt(s, 10, 32)
return int32(v)
}
func StrToUInt64(s string) uint64 {
v, _ := strconv.ParseInt(s, 10, 64)
return uint64(v)
}
func StrToUInt32(s string) uint32 {
v, _ := strconv.ParseInt(s, 10, 32)
return uint32(v)
}
func StrToInt(s string) int {
v, _ := strconv.ParseInt(s, 10, 32)
return int(v)
}
func Int64ToStr(x int64) string {
return strconv.FormatInt(x, 10)
}
func Int32ToStr(x int32) string {
return strconv.FormatInt(int64(x), 10)
}
func UInt64ToStr(x uint64) string {
return strconv.FormatUint(x, 10)
}
func UInt32ToStr(x uint32) string {
return strconv.FormatUint(uint64(x), 10)
}
func IntToStr(x int) string {
return strconv.FormatUint(uint64(x), 10)
}
// ByteToStr converts byte slice to a string without memory allocation.
// Note it may break if string and/or slice header will change
// in the future go versions.
func ByteToStr(bts []byte) string {
/* #nosec G103 */
return *(*string)(unsafe.Pointer(&bts))
}
// B2S is alias for ByteToStr
func B2S(bts []byte) string {
return ByteToStr(bts)
}
// StrToByte converts string to a byte slice without memory allocation.
// Note it may break if string and/or slice header will change
// in the future go versions.
func StrToByte(str string) (b []byte) {
/* #nosec G103 */
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
/* #nosec G103 */
sh := (*reflect.StringHeader)(unsafe.Pointer(&str))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
}
// S2B is alias for StrToByte
func S2B(str string) []byte {
return StrToByte(str)
}