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