2017-07-26 23:20:19 +00:00
|
|
|
// Copyright 2017 clair authors
|
|
|
|
//
|
|
|
|
// 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 strutil
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
import (
|
|
|
|
"regexp"
|
2017-07-26 23:20:19 +00:00
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
set "github.com/deckarep/golang-set"
|
|
|
|
)
|
|
|
|
|
|
|
|
var urlParametersRegexp = regexp.MustCompile(`(\?|\&)([^=]+)\=([^ &]+)`)
|
2017-07-26 23:20:19 +00:00
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
func convertToSet(X []string) set.Set {
|
|
|
|
s := set.NewSet()
|
2017-07-26 23:20:19 +00:00
|
|
|
for _, x := range X {
|
2018-09-20 19:57:06 +00:00
|
|
|
s.Add(x)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2017-07-26 23:20:19 +00:00
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
func setToStringSlice(s set.Set) []string {
|
|
|
|
strs := make([]string, 0, s.Cardinality())
|
|
|
|
for _, str := range s.ToSlice() {
|
|
|
|
strs = append(strs, str.(string))
|
2017-07-26 23:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
return strs
|
2017-07-26 23:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
// Difference returns the strings that are present in X but not in Y.
|
|
|
|
func Difference(X, Y []string) []string {
|
|
|
|
x := convertToSet(X)
|
|
|
|
y := convertToSet(Y)
|
|
|
|
return setToStringSlice(x.Difference(y))
|
|
|
|
}
|
2017-07-26 23:20:19 +00:00
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
// Intersect returns the strings that are present in both X and Y.
|
|
|
|
func Intersect(X, Y []string) []string {
|
|
|
|
x := convertToSet(X)
|
|
|
|
y := convertToSet(Y)
|
|
|
|
return setToStringSlice(x.Intersect(y))
|
|
|
|
}
|
2017-07-26 23:20:19 +00:00
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
// CleanURL removes all parameters from an URL.
|
|
|
|
func CleanURL(str string) string {
|
|
|
|
return urlParametersRegexp.ReplaceAllString(str, "")
|
2017-07-26 23:20:19 +00:00
|
|
|
}
|