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