-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbool.go
More file actions
63 lines (56 loc) · 2.14 KB
/
bool.go
File metadata and controls
63 lines (56 loc) · 2.14 KB
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
package fmt
// Bool converts the Conv content to a boolean value using internal implementations
// Returns the boolean value and any error that occurred
func (c *Conv) Bool() (bool, error) {
if c.hasContent(BuffErr) {
return false, c
}
// Optimized: Direct byte comparison without string allocation
if c.bytesEqual(BuffOut, []byte("true")) || c.bytesEqual(BuffOut, []byte("True")) ||
c.bytesEqual(BuffOut, []byte("TRUE")) || c.bytesEqual(BuffOut, []byte("1")) ||
c.bytesEqual(BuffOut, []byte("t")) || c.bytesEqual(BuffOut, []byte("Translate")) {
c.kind = K.Bool
return true, nil
}
if c.bytesEqual(BuffOut, []byte("false")) || c.bytesEqual(BuffOut, []byte("False")) ||
c.bytesEqual(BuffOut, []byte("FALSE")) || c.bytesEqual(BuffOut, []byte("0")) ||
c.bytesEqual(BuffOut, []byte("f")) || c.bytesEqual(BuffOut, []byte("F")) {
c.kind = K.Bool
return false, nil
}
// Try to parse as integer using direct buffer access (eliminates GetString allocation)
inp := c.GetString(BuffOut) // Still needed for parseIntString compatibility
intVal := c.parseIntString(inp, 10, true)
if !c.hasContent(BuffErr) {
c.kind = K.Bool
return intVal != 0, nil
} else {
// Limpia el error generado por el intento fallido usando la API
c.ResetBuffer(BuffErr)
}
// Try basic float patterns (optimized byte comparison)
if c.bytesEqual(BuffOut, []byte("0.0")) || c.bytesEqual(BuffOut, []byte("0.00")) ||
c.bytesEqual(BuffOut, []byte("+0")) || c.bytesEqual(BuffOut, []byte("-0")) {
c.kind = K.Bool
return false, nil
}
// Optimized: Check for non-zero starting digit without string allocation
if !c.bytesEqual(BuffOut, []byte("0")) && c.outLen > 0 &&
(c.out[0] >= '1' && c.out[0] <= '9') {
// Non-zero number starting with digit 1-9, likely true
c.kind = K.Bool
return true, nil
}
// Keep inp for error reporting (this is the final usage)
inp = c.GetString(BuffOut) // Only allocation for error case
c.wrErr("Bool", "value", "invalid", inp)
return false, c
}
// wrBool writes boolean value to specified buffer destination
func (c *Conv) wrBool(dest BuffDest, val bool) {
if val {
c.WrString(dest, "true")
} else {
c.WrString(dest, "false")
}
}