Skip to content

Commit eaf746e

Browse files
author
vals
committed
Optimized
1 parent 6e87ac5 commit eaf746e

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

level/level.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ type Level uint8
6464

6565
// IsSingle returns true if value contains single of the available flag.
6666
func (l *Level) IsSingle() bool {
67-
return bits.OnesCount(uint(*l)) == 1 &&
68-
*l <= Level(overflowLevelValue+1)>>1
67+
return *l > 0 && bits.OnesCount(uint(*l)) == 1
6968
}
7069

7170
// Contains method returns true if value contains the specified flag.
@@ -151,12 +150,9 @@ func (l *Level) Set(flags ...Level) (Level, error) {
151150

152151
for _, flag := range flags {
153152
if !flag.IsValid() {
154-
return *l, fmt.Errorf("the %d is invalid flag value", flag)
155-
}
156-
157-
if ok, _ := r.Contains(flag); !ok {
158-
r += Level(flag)
153+
return *l, fmt.Errorf("the %d is an invalid flag value", flag)
159154
}
155+
r |= flag
160156
}
161157

162158
*l = r
@@ -171,12 +167,9 @@ func (l *Level) Add(flags ...Level) (Level, error) {
171167

172168
for _, flag := range flags {
173169
if !flag.IsValid() {
174-
return *l, fmt.Errorf("the %d is invalid flag value", flag)
175-
}
176-
177-
if ok, _ := r.Contains(flag); !ok {
178-
r += Level(flag)
170+
return *l, fmt.Errorf("the %d is an invalid flag value", flag)
179171
}
172+
r |= flag
180173
}
181174

182175
*l = r
@@ -191,12 +184,9 @@ func (l *Level) Delete(flags ...Level) (Level, error) {
191184

192185
for _, flag := range flags {
193186
if !flag.IsValid() {
194-
return *l, fmt.Errorf("the %d is invalid flag value", flag)
195-
}
196-
197-
if ok, _ := r.Contains(flag); ok {
198-
r -= Level(flag)
187+
return *l, fmt.Errorf("the %d is an invalid flag value", flag)
199188
}
189+
r &^= flag
200190
}
201191

202192
*l = r

0 commit comments

Comments
 (0)