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