/
enum_mlevel.go
76 lines (64 loc) · 2.06 KB
/
enum_mlevel.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
package flate
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// MemoryLevel indicates the amount of RAM for the Reader or Writer to use.
type MemoryLevel byte
const (
// DefaultMemory requests that the default value for MemoryLevel be
// selected. This is currently equivalent to 9 (FastestMemory).
DefaultMemory MemoryLevel = 0
// SmallestMemory is the smallest possible MemoryLevel. As MemoryLevel
// increases from 1 (SmallestMemory) to 9 (FastestMemory), the amount
// of memory used doubles with each increase.
SmallestMemory MemoryLevel = 1
// FastestMemory is the largest possible MemoryLevel. As MemoryLevel
// increases from 1 (SmallestMemory) to 9 (FastestMemory), the amount
// of memory used doubles with each increase.
FastestMemory MemoryLevel = 9
)
// IsValid returns true if mlevel is a valid MemoryLevel constant.
func (mlevel MemoryLevel) IsValid() bool {
return mlevel >= DefaultMemory && mlevel <= FastestMemory
}
// GoString returns the Go string representation of this MemoryLevel constant.
func (mlevel MemoryLevel) GoString() string {
if mlevel == DefaultMemory {
return "DefaultMemory"
}
return fmt.Sprintf("MemoryLevel(%d)", uint(mlevel))
}
// String returns the string representation of this MemoryLevel constant.
func (mlevel MemoryLevel) String() string {
if mlevel == DefaultMemory {
return strDefault
}
return fmt.Sprintf("%d", uint(mlevel))
}
// MarshalJSON returns the JSON representation of this MemoryLevel constant.
func (mlevel MemoryLevel) MarshalJSON() ([]byte, error) {
return json.Marshal(uint(mlevel))
}
// Parse parses a string representation of a MemoryLevel constant.
func (mlevel *MemoryLevel) Parse(str string) error {
if strings.EqualFold(str, strDefault) {
*mlevel = DefaultMemory
return nil
}
u64, err := strconv.ParseUint(str, 10, 8)
if err != nil {
*mlevel = DefaultMemory
return err
}
if u64 > 9 {
*mlevel = DefaultMemory
return fmt.Errorf("value %d is out of range", u64)
}
*mlevel = MemoryLevel(u64)
return nil
}
var _ fmt.GoStringer = MemoryLevel(0)
var _ fmt.Stringer = MemoryLevel(0)