-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
189 lines (152 loc) · 4.14 KB
/
utils.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package mseedio
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
"time"
)
// getDigitsFloat64 returns the number of digits in a float64
func getDigitsFloat64(num float64) (intDigitCount int, fracDigitCount int) {
strNum := strconv.FormatFloat(num, 'f', -1, 64)
parts := strings.Split(strNum, ".")
intPart := parts[0]
for _, char := range intPart {
if char >= '0' && char <= '9' {
intDigitCount++
}
}
var fracPart string
if len(parts) > 1 {
fracPart = parts[1]
}
for _, char := range fracPart {
if char >= '0' && char <= '9' {
fracDigitCount++
}
}
return intDigitCount, fracDigitCount
}
// nextPow2 returns the next power of 2
func nextPow2(num int) int {
if num <= 0 {
return 1
}
num--
num |= num >> 1
num |= num >> 2
num |= num >> 4
num |= num >> 8
num |= num >> 16
num++
return num
}
// setSignToUint sets sign to an unsigned integer
func setSignToUint(value, bitWidth uint32) int32 {
if value>>(bitWidth-1) == 1 {
offset := int64(math.Pow(2, float64(bitWidth)))
return int32(int64(value) - offset)
}
return int32(value)
}
// getMergedUint combines bytes into an uint number by its space
func getMergedUint(data []byte, space, bitOrder int) (uint, error) {
if space <= 0 || space > 32 {
return 0, fmt.Errorf("invalid bits space value")
}
numSegments := 32 / space
if len(data) != numSegments {
return 0, fmt.Errorf("invalid data length")
}
var number uint
if bitOrder == LSBFIRST {
for i := 0; i < numSegments; i++ {
number |= uint(data[i]) << (space * i)
}
return number, nil
}
for i := numSegments - 1; i >= 0; i-- {
number |= uint(data[i]) << (space * (numSegments - 1 - i))
}
return number, nil
}
// getSplitedBytes returns bytes by sapce from a number
func getSplitedBytes(number uint, space, bitOrder int) ([]byte, error) {
if space <= 0 || space > 32 {
return nil, fmt.Errorf("invalid bits space value")
}
numSegments := 32 / space
dataArray := make([]byte, 0, numSegments)
mask := (1 << space) - 1
if bitOrder == LSBFIRST {
for i := 0; i < numSegments; i++ {
data := byte(number & uint(mask))
dataArray = append(dataArray, data)
number >>= space
}
return dataArray, nil
}
for i := numSegments - 1; i >= 0; i-- {
data := byte((number >> (space * i)) & uint(mask))
dataArray = append(dataArray, data)
}
return dataArray, nil
}
// getBitOrder returns bit order from SectionEndOffset
func getBitOrder(buffer []byte) (int, error) {
if len(buffer) < 2 {
return -1, fmt.Errorf("buffer is too short")
}
bitOrder := assembleInt(buffer, 2, MSBFIRST)
if bitOrder == FIXED_SECTION_LENGTH {
return MSBFIRST, nil
}
bitOrder = assembleInt(buffer, 2, LSBFIRST)
if bitOrder == FIXED_SECTION_LENGTH {
return LSBFIRST, nil
}
return -1, fmt.Errorf("buffer is not SectionEndOffset")
}
// getBlocketteType returns blockette type
func getBlocketteType(buffer []byte, bitOrder int) (int32, error) {
if len(buffer) < 2 {
return 0, fmt.Errorf("buffer is too short")
}
typ := assembleInt(buffer, 2, bitOrder)
return typ, nil
}
// getDaysByDate returns days of year
func getDaysByDate(date time.Time) int {
return date.YearDay()
}
// getMonthByDays returns month by days of year
func getMonthByDays(year, days int) time.Time {
if days < 1 || days > 366 {
return time.Time{}
}
return time.Date(year, time.January, days, 0, 0, 0, 0, time.UTC)
}
// getStructFieldValue gets the value of a struct field with reflection
func getStructFieldValue(v reflect.Value, fieldName string) (any, error) {
field := v.FieldByName(fieldName)
if !field.IsValid() {
return nil, fmt.Errorf("field %s does not exist", fieldName)
}
return field.Interface(), nil
}
// setStructFieldValue sets the value of a struct field with reflection
func setStructFieldValue(v reflect.Value, fieldName string, fieldValue any) error {
field := v.FieldByName(fieldName)
if !field.IsValid() {
return fmt.Errorf("field %s does not exist", fieldName)
}
if !field.CanSet() {
return fmt.Errorf("cannot set field %s", fieldName)
}
if field.Type().Kind() != reflect.TypeOf(fieldValue).Kind() {
return fmt.Errorf("type mismatch for field %s", fieldName)
}
field.Set(reflect.ValueOf(fieldValue))
return nil
}