| 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 |  | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 32 | func JoinWithPrefix(strs []string, prefix string) string { | 
|  | 33 | if len(strs) == 0 { | 
|  | 34 | return "" | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | if len(strs) == 1 { | 
|  | 38 | return prefix + strs[0] | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | n := len(" ") * (len(strs) - 1) | 
|  | 42 | for _, s := range strs { | 
|  | 43 | n += len(prefix) + len(s) | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | ret := make([]byte, 0, n) | 
|  | 47 | for i, s := range strs { | 
|  | 48 | if i != 0 { | 
|  | 49 | ret = append(ret, ' ') | 
|  | 50 | } | 
|  | 51 | ret = append(ret, prefix...) | 
|  | 52 | ret = append(ret, s...) | 
|  | 53 | } | 
|  | 54 | return string(ret) | 
|  | 55 | } | 
| Colin Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 56 |  | 
| Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 57 | func JoinWithSuffix(strs []string, suffix string, separator string) string { | 
|  | 58 | if len(strs) == 0 { | 
|  | 59 | return "" | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | if len(strs) == 1 { | 
|  | 63 | return strs[0] + suffix | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | n := len(" ") * (len(strs) - 1) | 
|  | 67 | for _, s := range strs { | 
|  | 68 | n += len(suffix) + len(s) | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | ret := make([]byte, 0, n) | 
|  | 72 | for i, s := range strs { | 
|  | 73 | if i != 0 { | 
|  | 74 | ret = append(ret, separator...) | 
|  | 75 | } | 
|  | 76 | ret = append(ret, s...) | 
|  | 77 | ret = append(ret, suffix...) | 
|  | 78 | } | 
|  | 79 | return string(ret) | 
|  | 80 | } | 
|  | 81 |  | 
| Inseob Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame] | 82 | func SortedStringKeys(m interface{}) []string { | 
|  | 83 | v := reflect.ValueOf(m) | 
|  | 84 | if v.Kind() != reflect.Map { | 
|  | 85 | panic(fmt.Sprintf("%#v is not a map", m)) | 
|  | 86 | } | 
|  | 87 | keys := v.MapKeys() | 
|  | 88 | s := make([]string, 0, len(keys)) | 
|  | 89 | for _, key := range keys { | 
|  | 90 | s = append(s, key.String()) | 
| Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 91 | } | 
|  | 92 | sort.Strings(s) | 
|  | 93 | return s | 
|  | 94 | } | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 95 |  | 
| Jooyung Han | 0302a84 | 2019-10-30 18:43:49 +0900 | [diff] [blame] | 96 | func SortedStringMapValues(m interface{}) []string { | 
|  | 97 | v := reflect.ValueOf(m) | 
|  | 98 | if v.Kind() != reflect.Map { | 
|  | 99 | panic(fmt.Sprintf("%#v is not a map", m)) | 
|  | 100 | } | 
|  | 101 | keys := v.MapKeys() | 
|  | 102 | s := make([]string, 0, len(keys)) | 
|  | 103 | for _, key := range keys { | 
|  | 104 | s = append(s, v.MapIndex(key).String()) | 
|  | 105 | } | 
|  | 106 | sort.Strings(s) | 
|  | 107 | return s | 
|  | 108 | } | 
|  | 109 |  | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 110 | func IndexList(s string, list []string) int { | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 111 | for i, l := range list { | 
|  | 112 | if l == s { | 
|  | 113 | return i | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | return -1 | 
|  | 118 | } | 
|  | 119 |  | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 120 | func InList(s string, list []string) bool { | 
|  | 121 | return IndexList(s, list) != -1 | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 124 | // 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] | 125 | func HasAnyPrefix(s string, prefixList []string) bool { | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 126 | for _, prefix := range prefixList { | 
|  | 127 | if strings.HasPrefix(s, prefix) { | 
|  | 128 | return true | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 | return false | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | // 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] | 135 | func PrefixInList(list []string, prefix string) bool { | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 136 | for _, s := range list { | 
| Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 137 | if strings.HasPrefix(s, prefix) { | 
|  | 138 | return true | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 | return false | 
|  | 142 | } | 
|  | 143 |  | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 144 | // IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element. | 
|  | 145 | func IndexListPred(pred func(s string) bool, list []string) int { | 
|  | 146 | for i, l := range list { | 
|  | 147 | if pred(l) { | 
|  | 148 | return i | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | return -1 | 
|  | 153 | } | 
|  | 154 |  | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 155 | func FilterList(list []string, filter []string) (remainder []string, filtered []string) { | 
|  | 156 | for _, l := range list { | 
|  | 157 | if InList(l, filter) { | 
|  | 158 | filtered = append(filtered, l) | 
|  | 159 | } else { | 
|  | 160 | remainder = append(remainder, l) | 
|  | 161 | } | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | return | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | func RemoveListFromList(list []string, filter_out []string) (result []string) { | 
|  | 168 | result = make([]string, 0, len(list)) | 
|  | 169 | for _, l := range list { | 
|  | 170 | if !InList(l, filter_out) { | 
|  | 171 | result = append(result, l) | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 | return | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | func RemoveFromList(s string, list []string) (bool, []string) { | 
|  | 178 | i := IndexList(s, list) | 
| Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 179 | if i == -1 { | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 180 | return false, list | 
|  | 181 | } | 
| Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 182 |  | 
|  | 183 | result := make([]string, 0, len(list)-1) | 
|  | 184 | result = append(result, list[:i]...) | 
|  | 185 | for _, l := range list[i+1:] { | 
|  | 186 | if l != s { | 
|  | 187 | result = append(result, l) | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 | return true, result | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 193 | // FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of | 
|  | 194 | // each.  It modifies the slice contents in place, and returns a subslice of the original slice. | 
|  | 195 | func FirstUniqueStrings(list []string) []string { | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 196 | // 128 was chosen based on BenchmarkFirstUniqueStrings results. | 
|  | 197 | if len(list) > 128 { | 
|  | 198 | return firstUniqueStringsMap(list) | 
|  | 199 | } | 
|  | 200 | return firstUniqueStringsList(list) | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | func firstUniqueStringsList(list []string) []string { | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 204 | k := 0 | 
|  | 205 | outer: | 
|  | 206 | for i := 0; i < len(list); i++ { | 
|  | 207 | for j := 0; j < k; j++ { | 
|  | 208 | if list[i] == list[j] { | 
|  | 209 | continue outer | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 | list[k] = list[i] | 
|  | 213 | k++ | 
|  | 214 | } | 
|  | 215 | return list[:k] | 
|  | 216 | } | 
|  | 217 |  | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 218 | func firstUniqueStringsMap(list []string) []string { | 
|  | 219 | k := 0 | 
|  | 220 | seen := make(map[string]bool, len(list)) | 
|  | 221 | for i := 0; i < len(list); i++ { | 
|  | 222 | if seen[list[i]] { | 
|  | 223 | continue | 
|  | 224 | } | 
|  | 225 | seen[list[i]] = true | 
|  | 226 | list[k] = list[i] | 
|  | 227 | k++ | 
|  | 228 | } | 
|  | 229 | return list[:k] | 
|  | 230 | } | 
|  | 231 |  | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 232 | // LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of | 
|  | 233 | // each.  It modifies the slice contents in place, and returns a subslice of the original slice. | 
|  | 234 | func LastUniqueStrings(list []string) []string { | 
|  | 235 | totalSkip := 0 | 
|  | 236 | for i := len(list) - 1; i >= totalSkip; i-- { | 
|  | 237 | skip := 0 | 
|  | 238 | for j := i - 1; j >= totalSkip; j-- { | 
|  | 239 | if list[i] == list[j] { | 
|  | 240 | skip++ | 
|  | 241 | } else { | 
|  | 242 | list[j+skip] = list[j] | 
|  | 243 | } | 
|  | 244 | } | 
|  | 245 | totalSkip += skip | 
|  | 246 | } | 
|  | 247 | return list[totalSkip:] | 
|  | 248 | } | 
|  | 249 |  | 
| Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 250 | // SortedUniqueStrings returns what the name says | 
|  | 251 | func SortedUniqueStrings(list []string) []string { | 
|  | 252 | unique := FirstUniqueStrings(list) | 
|  | 253 | sort.Strings(unique) | 
|  | 254 | return unique | 
|  | 255 | } | 
|  | 256 |  | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 257 | // checkCalledFromInit panics if a Go package's init function is not on the | 
|  | 258 | // call stack. | 
|  | 259 | func checkCalledFromInit() { | 
|  | 260 | for skip := 3; ; skip++ { | 
|  | 261 | _, funcName, ok := callerName(skip) | 
|  | 262 | if !ok { | 
|  | 263 | panic("not called from an init func") | 
|  | 264 | } | 
|  | 265 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 266 | if funcName == "init" || strings.HasPrefix(funcName, "init·") || | 
|  | 267 | strings.HasPrefix(funcName, "init.") { | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 268 | return | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 | } | 
|  | 272 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 273 | // A regex to find a package path within a function name. It finds the shortest string that is | 
|  | 274 | // followed by '.' and doesn't have any '/'s left. | 
|  | 275 | var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) | 
|  | 276 |  | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 277 | // callerName returns the package path and function name of the calling | 
|  | 278 | // function.  The skip argument has the same meaning as the skip argument of | 
|  | 279 | // runtime.Callers. | 
|  | 280 | func callerName(skip int) (pkgPath, funcName string, ok bool) { | 
|  | 281 | var pc [1]uintptr | 
|  | 282 | n := runtime.Callers(skip+1, pc[:]) | 
|  | 283 | if n != 1 { | 
|  | 284 | return "", "", false | 
|  | 285 | } | 
|  | 286 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 287 | f := runtime.FuncForPC(pc[0]).Name() | 
|  | 288 | s := pkgPathRe.FindStringSubmatch(f) | 
|  | 289 | if len(s) < 3 { | 
|  | 290 | 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] | 291 | } | 
|  | 292 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 293 | return s[1], s[2], true | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 294 | } | 
| Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 295 |  | 
|  | 296 | func GetNumericSdkVersion(v string) string { | 
|  | 297 | if strings.Contains(v, "system_") { | 
|  | 298 | return strings.Replace(v, "system_", "", 1) | 
|  | 299 | } | 
|  | 300 | return v | 
|  | 301 | } | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 302 |  | 
|  | 303 | // copied from build/kati/strutil.go | 
|  | 304 | func substPattern(pat, repl, str string) string { | 
|  | 305 | ps := strings.SplitN(pat, "%", 2) | 
|  | 306 | if len(ps) != 2 { | 
|  | 307 | if str == pat { | 
|  | 308 | return repl | 
|  | 309 | } | 
|  | 310 | return str | 
|  | 311 | } | 
|  | 312 | in := str | 
|  | 313 | trimed := str | 
|  | 314 | if ps[0] != "" { | 
|  | 315 | trimed = strings.TrimPrefix(in, ps[0]) | 
|  | 316 | if trimed == in { | 
|  | 317 | return str | 
|  | 318 | } | 
|  | 319 | } | 
|  | 320 | in = trimed | 
|  | 321 | if ps[1] != "" { | 
|  | 322 | trimed = strings.TrimSuffix(in, ps[1]) | 
|  | 323 | if trimed == in { | 
|  | 324 | return str | 
|  | 325 | } | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | rs := strings.SplitN(repl, "%", 2) | 
|  | 329 | if len(rs) != 2 { | 
|  | 330 | return repl | 
|  | 331 | } | 
|  | 332 | return rs[0] + trimed + rs[1] | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | // copied from build/kati/strutil.go | 
|  | 336 | func matchPattern(pat, str string) bool { | 
|  | 337 | i := strings.IndexByte(pat, '%') | 
|  | 338 | if i < 0 { | 
|  | 339 | return pat == str | 
|  | 340 | } | 
|  | 341 | return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:]) | 
|  | 342 | } | 
| Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 343 |  | 
|  | 344 | var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+") | 
|  | 345 |  | 
|  | 346 | // splitFileExt splits a file name into root, suffix and ext. root stands for the file name without | 
|  | 347 | // the file extension and the version number (e.g. "libexample"). suffix stands for the | 
|  | 348 | // concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the | 
|  | 349 | // file extension after the version numbers are trimmed (e.g. ".so"). | 
|  | 350 | func SplitFileExt(name string) (string, string, string) { | 
|  | 351 | // Extract and trim the shared lib version number if the file name ends with dot digits. | 
|  | 352 | suffix := "" | 
|  | 353 | matches := shlibVersionPattern.FindAllStringIndex(name, -1) | 
|  | 354 | if len(matches) > 0 { | 
|  | 355 | lastMatch := matches[len(matches)-1] | 
|  | 356 | if lastMatch[1] == len(name) { | 
|  | 357 | suffix = name[lastMatch[0]:lastMatch[1]] | 
|  | 358 | name = name[0:lastMatch[0]] | 
|  | 359 | } | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | // Extract the file name root and the file extension. | 
|  | 363 | ext := filepath.Ext(name) | 
|  | 364 | root := strings.TrimSuffix(name, ext) | 
|  | 365 | suffix = ext + suffix | 
|  | 366 |  | 
|  | 367 | return root, suffix, ext | 
|  | 368 | } | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 369 |  | 
|  | 370 | // ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths. | 
|  | 371 | func ShardPaths(paths Paths, shardSize int) []Paths { | 
|  | 372 | if len(paths) == 0 { | 
|  | 373 | return nil | 
|  | 374 | } | 
|  | 375 | ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize) | 
|  | 376 | for len(paths) > shardSize { | 
|  | 377 | ret = append(ret, paths[0:shardSize]) | 
|  | 378 | paths = paths[shardSize:] | 
|  | 379 | } | 
|  | 380 | if len(paths) > 0 { | 
|  | 381 | ret = append(ret, paths) | 
|  | 382 | } | 
|  | 383 | return ret | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | // ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize | 
|  | 387 | // elements. | 
|  | 388 | func ShardStrings(s []string, shardSize int) [][]string { | 
|  | 389 | if len(s) == 0 { | 
|  | 390 | return nil | 
|  | 391 | } | 
|  | 392 | ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize) | 
|  | 393 | for len(s) > shardSize { | 
|  | 394 | ret = append(ret, s[0:shardSize]) | 
|  | 395 | s = s[shardSize:] | 
|  | 396 | } | 
|  | 397 | if len(s) > 0 { | 
|  | 398 | ret = append(ret, s) | 
|  | 399 | } | 
|  | 400 | return ret | 
|  | 401 | } | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 402 |  | 
|  | 403 | func CheckDuplicate(values []string) (duplicate string, found bool) { | 
|  | 404 | seen := make(map[string]string) | 
|  | 405 | for _, v := range values { | 
|  | 406 | if duplicate, found = seen[v]; found { | 
|  | 407 | return | 
|  | 408 | } | 
|  | 409 | seen[v] = v | 
|  | 410 | } | 
|  | 411 | return | 
|  | 412 | } |