-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The intersects operator and its case-insensitive variant, operate on strings slices. If all elements in the RHS slice are present in the slice given by LHS, the operator evaluates to true. Otherwise, it evaluates to false.
- Loading branch information
1 parent
913b71a
commit 9df026f
Showing
5 changed files
with
124 additions
and
2 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
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
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,46 @@ | ||
/* | ||
* Copyright 2021-2022 by Nedim Sabic Sabic | ||
* https://www.fibratus.io | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package sets | ||
|
||
import "strings" | ||
|
||
// IntersectionStrings computes the intersection of two | ||
// string slices. The boolean argument specifies if the | ||
// string comparison is case-sensitive or not. | ||
func IntersectionStrings(s1, s2 []string, ignoreCase bool) []string { | ||
inter := make([]string, 0) | ||
bucket := map[string]bool{} | ||
|
||
for _, i := range s1 { | ||
for _, j := range s2 { | ||
var eq bool | ||
if ignoreCase { | ||
eq = strings.EqualFold(i, j) && !bucket[i] | ||
} else { | ||
eq = i == j && !bucket[i] | ||
} | ||
if eq { | ||
inter = append(inter, i) | ||
bucket[i] = true | ||
} | ||
} | ||
} | ||
|
||
return inter | ||
} |
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,53 @@ | ||
/* | ||
* Copyright 2021-2022 by Nedim Sabic Sabic | ||
* https://www.fibratus.io | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package sets | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestIntersectionStrings(t *testing.T) { | ||
var tests = []struct { | ||
s1 []string | ||
s2 []string | ||
ignoreCase bool | ||
in []string | ||
}{ | ||
{ | ||
[]string{"-k", "DcomLaunch", "-p", "-s", "LSM"}, []string{"DcomLaunch", "-s"}, true, []string{"DcomLaunch", "-s"}, | ||
}, | ||
{ | ||
[]string{"-k", "DcomLaunch", "-p", "-s", "LSM"}, []string{"DComLaunch", "-s"}, false, []string{"-s"}, | ||
}, | ||
{ | ||
[]string{"-k", "DcomLaunch", "-p", "-s", "LSM"}, []string{"LocalSystemNetworkRestricted"}, true, []string{}, | ||
}, | ||
{ | ||
[]string{"LSM", "-s"}, []string{"-S", "lsm"}, true, []string{"LSM", "-s"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(strings.Join(tt.in, ","), func(t *testing.T) { | ||
assert.Equal(t, tt.in, IntersectionStrings(tt.s1, tt.s2, tt.ignoreCase)) | ||
}) | ||
} | ||
} |