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