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