• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

enetx / g / 22200209868

19 Feb 2026 09:08PM UTC coverage: 89.376% (-0.4%) from 89.809%
22200209868

push

github

enetx
perf: reduce allocations across string, collection, and encoding types

string:
- Lower/Upper/IsLower/IsUpper/IsTitle: Bytes() → BytesUnsafe()/StringUnsafe()
- Random: ASCII fast-path with WriteByte, avoid global charset Runes() alloc
- ReplaceNth: Builder instead of triple string concatenation
- Chunks: ASCII fast-path (zero-copy slicing); Unicode path via
  utf8.DecodeRuneInString, eliminating []rune alloc and per-chunk String([]rune) alloc
- Truncate: ASCII fast-path + single-pass Unicode walk; drop LenRunes()+Runes() double scan
- Center: cache s.LenRunes() and pad.LenRunes() (4 scans → 2)
- writePadding: ASCII fast-path with WriteByte; non-ASCII via utf8.DecodeRuneInString,
  eliminating pad.Runes() alloc on every call
- Reverse: BytesUnsafe()/StringUnsafe()
- Similarity: plain []int for DP table, min() builtins

string/hash: BytesUnsafe()/StringUnsafe() for MD5/SHA1/SHA256/SHA512

string/encode:
- Base64/JSON: BytesUnsafe() to avoid copy
- URL: Grow() pre-allocation
- Hex: inline lookup table — zero per-byte allocs (was: strconv.FormatInt per byte)
- Binary: inline bit shift — zero per-byte allocs (was: fmt.Sprintf per byte)
- Octal: strconv.AppendInt into stack buffer — zero per-rune allocs

string/compress: all five writers use BytesUnsafe() instead of io.WriteString
  (io.WriteString on non-StringWriter falls back to []byte(s) copy)

int: Hex/Octal use strconv.FormatInt instead of fmt.Sprintf

set/map/map_ordered: simplify NewX size param (drop Slice[Int] unwrap indirection)

String() methods: add Grow() pre-allocation to Slice, Set, Map, MapOrd,
  MapSafe, Heap, Deque (n*8 or n*16 estimate reduces realloc churn)

slice: Join pre-computes exact size and uses a single strings.Builder,
  eliminating intermediate []string allocation

133 of 164 new or added lines in 12 files covered. (81.1%)

9 existing lines in 4 files now uncovered.

5325 of 5958 relevant lines covered (89.38%)

14237.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.51
/float.go
1
package g
2

3
import (
4
        "database/sql/driver"
5
        "encoding/binary"
6
        "fmt"
7
        "math"
8
        "math/big"
9
        "strconv"
10

11
        "github.com/enetx/g/cmp"
12
        "github.com/enetx/g/constraints"
13
)
14

15
// Float is an alias for the float64 type.
16
type Float float64
17

18
// NewFloat creates a new Float with the provided value.
19
func NewFloat[T constraints.Float | constraints.Integer](float T) Float { return Float(float) }
1✔
20

21
// Transform applies a transformation function to the Float and returns the result.
22
func (f Float) Transform(fn func(Float) Float) Float { return fn(f) }
2✔
23

24
// BytesBE returns the IEEE-754 representation of the Float as Bytes in BigEndian order.
25
// The Float is converted to its 64-bit IEEE-754 binary representation.
26
func (f Float) BytesBE() Bytes {
73✔
27
        var buf [8]byte
73✔
28
        bits := math.Float64bits(float64(f))
73✔
29
        binary.BigEndian.PutUint64(buf[:], bits)
73✔
30

73✔
31
        return Bytes(buf[:])
73✔
32
}
73✔
33

34
// BytesLE returns the IEEE-754 representation of the Float as Bytes in LittleEndian order.
35
// The Float is converted to its 64-bit IEEE-754 binary representation.
36
func (f Float) BytesLE() Bytes {
73✔
37
        var buf [8]byte
73✔
38
        bits := math.Float64bits(float64(f))
73✔
39
        binary.LittleEndian.PutUint64(buf[:], bits)
73✔
40

73✔
41
        return Bytes(buf[:])
73✔
42
}
73✔
43

44
// Min returns the minimum of two Floats.
45
func (f Float) Min(b ...Float) Float { return cmp.Min(append(b, f)...) }
1✔
46

47
// Max returns the maximum of two Floats.
48
func (f Float) Max(b ...Float) Float { return cmp.Max(append(b, f)...) }
1✔
49

50
// Sqrt returns the square root of the Float.
51
//
52
// Returns:
53
//   - Float: The square root of the Float. If the Float is negative, the result is NaN.
54
//
55
// Example usage:
56
//
57
//        f := g.NewFloat(9)
58
//        result := f.Sqrt() // result = 3
59
func (f Float) Sqrt() Float { return Float(math.Sqrt(f.Std())) }
×
60

61
// Pow raises the Float to the power of the given exponent.
62
//
63
// Parameters:
64
//   - exp (Float): The exponent to raise the Float to.
65
//
66
// Returns:
67
//   - Float: The result of raising the Float to the specified power.
68
//
69
// Example usage:
70
//
71
//        f := g.NewFloat(2)
72
//        result := f.Pow(3) // result = 8
73
func (f Float) Pow(exp Float) Float { return Float(math.Pow(f.Std(), exp.Std())) }
×
74

75
// Mod returns the floating-point remainder of f / b as defined by IEEE 754.
76
//
77
// Parameters:
78
//   - b (Float): The divisor.
79
//
80
// Returns:
81
//   - Float: The remainder of f / b.
82
//
83
// Example usage:
84
//
85
//        f := g.NewFloat(5.5)
86
//        result := f.Mod(2) // result = 1.5
87
func (f Float) Mod(b Float) Float { return Float(math.Mod(f.Std(), b.Std())) }
×
88

89
// Abs returns the absolute value of the Float.
90
func (f Float) Abs() Float { return Float(math.Abs(f.Std())) }
3✔
91

92
// Add adds two Floats and returns the result.
93
func (f Float) Add(b Float) Float { return f + b }
2✔
94

95
// BigFloat returns the Float as a *big.Float.
96
func (f Float) BigFloat() *big.Float { return big.NewFloat(f.Std()) }
3✔
97

98
// Cmp compares two Floats and returns an cmp.Ordering.
99
func (f Float) Cmp(b Float) cmp.Ordering { return cmp.Cmp(f, b) }
57✔
100

101
// Div divides two Floats and returns the result.
102
func (f Float) Div(b Float) Float { return f / b }
1✔
103

104
// IsZero checks if the Float is 0.
105
func (f Float) IsZero() bool { return f.Eq(0) }
3✔
106

107
// Eq checks if two Floats are equal.
108
func (f Float) Eq(b Float) bool { return f.Cmp(b).IsEq() }
39✔
109

110
// Std returns the Float as a float64.
111
func (f Float) Std() float64 { return float64(f) }
66✔
112

113
// Gt checks if the Float is greater than the specified Float.
114
func (f Float) Gt(b Float) bool { return f.Cmp(b).IsGt() }
4✔
115

116
// Gte checks if the Float is greater than or equal to the specified Float.
117
func (f Float) Gte(b Float) bool { return !f.Lt(b) }
×
118

119
// Lte checks if the Float is less than or equal to the specified Float.
120
func (f Float) Lte(b Float) bool { return !f.Gt(b) }
×
121

122
// Int returns the Float as an Int.
123
func (f Float) Int() Int { return Int(f) }
3✔
124

125
// String returns the Float as an String.
126
func (f Float) String() String { return String(strconv.FormatFloat(f.Std(), 'g', -1, 64)) }
3✔
127

128
// Lt checks if the Float is less than the specified Float.
129
func (f Float) Lt(b Float) bool { return f.Cmp(b).IsLt() }
10✔
130

131
// Mul multiplies two Floats and returns the result.
UNCOV
132
func (f Float) Mul(b Float) Float { return f * b }
×
133

134
// Ne checks if two Floats are not equal.
135
func (f Float) Ne(b Float) bool { return !f.Eq(b) }
32✔
136

137
// Round rounds the Float to the nearest integer and returns the result as an Int.
138
func (f Float) Round() Int { return Int(math.Round(f.Std())) }
7✔
139

140
// RoundDecimal rounds the Float value to the specified number of decimal places.
141
//
142
// Parameters:
143
//   - precision (Int): The number of decimal places to round to. If negative, the Float is returned unchanged.
144
//     Values greater than 308 are capped at 308 to prevent overflow.
145
//
146
// Returns:
147
//   - Float: A new Float value rounded to the specified number of decimal places.
148
func (f Float) RoundDecimal(precision Int) Float {
31✔
149
        if precision < 0 {
32✔
150
                return f
1✔
151
        }
1✔
152

153
        if precision > 308 { // 10^309 == +Inf
30✔
154
                precision = 308
×
155
        }
×
156

157
        pow := math.Pow(10, float64(precision))
30✔
158

30✔
159
        return Float(math.Round(f.Std()*pow) / pow)
30✔
160
}
161

162
// TruncDecimal truncates the Float value to the specified number of decimal places.
163
//
164
// Parameters:
165
//   - precision (Int): The number of decimal places to truncate to. If negative, the Float is returned unchanged.
166
//     Values greater than 308 are capped at 308.
167
//
168
// Returns:
169
//   - Float: A new Float value truncated to the specified number of decimal places.
170
func (f Float) TruncDecimal(precision Int) Float {
×
171
        if precision < 0 {
×
172
                return f
×
173
        }
×
174

175
        if precision > 308 {
×
176
                precision = 308
×
177
        }
×
178

179
        pow := math.Pow(10, float64(precision))
×
180

×
181
        return Float(math.Trunc(f.Std()*pow) / pow)
×
182
}
183

184
// CeilDecimal rounds the Float value up (towards +Inf) to the specified number of decimal places.
185
//
186
// Parameters:
187
//   - precision (Int): The number of decimal places to round up to. If negative, the Float is returned unchanged.
188
//     Values greater than 308 are capped at 308.
189
//
190
// Returns:
191
//   - Float: A new Float value rounded up to the specified number of decimal places.
192
func (f Float) CeilDecimal(precision Int) Float {
×
193
        if precision < 0 {
×
194
                return f
×
195
        }
×
196

197
        if precision > 308 {
×
198
                precision = 308
×
199
        }
×
200

201
        pow := math.Pow(10, float64(precision))
×
202

×
203
        return Float(math.Ceil(f.Std()*pow) / pow)
×
204
}
205

206
// FloorDecimal rounds the Float value down (towards -Inf) to the specified number of decimal places.
207
//
208
// Parameters:
209
//   - precision (Int): The number of decimal places to round down to. If negative, the Float is returned unchanged.
210
//     Values greater than 308 are capped at 308.
211
//
212
// Returns:
213
//   - Float: A new Float value rounded down to the specified number of decimal places.
214
func (f Float) FloorDecimal(precision Int) Float {
×
215
        if precision < 0 {
×
216
                return f
×
217
        }
×
218

219
        if precision > 308 {
×
220
                precision = 308
×
221
        }
×
222

223
        pow := math.Pow(10, float64(precision))
×
224

×
225
        return Float(math.Floor(f.Std()*pow) / pow)
×
226
}
227

228
// Sub subtracts two Floats and returns the result.
UNCOV
229
func (f Float) Sub(b Float) Float { return f - b }
×
230

231
// Bits returns IEEE-754 representation of f.
232
func (f Float) Bits() uint64 { return math.Float64bits(f.Std()) }
10✔
233

234
// Float32 returns the Float as a float32.
235
func (f Float) Float32() float32 { return float32(f) }
3✔
236

237
// Print writes the value of the Float to the standard output (console)
238
// and returns the Float unchanged.
239
func (f Float) Print() Float { fmt.Print(f); return f }
1✔
240

241
// Println writes the value of the Float to the standard output (console) with a newline
242
// and returns the Float unchanged.
243
func (f Float) Println() Float { fmt.Println(f); return f }
1✔
244

245
// Scan implements the database/sql.Scanner interface for g.Float.
246
//
247
// Behavior:
248
//   - If src is nil, the value is set to 0 (SQL NULL).
249
//   - If src is a float64 (common SQL REAL/DOUBLE type), it is assigned.
250
//   - Otherwise, an error is returned.
251
//
252
// Supported SQL types (common):
253
//   - REAL / DOUBLE → float64
254
//
255
// Notes:
256
//   - This allows g.Float to be used directly with database/sql and compatible drivers.
257
func (f *Float) Scan(src any) error {
4✔
258
        if src == nil {
5✔
259
                *f = 0
1✔
260
                return nil
1✔
261
        }
1✔
262

263
        if f64, ok := src.(float64); ok {
5✔
264
                *f = Float(f64)
2✔
265
                return nil
2✔
266
        }
2✔
267

268
        return fmt.Errorf("g.Float.Scan: cannot scan %T into g.Float", src)
1✔
269
}
270

271
// Value implements the database/sql/driver.Valuer interface for g.Float.
272
//
273
// Behavior:
274
//   - Returns the underlying float64 value, ready for database insertion.
275
//   - Always returns a value compatible with SQL REAL / DOUBLE types.
276
func (f Float) Value() (driver.Value, error) { return float64(f), nil }
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc