Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 16 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 17 | import ( |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 18 | "fmt" |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 19 | "path/filepath" |
Inseob Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame] | 20 | "reflect" |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 21 | "regexp" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 22 | "runtime" |
| 23 | "sort" |
| 24 | "strings" |
Colin Cross | 31a6745 | 2023-11-02 16:57:08 -0700 | [diff] [blame^] | 25 | "sync" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 26 | ) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 27 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 28 | // CopyOf returns a new slice that has the same contents as s. |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 29 | func CopyOf[T any](s []T) []T { |
Spandan Das | cc4da76 | 2023-04-27 19:34:08 +0000 | [diff] [blame] | 30 | // If the input is nil, return nil and not an empty list |
| 31 | if s == nil { |
| 32 | return s |
| 33 | } |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 34 | return append([]T{}, s...) |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 37 | // Concat returns a new slice concatenated from the two input slices. It does not change the input |
Jiakai Zhang | 8fe3a41 | 2023-02-23 17:37:16 +0000 | [diff] [blame] | 38 | // slices. |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 39 | func Concat[T any](s1, s2 []T) []T { |
| 40 | res := make([]T, 0, len(s1)+len(s2)) |
| 41 | res = append(res, s1...) |
| 42 | res = append(res, s2...) |
Jiakai Zhang | 8fe3a41 | 2023-02-23 17:37:16 +0000 | [diff] [blame] | 43 | return res |
| 44 | } |
| 45 | |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 46 | // JoinPathsWithPrefix converts the paths to strings, prefixes them |
| 47 | // with prefix and then joins them separated by " ". |
| 48 | func JoinPathsWithPrefix(paths []Path, prefix string) string { |
| 49 | strs := make([]string, len(paths)) |
| 50 | for i := range paths { |
| 51 | strs[i] = paths[i].String() |
| 52 | } |
| 53 | return JoinWithPrefixAndSeparator(strs, prefix, " ") |
| 54 | } |
| 55 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 56 | // JoinWithPrefix prepends the prefix to each string in the list and |
| 57 | // returns them joined together with " " as separator. |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 58 | func JoinWithPrefix(strs []string, prefix string) string { |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 59 | return JoinWithPrefixAndSeparator(strs, prefix, " ") |
| 60 | } |
| 61 | |
| 62 | // JoinWithPrefixAndSeparator prepends the prefix to each string in the list and |
| 63 | // returns them joined together with the given separator. |
| 64 | func JoinWithPrefixAndSeparator(strs []string, prefix string, sep string) string { |
Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 65 | return JoinWithPrefixSuffixAndSeparator(strs, prefix, "", sep) |
| 66 | } |
| 67 | |
| 68 | // JoinWithSuffixAndSeparator appends the suffix to each string in the list and |
| 69 | // returns them joined together with the given separator. |
| 70 | func JoinWithSuffixAndSeparator(strs []string, suffix string, sep string) string { |
| 71 | return JoinWithPrefixSuffixAndSeparator(strs, "", suffix, sep) |
| 72 | } |
| 73 | |
| 74 | // JoinWithPrefixSuffixAndSeparator appends the prefix/suffix to each string in the list and |
| 75 | // returns them joined together with the given separator. |
| 76 | func JoinWithPrefixSuffixAndSeparator(strs []string, prefix, suffix, sep string) string { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 77 | if len(strs) == 0 { |
| 78 | return "" |
| 79 | } |
| 80 | |
Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 81 | // Pre-calculate the length of the result |
| 82 | length := 0 |
| 83 | for _, s := range strs { |
| 84 | length += len(s) |
| 85 | } |
| 86 | length += (len(prefix)+len(suffix))*len(strs) + len(sep)*(len(strs)-1) |
| 87 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 88 | var buf strings.Builder |
Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 89 | buf.Grow(length) |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 90 | buf.WriteString(prefix) |
| 91 | buf.WriteString(strs[0]) |
Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 92 | buf.WriteString(suffix) |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 93 | for i := 1; i < len(strs); i++ { |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 94 | buf.WriteString(sep) |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 95 | buf.WriteString(prefix) |
| 96 | buf.WriteString(strs[i]) |
Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 97 | buf.WriteString(suffix) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 98 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 99 | return buf.String() |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 100 | } |
Colin Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 101 | |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 102 | // SortedStringKeys returns the keys of the given map in the ascending order. |
| 103 | // |
| 104 | // Deprecated: Use SortedKeys instead. |
Cole Faust | 195c781 | 2023-03-01 14:21:30 -0800 | [diff] [blame] | 105 | func SortedStringKeys[V any](m map[string]V) []string { |
| 106 | return SortedKeys(m) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 107 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 108 | |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 109 | type Ordered interface { |
| 110 | ~string | |
| 111 | ~float32 | ~float64 | |
| 112 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | |
| 113 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
| 114 | } |
| 115 | |
| 116 | // SortedKeys returns the keys of the given map in the ascending order. |
| 117 | func SortedKeys[T Ordered, V any](m map[T]V) []T { |
| 118 | if len(m) == 0 { |
| 119 | return nil |
| 120 | } |
| 121 | ret := make([]T, 0, len(m)) |
| 122 | for k := range m { |
| 123 | ret = append(ret, k) |
| 124 | } |
| 125 | sort.Slice(ret, func(i, j int) bool { |
| 126 | return ret[i] < ret[j] |
| 127 | }) |
| 128 | return ret |
| 129 | } |
| 130 | |
Colin Cross | 9eb853b | 2022-02-17 11:13:37 -0800 | [diff] [blame] | 131 | // stringValues returns the values of the given string-valued map in randomized map order. |
| 132 | func stringValues(m interface{}) []string { |
| 133 | v := reflect.ValueOf(m) |
| 134 | if v.Kind() != reflect.Map { |
| 135 | panic(fmt.Sprintf("%#v is not a map", m)) |
| 136 | } |
| 137 | if v.Len() == 0 { |
| 138 | return nil |
| 139 | } |
| 140 | iter := v.MapRange() |
| 141 | s := make([]string, 0, v.Len()) |
| 142 | for iter.Next() { |
| 143 | s = append(s, iter.Value().String()) |
| 144 | } |
| 145 | return s |
| 146 | } |
| 147 | |
| 148 | // SortedStringValues returns the values of the given string-valued map in the ascending order. |
| 149 | func SortedStringValues(m interface{}) []string { |
| 150 | s := stringValues(m) |
| 151 | sort.Strings(s) |
| 152 | return s |
| 153 | } |
| 154 | |
| 155 | // SortedUniqueStringValues returns the values of the given string-valued map in the ascending order |
| 156 | // with duplicates removed. |
| 157 | func SortedUniqueStringValues(m interface{}) []string { |
| 158 | s := stringValues(m) |
| 159 | return SortedUniqueStrings(s) |
| 160 | } |
| 161 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 162 | // IndexList returns the index of the first occurrence of the given string in the list or -1 |
Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 163 | func IndexList[T comparable](t T, list []T) int { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 164 | for i, l := range list { |
Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 165 | if l == t { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 166 | return i |
| 167 | } |
| 168 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 169 | return -1 |
| 170 | } |
| 171 | |
Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 172 | func InList[T comparable](t T, list []T) bool { |
| 173 | return IndexList(t, list) != -1 |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 176 | func setFromList[T comparable](l []T) map[T]bool { |
| 177 | m := make(map[T]bool, len(l)) |
| 178 | for _, t := range l { |
| 179 | m[t] = true |
| 180 | } |
| 181 | return m |
| 182 | } |
| 183 | |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 184 | // ListSetDifference checks if the two lists contain the same elements. It returns |
| 185 | // a boolean which is true if there is a difference, and then returns lists of elements |
| 186 | // that are in l1 but not l2, and l2 but not l1. |
| 187 | func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) { |
| 188 | listsDiffer := false |
| 189 | diff1 := []T{} |
| 190 | diff2 := []T{} |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 191 | m1 := setFromList(l1) |
| 192 | m2 := setFromList(l2) |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 193 | for t := range m1 { |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 194 | if _, ok := m2[t]; !ok { |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 195 | diff1 = append(diff1, t) |
| 196 | listsDiffer = true |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 197 | } |
| 198 | } |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 199 | for t := range m2 { |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 200 | if _, ok := m1[t]; !ok { |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 201 | diff2 = append(diff2, t) |
| 202 | listsDiffer = true |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 203 | } |
| 204 | } |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 205 | return listsDiffer, diff1, diff2 |
Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 208 | // Returns true if the given string s is prefixed with any string in the given prefix list. |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 209 | func HasAnyPrefix(s string, prefixList []string) bool { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 210 | for _, prefix := range prefixList { |
| 211 | if strings.HasPrefix(s, prefix) { |
| 212 | return true |
| 213 | } |
| 214 | } |
| 215 | return false |
| 216 | } |
| 217 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 218 | // Returns true if any string in the given list has the given substring. |
| 219 | func SubstringInList(list []string, substr string) bool { |
| 220 | for _, s := range list { |
| 221 | if strings.Contains(s, substr) { |
| 222 | return true |
| 223 | } |
| 224 | } |
| 225 | return false |
| 226 | } |
| 227 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 228 | // Returns true if any string in the given list has the given prefix. |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 229 | func PrefixInList(list []string, prefix string) bool { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 230 | for _, s := range list { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 231 | if strings.HasPrefix(s, prefix) { |
| 232 | return true |
| 233 | } |
| 234 | } |
| 235 | return false |
| 236 | } |
| 237 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 238 | // Returns true if any string in the given list has the given suffix. |
| 239 | func SuffixInList(list []string, suffix string) bool { |
| 240 | for _, s := range list { |
| 241 | if strings.HasSuffix(s, suffix) { |
| 242 | return true |
| 243 | } |
| 244 | } |
| 245 | return false |
| 246 | } |
| 247 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 248 | // IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element. |
| 249 | func IndexListPred(pred func(s string) bool, list []string) int { |
| 250 | for i, l := range list { |
| 251 | if pred(l) { |
| 252 | return i |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return -1 |
| 257 | } |
| 258 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 259 | // FilterList divides the string list into two lists: one with the strings belonging |
| 260 | // to the given filter list, and the other with the remaining ones |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 261 | func FilterList(list []string, filter []string) (remainder []string, filtered []string) { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 262 | // InList is O(n). May be worth using more efficient lookup for longer lists. |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 263 | for _, l := range list { |
| 264 | if InList(l, filter) { |
| 265 | filtered = append(filtered, l) |
| 266 | } else { |
| 267 | remainder = append(remainder, l) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return |
| 272 | } |
| 273 | |
Martin Stjernholm | 1461c4d | 2021-03-27 19:04:05 +0000 | [diff] [blame] | 274 | // FilterListPred returns the elements of the given list for which the predicate |
| 275 | // returns true. Order is kept. |
| 276 | func FilterListPred(list []string, pred func(s string) bool) (filtered []string) { |
| 277 | for _, l := range list { |
| 278 | if pred(l) { |
| 279 | filtered = append(filtered, l) |
| 280 | } |
| 281 | } |
| 282 | return |
| 283 | } |
| 284 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 285 | // RemoveListFromList removes the strings belonging to the filter list from the |
| 286 | // given list and returns the result |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 287 | func RemoveListFromList(list []string, filter_out []string) (result []string) { |
| 288 | result = make([]string, 0, len(list)) |
| 289 | for _, l := range list { |
| 290 | if !InList(l, filter_out) { |
| 291 | result = append(result, l) |
| 292 | } |
| 293 | } |
| 294 | return |
| 295 | } |
| 296 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 297 | // RemoveFromList removes given string from the string list. |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 298 | func RemoveFromList(s string, list []string) (bool, []string) { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 299 | result := make([]string, 0, len(list)) |
| 300 | var removed bool |
| 301 | for _, item := range list { |
| 302 | if item != s { |
| 303 | result = append(result, item) |
| 304 | } else { |
| 305 | removed = true |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 306 | } |
| 307 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 308 | return removed, result |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 311 | // FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 312 | // each. It does not modify the input slice. |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 313 | func FirstUniqueStrings(list []string) []string { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 314 | return firstUnique(list) |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 317 | // firstUnique returns all unique elements of a slice, keeping the first copy of each. It |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 318 | // does not modify the input slice. |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 319 | func firstUnique[T comparable](slice []T) []T { |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 320 | // Do not modify the input in-place, operate on a copy instead. |
| 321 | slice = CopyOf(slice) |
| 322 | return firstUniqueInPlace(slice) |
| 323 | } |
| 324 | |
| 325 | // firstUniqueInPlace returns all unique elements of a slice, keeping the first copy of |
| 326 | // each. It modifies the slice contents in place, and returns a subslice of the original |
| 327 | // slice. |
| 328 | func firstUniqueInPlace[T comparable](slice []T) []T { |
| 329 | // 128 was chosen based on BenchmarkFirstUniqueStrings results. |
| 330 | if len(slice) > 128 { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 331 | return firstUniqueMap(slice) |
| 332 | } |
| 333 | return firstUniqueList(slice) |
| 334 | } |
| 335 | |
| 336 | // firstUniqueList is an implementation of firstUnique using an O(N^2) list comparison to look for |
| 337 | // duplicates. |
| 338 | func firstUniqueList[T any](in []T) []T { |
| 339 | writeIndex := 0 |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 340 | outer: |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 341 | for readIndex := 0; readIndex < len(in); readIndex++ { |
| 342 | for compareIndex := 0; compareIndex < writeIndex; compareIndex++ { |
| 343 | if interface{}(in[readIndex]) == interface{}(in[compareIndex]) { |
| 344 | // The value at readIndex already exists somewhere in the output region |
| 345 | // of the slice before writeIndex, skip it. |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 346 | continue outer |
| 347 | } |
| 348 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 349 | if readIndex != writeIndex { |
| 350 | in[writeIndex] = in[readIndex] |
| 351 | } |
| 352 | writeIndex++ |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 353 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 354 | return in[0:writeIndex] |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 357 | // firstUniqueMap is an implementation of firstUnique using an O(N) hash set lookup to look for |
| 358 | // duplicates. |
| 359 | func firstUniqueMap[T comparable](in []T) []T { |
| 360 | writeIndex := 0 |
| 361 | seen := make(map[T]bool, len(in)) |
| 362 | for readIndex := 0; readIndex < len(in); readIndex++ { |
| 363 | if _, exists := seen[in[readIndex]]; exists { |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 364 | continue |
| 365 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 366 | seen[in[readIndex]] = true |
| 367 | if readIndex != writeIndex { |
| 368 | in[writeIndex] = in[readIndex] |
| 369 | } |
| 370 | writeIndex++ |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 371 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 372 | return in[0:writeIndex] |
| 373 | } |
| 374 | |
Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 375 | // ReverseSliceInPlace reverses the elements of a slice in place and returns it. |
| 376 | func ReverseSliceInPlace[T any](in []T) []T { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 377 | for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 { |
| 378 | in[i], in[j] = in[j], in[i] |
| 379 | } |
Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 380 | return in |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 383 | // ReverseSlice returns a copy of a slice in reverse order. |
| 384 | func ReverseSlice[T any](in []T) []T { |
| 385 | if in == nil { |
| 386 | return in |
| 387 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 388 | out := make([]T, len(in)) |
| 389 | for i := 0; i < len(in); i++ { |
| 390 | out[i] = in[len(in)-1-i] |
| 391 | } |
| 392 | return out |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 395 | // LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of |
| 396 | // each. It modifies the slice contents in place, and returns a subslice of the original slice. |
| 397 | func LastUniqueStrings(list []string) []string { |
| 398 | totalSkip := 0 |
| 399 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 400 | skip := 0 |
| 401 | for j := i - 1; j >= totalSkip; j-- { |
| 402 | if list[i] == list[j] { |
| 403 | skip++ |
| 404 | } else { |
| 405 | list[j+skip] = list[j] |
| 406 | } |
| 407 | } |
| 408 | totalSkip += skip |
| 409 | } |
| 410 | return list[totalSkip:] |
| 411 | } |
| 412 | |
Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 413 | // SortedUniqueStrings returns what the name says |
| 414 | func SortedUniqueStrings(list []string) []string { |
Spandan Das | 8a8714c | 2023-04-25 18:03:54 +0000 | [diff] [blame] | 415 | // FirstUniqueStrings creates a copy of `list`, so the input remains untouched. |
Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 416 | unique := FirstUniqueStrings(list) |
| 417 | sort.Strings(unique) |
| 418 | return unique |
| 419 | } |
| 420 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 421 | // checkCalledFromInit panics if a Go package's init function is not on the |
| 422 | // call stack. |
| 423 | func checkCalledFromInit() { |
| 424 | for skip := 3; ; skip++ { |
| 425 | _, funcName, ok := callerName(skip) |
| 426 | if !ok { |
| 427 | panic("not called from an init func") |
| 428 | } |
| 429 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 430 | if funcName == "init" || strings.HasPrefix(funcName, "init·") || |
| 431 | strings.HasPrefix(funcName, "init.") { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 432 | return |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 437 | // A regex to find a package path within a function name. It finds the shortest string that is |
| 438 | // followed by '.' and doesn't have any '/'s left. |
| 439 | var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) |
| 440 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 441 | // callerName returns the package path and function name of the calling |
| 442 | // function. The skip argument has the same meaning as the skip argument of |
| 443 | // runtime.Callers. |
| 444 | func callerName(skip int) (pkgPath, funcName string, ok bool) { |
| 445 | var pc [1]uintptr |
| 446 | n := runtime.Callers(skip+1, pc[:]) |
| 447 | if n != 1 { |
| 448 | return "", "", false |
| 449 | } |
| 450 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 451 | f := runtime.FuncForPC(pc[0]).Name() |
| 452 | s := pkgPathRe.FindStringSubmatch(f) |
| 453 | if len(s) < 3 { |
| 454 | panic(fmt.Errorf("failed to extract package path and function name from %q", f)) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 457 | return s[1], s[2], true |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 458 | } |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 459 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 460 | // GetNumericSdkVersion removes the first occurrence of system_ in a string, |
| 461 | // which is assumed to be something like "system_1.2.3" |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 462 | func GetNumericSdkVersion(v string) string { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 463 | return strings.Replace(v, "system_", "", 1) |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 464 | } |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 465 | |
| 466 | // copied from build/kati/strutil.go |
| 467 | func substPattern(pat, repl, str string) string { |
| 468 | ps := strings.SplitN(pat, "%", 2) |
| 469 | if len(ps) != 2 { |
| 470 | if str == pat { |
| 471 | return repl |
| 472 | } |
| 473 | return str |
| 474 | } |
| 475 | in := str |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 476 | trimmed := str |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 477 | if ps[0] != "" { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 478 | trimmed = strings.TrimPrefix(in, ps[0]) |
| 479 | if trimmed == in { |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 480 | return str |
| 481 | } |
| 482 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 483 | in = trimmed |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 484 | if ps[1] != "" { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 485 | trimmed = strings.TrimSuffix(in, ps[1]) |
| 486 | if trimmed == in { |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 487 | return str |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | rs := strings.SplitN(repl, "%", 2) |
| 492 | if len(rs) != 2 { |
| 493 | return repl |
| 494 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 495 | return rs[0] + trimmed + rs[1] |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // copied from build/kati/strutil.go |
| 499 | func matchPattern(pat, str string) bool { |
| 500 | i := strings.IndexByte(pat, '%') |
| 501 | if i < 0 { |
| 502 | return pat == str |
| 503 | } |
| 504 | return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:]) |
| 505 | } |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 506 | |
| 507 | var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+") |
| 508 | |
| 509 | // splitFileExt splits a file name into root, suffix and ext. root stands for the file name without |
| 510 | // the file extension and the version number (e.g. "libexample"). suffix stands for the |
| 511 | // concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the |
| 512 | // file extension after the version numbers are trimmed (e.g. ".so"). |
| 513 | func SplitFileExt(name string) (string, string, string) { |
| 514 | // Extract and trim the shared lib version number if the file name ends with dot digits. |
| 515 | suffix := "" |
| 516 | matches := shlibVersionPattern.FindAllStringIndex(name, -1) |
| 517 | if len(matches) > 0 { |
| 518 | lastMatch := matches[len(matches)-1] |
| 519 | if lastMatch[1] == len(name) { |
| 520 | suffix = name[lastMatch[0]:lastMatch[1]] |
| 521 | name = name[0:lastMatch[0]] |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Extract the file name root and the file extension. |
| 526 | ext := filepath.Ext(name) |
| 527 | root := strings.TrimSuffix(name, ext) |
| 528 | suffix = ext + suffix |
| 529 | |
| 530 | return root, suffix, ext |
| 531 | } |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 532 | |
| 533 | // ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths. |
| 534 | func ShardPaths(paths Paths, shardSize int) []Paths { |
| 535 | if len(paths) == 0 { |
| 536 | return nil |
| 537 | } |
| 538 | ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize) |
| 539 | for len(paths) > shardSize { |
| 540 | ret = append(ret, paths[0:shardSize]) |
| 541 | paths = paths[shardSize:] |
| 542 | } |
| 543 | if len(paths) > 0 { |
| 544 | ret = append(ret, paths) |
| 545 | } |
| 546 | return ret |
| 547 | } |
| 548 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 549 | // ShardString takes a string and returns a slice of strings where the length of each one is |
| 550 | // at most shardSize. |
| 551 | func ShardString(s string, shardSize int) []string { |
| 552 | if len(s) == 0 { |
| 553 | return nil |
| 554 | } |
| 555 | ret := make([]string, 0, (len(s)+shardSize-1)/shardSize) |
| 556 | for len(s) > shardSize { |
| 557 | ret = append(ret, s[0:shardSize]) |
| 558 | s = s[shardSize:] |
| 559 | } |
| 560 | if len(s) > 0 { |
| 561 | ret = append(ret, s) |
| 562 | } |
| 563 | return ret |
| 564 | } |
| 565 | |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 566 | // ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize |
| 567 | // elements. |
| 568 | func ShardStrings(s []string, shardSize int) [][]string { |
| 569 | if len(s) == 0 { |
| 570 | return nil |
| 571 | } |
| 572 | ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize) |
| 573 | for len(s) > shardSize { |
| 574 | ret = append(ret, s[0:shardSize]) |
| 575 | s = s[shardSize:] |
| 576 | } |
| 577 | if len(s) > 0 { |
| 578 | ret = append(ret, s) |
| 579 | } |
| 580 | return ret |
| 581 | } |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 582 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 583 | // CheckDuplicate checks if there are duplicates in given string list. |
| 584 | // If there are, it returns first such duplicate and true. |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 585 | func CheckDuplicate(values []string) (duplicate string, found bool) { |
| 586 | seen := make(map[string]string) |
| 587 | for _, v := range values { |
| 588 | if duplicate, found = seen[v]; found { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 589 | return duplicate, true |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 590 | } |
| 591 | seen[v] = v |
| 592 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 593 | return "", false |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 594 | } |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 595 | |
| 596 | func AddToStringSet(set map[string]bool, items []string) { |
| 597 | for _, item := range items { |
| 598 | set[item] = true |
| 599 | } |
| 600 | } |
Colin Cross | 31a6745 | 2023-11-02 16:57:08 -0700 | [diff] [blame^] | 601 | |
| 602 | // SyncMap is a wrapper around sync.Map that provides type safety via generics. |
| 603 | type SyncMap[K comparable, V any] struct { |
| 604 | sync.Map |
| 605 | } |
| 606 | |
| 607 | // Load returns the value stored in the map for a key, or the zero value if no |
| 608 | // value is present. |
| 609 | // The ok result indicates whether value was found in the map. |
| 610 | func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { |
| 611 | v, ok := m.Map.Load(key) |
| 612 | if !ok { |
| 613 | return *new(V), false |
| 614 | } |
| 615 | return v.(V), true |
| 616 | } |
| 617 | |
| 618 | // Store sets the value for a key. |
| 619 | func (m *SyncMap[K, V]) Store(key K, value V) { |
| 620 | m.Map.Store(key, value) |
| 621 | } |
| 622 | |
| 623 | // LoadOrStore returns the existing value for the key if present. |
| 624 | // Otherwise, it stores and returns the given value. |
| 625 | // The loaded result is true if the value was loaded, false if stored. |
| 626 | func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { |
| 627 | v, loaded := m.Map.LoadOrStore(key, value) |
| 628 | return v.(V), loaded |
| 629 | } |