forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: imp dhcp host normalization, validation
- Loading branch information
Showing
6 changed files
with
241 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package aghstrings | ||
|
||
// unit is a convenient alias for struct{} | ||
type unit = struct{} | ||
|
||
// Set is a set of strings. | ||
type Set struct { | ||
m map[string]unit | ||
} | ||
|
||
// NewSet returns a new string set containing strs. | ||
func NewSet(strs ...string) (set *Set) { | ||
set = &Set{ | ||
m: make(map[string]unit, len(strs)), | ||
} | ||
|
||
for _, s := range strs { | ||
set.Add(s) | ||
} | ||
|
||
return set | ||
} | ||
|
||
// Add adds s to the set. Add panics if the set is a nil set, just like a nil | ||
// map does. | ||
func (set *Set) Add(s string) { | ||
set.m[s] = unit{} | ||
} | ||
|
||
// Del deletes s from the set. Calling Del on a nil set has no effect, just | ||
// like delete on an empty map doesn't. | ||
func (set *Set) Del(s string) { | ||
if set == nil { | ||
return | ||
} | ||
|
||
delete(set.m, s) | ||
} | ||
|
||
// Has returns true if s is in the set. Calling Has on a nil set returns false, | ||
// just like indexing on an empty map does. | ||
func (set *Set) Has(s string) (ok bool) { | ||
if set == nil { | ||
return false | ||
} | ||
|
||
_, ok = set.m[s] | ||
|
||
return ok | ||
} | ||
|
||
// Len returns the length of the set. A nil set has a length of zero, just like | ||
// an empty map. | ||
func (set *Set) Len() (n int) { | ||
if set == nil { | ||
return 0 | ||
} | ||
|
||
return len(set.m) | ||
} | ||
|
||
// Values returns all values in the set. The order of the values is undefined. | ||
// Values returns nil if the set is nil. | ||
func (set *Set) Values() (strs []string) { | ||
if set == nil { | ||
return nil | ||
} | ||
|
||
strs = make([]string, 0, len(set.m)) | ||
for s := range set.m { | ||
strs = append(strs, s) | ||
} | ||
|
||
return strs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package aghstrings | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSet(t *testing.T) { | ||
const s = "a" | ||
|
||
t.Run("nil", func(t *testing.T) { | ||
var set *Set | ||
|
||
assert.NotPanics(t, func() { | ||
set.Del(s) | ||
}) | ||
|
||
assert.NotPanics(t, func() { | ||
assert.False(t, set.Has(s)) | ||
}) | ||
|
||
assert.NotPanics(t, func() { | ||
assert.Equal(t, 0, set.Len()) | ||
}) | ||
|
||
assert.NotPanics(t, func() { | ||
assert.Nil(t, set.Values()) | ||
}) | ||
|
||
assert.Panics(t, func() { | ||
set.Add(s) | ||
}) | ||
}) | ||
|
||
t.Run("non_nil", func(t *testing.T) { | ||
set := NewSet() | ||
assert.Equal(t, 0, set.Len()) | ||
|
||
ok := set.Has(s) | ||
assert.False(t, ok) | ||
|
||
set.Add(s) | ||
ok = set.Has(s) | ||
assert.True(t, ok) | ||
|
||
assert.Equal(t, []string{s}, set.Values()) | ||
|
||
set.Del(s) | ||
ok = set.Has(s) | ||
assert.False(t, ok) | ||
|
||
set = NewSet(s) | ||
assert.Equal(t, 1, set.Len()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.