-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve utils of slices #22379
Merged
Merged
Improve utils of slices #22379
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
972e934
chore: use IsStringInSlice
wolfogre 2e8a263
feat: IsInSlice
wolfogre 7dac83b
feat: replace IsInt64InSlice
wolfogre 6cb8d32
feat: new IsEqualSlice
wolfogre 8f3f1ce
repalce: IsSliceInt64Eq
wolfogre 273f32e
chore: add comment
wolfogre 7136481
chore: conbine files to slice.go
wolfogre d2d83e4
feat: RemoveFromSlice
wolfogre ffc758f
chore: replace RemoveIDFromList
wolfogre 1bf153e
test: add tests
wolfogre 7fdd874
chore: add comments
wolfogre 5c4f408
Merge branch 'main' into feature/improve_compare
wolfogre 9d3c9bc
fix: typo
wolfogre 7fc64c2
fix: remove checking
wolfogre b7b6cee
fix: treat nil and [] as equal slices
wolfogre 333a2cc
Merge branch 'main' into feature/improve_compare
wolfogre 22ffe41
Merge branch 'main' into feature/improve_compare
wolfogre 0879162
chore: rename to SliceContains
wolfogre 7caaa15
fix: SliceContainsFunc[T any]
wolfogre 62fc8ad
chore: rename to SliceContainsString
wolfogre 3a16168
chore: update func signatures
wolfogre a65b857
chore: SliceSortedEqual
wolfogre 45b1f72
chore: SliceRemoveAll
wolfogre 3590d77
test: more cases
wolfogre 3da09d8
Merge branch 'main' into feature/improve_compare
wolfogre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,17 +1,76 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Most of the functions in this file can have better implementations with "golang.org/x/exp/slices". | ||
// However, "golang.org/x/exp" is experimental and unreliable, we shouldn't use it. | ||
// So lets waiting for the "slices" has be promoted to the main repository one day. | ||
|
||
package util | ||
|
||
// RemoveIDFromList removes the given ID from the slice, if found. | ||
// It does not preserve order, and assumes the ID is unique. | ||
func RemoveIDFromList(list []int64, id int64) ([]int64, bool) { | ||
n := len(list) - 1 | ||
for i, item := range list { | ||
if item == id { | ||
list[i] = list[n] | ||
return list[:n], true | ||
import "strings" | ||
|
||
// IsInSlice returns true if the target exists in the slice. | ||
func IsInSlice[T comparable](target T, slice []T) bool { | ||
return IsInSliceFunc(func(t T) bool { return t == target }, slice) | ||
} | ||
|
||
// IsInSliceFunc returns true if any element in the slice satisfies the targetFunc. | ||
func IsInSliceFunc[T comparable](targetFunc func(T) bool, slice []T) bool { | ||
for _, v := range slice { | ||
if targetFunc(v) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// IsStringInSlice sequential searches if string exists in slice. | ||
func IsStringInSlice(target string, slice []string, insensitive ...bool) bool { | ||
if len(insensitive) != 0 && insensitive[0] { | ||
target = strings.ToLower(target) | ||
return IsInSliceFunc(func(t string) bool { return strings.ToLower(t) == target }, slice) | ||
} | ||
|
||
return IsInSlice(target, slice) | ||
} | ||
|
||
// IsEqualSlice returns true if slices are equal. | ||
// Be careful, two slice which have the same elements but different order are considered equal here. | ||
func IsEqualSlice[T comparable](target, source []T) bool { | ||
if len(target) != len(source) { | ||
return false | ||
} | ||
|
||
counts := make(map[T]int, len(target)) | ||
for _, v := range target { | ||
counts[v]++ | ||
} | ||
for _, v := range source { | ||
counts[v]-- | ||
} | ||
|
||
for _, v := range counts { | ||
if v != 0 { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// RemoveFromSlice removes all the target elements from the slice. | ||
func RemoveFromSlice[T comparable](target T, slice []T) []T { | ||
return RemoveFromSliceFunc(func(t T) bool { return t == target }, slice) | ||
} | ||
|
||
// RemoveFromSliceFunc removes all elements which satisfy the targetFunc from the slice. | ||
func RemoveFromSliceFunc[T comparable](targetFunc func(T) bool, slice []T) []T { | ||
idx := 0 | ||
for _, v := range slice { | ||
if targetFunc(v) { | ||
continue | ||
} | ||
slice[idx] = v | ||
idx++ | ||
} | ||
return list, false | ||
return slice[:idx] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have this loop (at least) twice: Once here, and once in
models/user.go#143-157
.Time for a common function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, they are different:
WhitelistTeamIDs/ApprovalsWhitelistTeamIDs/MergeWhitelistTeamIDs
vsWhitelistUserIDs/ApprovalsWhitelistUserIDs/MergeWhitelistUserIDs
, I have no idea how to write a common function which has less lines.Anyway, I think it's out of scope to improve this logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer to keep the code: