@@ -29,7 +29,7 @@ const (
29
29
30
30
// The overflowLayoutValue is a exceeding the limit of permissible
31
31
// values for the Layout.
32
- overflowLayoutValue Layout = (1 << iota ) - 1
32
+ overflowLayoutValue Layout = (1 << iota )
33
33
34
34
// Default is the default format for the log message.
35
35
Default = ShortFilePath | FuncName | LineNumber
@@ -41,40 +41,58 @@ type Layout uint8
41
41
// IsSingle returns true if value contains single of the available flag.
42
42
// The custom flags cannot be valid since they should not affect the
43
43
// formatting settings. The zero value is an invalid flag too.
44
- func (f * Layout ) IsSingle () bool {
45
- return bits .OnesCount (uint (* f )) == 1 &&
46
- * f <= Layout (overflowLayoutValue + 1 )>> 1
44
+ func (l * Layout ) IsSingle () bool {
45
+ return bits .OnesCount (uint (* l )) == 1 &&
46
+ * l <= Layout (overflowLayoutValue + 1 )>> 1
47
47
}
48
48
49
49
// Contains method returns true if value contains the specified flag.
50
50
// Returns false and an error if the value is invalid or an
51
51
// invalid flag is specified.
52
- func (f * Layout ) Contains (flag Layout ) (bool , error ) {
52
+ func (l * Layout ) Contains (flag Layout ) (bool , error ) {
53
53
switch {
54
54
case ! flag .IsValid ():
55
55
return false , errors .New ("incorrect flag value" )
56
- case ! f .IsValid ():
56
+ case ! l .IsValid ():
57
57
return false , errors .New ("the object is damaged" )
58
58
}
59
59
60
- return * f & Layout (flag ) == Layout (flag ), nil
60
+ return * l & Layout (flag ) == Layout (flag ), nil
61
61
}
62
62
63
63
// IsValid returns true if value contains zero, one or an
64
64
// unique sum of valid FormatFlag flags. The zero value is a valid value.
65
- func (f * Layout ) IsValid () bool {
66
- return * f <= overflowLayoutValue
65
+ func (l * Layout ) IsValid () bool {
66
+ // Check if object is zero, which is a valid value.
67
+ if * l == 0 {
68
+ return true
69
+ }
70
+
71
+ copy := * l
72
+ // Iterate over all possible values of the constants and
73
+ // check whether they are part of object.
74
+ for layout := Layout (1 ); layout < overflowLayoutValue ; layout <<= 1 {
75
+ // If layout is part of the object, remove it from object.
76
+ if copy & layout == layout {
77
+ copy ^= layout
78
+ }
79
+ }
80
+
81
+ // Check whether all bits of t were "turned off".
82
+ // If t is zero, it means that all bits were matched values
83
+ // of constants, and therefore t is valid.
84
+ return copy == 0
67
85
}
68
86
69
87
// FilePath returns true if value contains the FullPath or ShortPath flags.
70
88
// Returns false and an error if the value is invalid.
71
- func (f * Layout ) FilePath () bool {
72
- ffp , err := f .Contains (FullFilePath )
89
+ func (l * Layout ) FilePath () bool {
90
+ ffp , err := l .Contains (FullFilePath )
73
91
if err == nil && ffp {
74
92
return true
75
93
}
76
94
77
- sfp , err := f .Contains (ShortFilePath )
95
+ sfp , err := l .Contains (ShortFilePath )
78
96
if err == nil && sfp {
79
97
return true
80
98
}
@@ -83,100 +101,100 @@ func (f *Layout) FilePath() bool {
83
101
}
84
102
85
103
// FullFilePath returns true if value contains the FullPath flag.
86
- func (f * Layout ) FullFilePath () bool {
87
- v , _ := f .Contains (FullFilePath )
104
+ func (l * Layout ) FullFilePath () bool {
105
+ v , _ := l .Contains (FullFilePath )
88
106
return v
89
107
}
90
108
91
109
// ShortFilePath returns true if value contains the ShortPath flag.
92
- func (f * Layout ) ShortFilePath () bool {
93
- v , _ := f .Contains (ShortFilePath )
110
+ func (l * Layout ) ShortFilePath () bool {
111
+ v , _ := l .Contains (ShortFilePath )
94
112
return v
95
113
}
96
114
97
115
// FuncName returns true if value contains the FuncName flag.
98
- func (f * Layout ) FuncName () bool {
99
- v , _ := f .Contains (FuncName )
116
+ func (l * Layout ) FuncName () bool {
117
+ v , _ := l .Contains (FuncName )
100
118
return v
101
119
}
102
120
103
121
// FuncAddress returns true if value contains the FuncAddress flag.
104
- func (f * Layout ) FuncAddress () bool {
105
- v , _ := f .Contains (FuncAddress )
122
+ func (l * Layout ) FuncAddress () bool {
123
+ v , _ := l .Contains (FuncAddress )
106
124
return v
107
125
}
108
126
109
127
// LineNumber returns true if value contains the LineNumber flag.
110
- func (f * Layout ) LineNumber () bool {
111
- v , _ := f .Contains (LineNumber )
128
+ func (l * Layout ) LineNumber () bool {
129
+ v , _ := l .Contains (LineNumber )
112
130
return v
113
131
}
114
132
115
133
// Set sets the specified flags ignores duplicates.
116
134
// The flags that were set previously will be discarded.
117
135
// Returns a new value if all is well or old value and an
118
136
// error if one or more invalid flags are specified.
119
- func (f * Layout ) Set (flags ... Layout ) (Layout , error ) {
137
+ func (l * Layout ) Set (flags ... Layout ) (Layout , error ) {
120
138
var r Layout
121
139
122
140
for _ , flag := range flags {
123
141
if ! flag .IsValid () {
124
- return * f , fmt .Errorf ("the %d is invalid flag value" , flag )
142
+ return * l , fmt .Errorf ("the %d is invalid flag value" , flag )
125
143
}
126
144
127
145
if ok , _ := r .Contains (flag ); ! ok {
128
146
r += Layout (flag )
129
147
}
130
148
}
131
149
132
- * f = r
133
- return * f , nil
150
+ * l = r
151
+ return * l , nil
134
152
}
135
153
136
154
// Add adds the specified flags ignores duplicates or flags that value
137
155
// already contains. Returns a new value if all is well or old value and
138
156
// an error if one or more invalid flags are specified.
139
- func (f * Layout ) Add (flags ... Layout ) (Layout , error ) {
140
- r := * f
157
+ func (l * Layout ) Add (flags ... Layout ) (Layout , error ) {
158
+ r := * l
141
159
142
160
for _ , flag := range flags {
143
161
if ! flag .IsValid () {
144
- return * f , fmt .Errorf ("the %d is invalid flag value" , flag )
162
+ return * l , fmt .Errorf ("the %d is invalid flag value" , flag )
145
163
}
146
164
147
165
if ok , _ := r .Contains (flag ); ! ok {
148
166
r += Layout (flag )
149
167
}
150
168
}
151
169
152
- * f = r
153
- return * f , nil
170
+ * l = r
171
+ return * l , nil
154
172
}
155
173
156
174
// Delete deletes the specified flags ignores duplicates or
157
175
// flags that were not set. Returns a new value if all is well or
158
176
// old value and an error if one or more invalid flags are specified.
159
- func (f * Layout ) Delete (flags ... Layout ) (Layout , error ) {
160
- r := * f
177
+ func (l * Layout ) Delete (flags ... Layout ) (Layout , error ) {
178
+ r := * l
161
179
162
180
for _ , flag := range flags {
163
181
if ! flag .IsValid () {
164
- return * f , fmt .Errorf ("the %d is invalid flag value" , flag )
182
+ return * l , fmt .Errorf ("the %d is invalid flag value" , flag )
165
183
}
166
184
167
185
if ok , _ := r .Contains (flag ); ok {
168
186
r -= Layout (flag )
169
187
}
170
188
}
171
189
172
- * f = r
173
- return * f , nil
190
+ * l = r
191
+ return * l , nil
174
192
}
175
193
176
194
// All returns true if all of the specified flags are set.
177
- func (f * Layout ) All (flags ... Layout ) bool {
195
+ func (l * Layout ) All (flags ... Layout ) bool {
178
196
for _ , flag := range flags {
179
- if ok , _ := f .Contains (flag ); ! ok {
197
+ if ok , _ := l .Contains (flag ); ! ok {
180
198
return false
181
199
}
182
200
}
@@ -185,9 +203,9 @@ func (f *Layout) All(flags ...Layout) bool {
185
203
}
186
204
187
205
// Any returns true if at least one of the specified flags is set.
188
- func (f * Layout ) Any (flags ... Layout ) bool {
206
+ func (l * Layout ) Any (flags ... Layout ) bool {
189
207
for _ , flag := range flags {
190
- if ok , _ := f .Contains (flag ); ok {
208
+ if ok , _ := l .Contains (flag ); ok {
191
209
return true
192
210
}
193
211
}
0 commit comments