blob: 47c45833b65d5d4c2b063b730bf67f87fa4ed5bb [file] [log] [blame]
Colin Crossc0b06f12015-04-08 13:03:43 -07001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Crossc0b06f12015-04-08 13:03:43 -070016
Dan Willemsenb1957a52016-06-23 23:44:54 -070017import (
Colin Cross3020fee2019-03-19 15:05:17 -070018 "fmt"
Ivan Lozano022a73b2019-09-09 20:29:31 -070019 "path/filepath"
Inseob Kim1a365c62019-06-08 15:47:51 +090020 "reflect"
Colin Cross3020fee2019-03-19 15:05:17 -070021 "regexp"
Dan Willemsenb1957a52016-06-23 23:44:54 -070022 "runtime"
23 "sort"
24 "strings"
25)
Colin Cross1f8c52b2015-06-16 16:38:17 -070026
Colin Cross454c0872019-02-15 23:03:34 -080027// CopyOf returns a new slice that has the same contents as s.
28func CopyOf(s []string) []string {
29 return append([]string(nil), s...)
30}
31
Sasha Smundak1e533922020-11-19 16:48:18 -080032// JoinWithPrefix prepends the prefix to each string in the list and
33// returns them joined together with " " as separator.
Colin Crossc0b06f12015-04-08 13:03:43 -070034func JoinWithPrefix(strs []string, prefix string) string {
35 if len(strs) == 0 {
36 return ""
37 }
38
Sasha Smundak1e533922020-11-19 16:48:18 -080039 var buf strings.Builder
40 buf.WriteString(prefix)
41 buf.WriteString(strs[0])
42 for i := 1; i < len(strs); i++ {
43 buf.WriteString(" ")
44 buf.WriteString(prefix)
45 buf.WriteString(strs[i])
Colin Crossc0b06f12015-04-08 13:03:43 -070046 }
Sasha Smundak1e533922020-11-19 16:48:18 -080047 return buf.String()
Colin Crossc0b06f12015-04-08 13:03:43 -070048}
Colin Cross9b6826f2015-04-10 15:47:33 -070049
Sasha Smundak1e533922020-11-19 16:48:18 -080050// JoinWithSuffix appends the suffix to each string in the list and
51// returns them joined together with given separator.
Inseob Kim1f086e22019-05-09 13:29:15 +090052func JoinWithSuffix(strs []string, suffix string, separator string) string {
53 if len(strs) == 0 {
54 return ""
55 }
56
Sasha Smundak1e533922020-11-19 16:48:18 -080057 var buf strings.Builder
58 buf.WriteString(strs[0])
59 buf.WriteString(suffix)
60 for i := 1; i < len(strs); i++ {
61 buf.WriteString(separator)
62 buf.WriteString(strs[i])
63 buf.WriteString(suffix)
Inseob Kim1f086e22019-05-09 13:29:15 +090064 }
Sasha Smundak1e533922020-11-19 16:48:18 -080065 return buf.String()
Inseob Kim1f086e22019-05-09 13:29:15 +090066}
67
Colin Cross9eb853b2022-02-17 11:13:37 -080068// SorterStringKeys returns the keys of the given string-keyed map in the ascending order.
Inseob Kim1a365c62019-06-08 15:47:51 +090069func SortedStringKeys(m interface{}) []string {
70 v := reflect.ValueOf(m)
71 if v.Kind() != reflect.Map {
72 panic(fmt.Sprintf("%#v is not a map", m))
73 }
Colin Cross9eb853b2022-02-17 11:13:37 -080074 if v.Len() == 0 {
75 return nil
76 }
77 iter := v.MapRange()
78 s := make([]string, 0, v.Len())
79 for iter.Next() {
80 s = append(s, iter.Key().String())
Colin Cross1f8c52b2015-06-16 16:38:17 -070081 }
82 sort.Strings(s)
83 return s
84}
Dan Willemsenb1957a52016-06-23 23:44:54 -070085
Colin Cross9eb853b2022-02-17 11:13:37 -080086// stringValues returns the values of the given string-valued map in randomized map order.
87func stringValues(m interface{}) []string {
88 v := reflect.ValueOf(m)
89 if v.Kind() != reflect.Map {
90 panic(fmt.Sprintf("%#v is not a map", m))
91 }
92 if v.Len() == 0 {
93 return nil
94 }
95 iter := v.MapRange()
96 s := make([]string, 0, v.Len())
97 for iter.Next() {
98 s = append(s, iter.Value().String())
99 }
100 return s
101}
102
103// SortedStringValues returns the values of the given string-valued map in the ascending order.
104func SortedStringValues(m interface{}) []string {
105 s := stringValues(m)
106 sort.Strings(s)
107 return s
108}
109
110// SortedUniqueStringValues returns the values of the given string-valued map in the ascending order
111// with duplicates removed.
112func SortedUniqueStringValues(m interface{}) []string {
113 s := stringValues(m)
114 return SortedUniqueStrings(s)
115}
116
Sasha Smundak1e533922020-11-19 16:48:18 -0800117// IndexList returns the index of the first occurrence of the given string in the list or -1
Colin Crossb4330e22017-12-22 15:47:09 -0800118func IndexList(s string, list []string) int {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700119 for i, l := range list {
120 if l == s {
121 return i
122 }
123 }
124
125 return -1
126}
127
Sasha Smundak1e533922020-11-19 16:48:18 -0800128// InList checks if the string belongs to the list
Colin Crossb4330e22017-12-22 15:47:09 -0800129func InList(s string, list []string) bool {
130 return IndexList(s, list) != -1
Dan Willemsenb1957a52016-06-23 23:44:54 -0700131}
132
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800133// Returns true if the given string s is prefixed with any string in the given prefix list.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800134func HasAnyPrefix(s string, prefixList []string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800135 for _, prefix := range prefixList {
136 if strings.HasPrefix(s, prefix) {
137 return true
138 }
139 }
140 return false
141}
142
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800143// Returns true if any string in the given list has the given substring.
144func SubstringInList(list []string, substr string) bool {
145 for _, s := range list {
146 if strings.Contains(s, substr) {
147 return true
148 }
149 }
150 return false
151}
152
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800153// Returns true if any string in the given list has the given prefix.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800154func PrefixInList(list []string, prefix string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800155 for _, s := range list {
Ivan Lozano5f595532017-07-13 14:46:05 -0700156 if strings.HasPrefix(s, prefix) {
157 return true
158 }
159 }
160 return false
161}
162
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400163// Returns true if any string in the given list has the given suffix.
164func SuffixInList(list []string, suffix string) bool {
165 for _, s := range list {
166 if strings.HasSuffix(s, suffix) {
167 return true
168 }
169 }
170 return false
171}
172
Jooyung Han12df5fb2019-07-11 16:18:47 +0900173// IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element.
174func IndexListPred(pred func(s string) bool, list []string) int {
175 for i, l := range list {
176 if pred(l) {
177 return i
178 }
179 }
180
181 return -1
182}
183
Sasha Smundak1e533922020-11-19 16:48:18 -0800184// FilterList divides the string list into two lists: one with the strings belonging
185// to the given filter list, and the other with the remaining ones
Colin Crossb4330e22017-12-22 15:47:09 -0800186func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800187 // InList is O(n). May be worth using more efficient lookup for longer lists.
Colin Crossb4330e22017-12-22 15:47:09 -0800188 for _, l := range list {
189 if InList(l, filter) {
190 filtered = append(filtered, l)
191 } else {
192 remainder = append(remainder, l)
193 }
194 }
195
196 return
197}
198
Martin Stjernholm1461c4d2021-03-27 19:04:05 +0000199// FilterListPred returns the elements of the given list for which the predicate
200// returns true. Order is kept.
201func FilterListPred(list []string, pred func(s string) bool) (filtered []string) {
202 for _, l := range list {
203 if pred(l) {
204 filtered = append(filtered, l)
205 }
206 }
207 return
208}
209
Sasha Smundak1e533922020-11-19 16:48:18 -0800210// RemoveListFromList removes the strings belonging to the filter list from the
211// given list and returns the result
Colin Crossb4330e22017-12-22 15:47:09 -0800212func RemoveListFromList(list []string, filter_out []string) (result []string) {
213 result = make([]string, 0, len(list))
214 for _, l := range list {
215 if !InList(l, filter_out) {
216 result = append(result, l)
217 }
218 }
219 return
220}
221
Sasha Smundak1e533922020-11-19 16:48:18 -0800222// RemoveFromList removes given string from the string list.
Colin Crossb4330e22017-12-22 15:47:09 -0800223func RemoveFromList(s string, list []string) (bool, []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800224 result := make([]string, 0, len(list))
225 var removed bool
226 for _, item := range list {
227 if item != s {
228 result = append(result, item)
229 } else {
230 removed = true
Logan Chien7922ab82018-03-06 18:29:27 +0800231 }
232 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800233 return removed, result
Colin Crossb4330e22017-12-22 15:47:09 -0800234}
235
Colin Crossb6715442017-10-24 11:13:31 -0700236// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
237// each. It modifies the slice contents in place, and returns a subslice of the original slice.
238func FirstUniqueStrings(list []string) []string {
Colin Cross27027c72020-02-28 15:34:17 -0800239 // 128 was chosen based on BenchmarkFirstUniqueStrings results.
240 if len(list) > 128 {
241 return firstUniqueStringsMap(list)
242 }
243 return firstUniqueStringsList(list)
244}
245
246func firstUniqueStringsList(list []string) []string {
Colin Crossb6715442017-10-24 11:13:31 -0700247 k := 0
248outer:
249 for i := 0; i < len(list); i++ {
250 for j := 0; j < k; j++ {
251 if list[i] == list[j] {
252 continue outer
253 }
254 }
255 list[k] = list[i]
256 k++
257 }
258 return list[:k]
259}
260
Colin Cross27027c72020-02-28 15:34:17 -0800261func firstUniqueStringsMap(list []string) []string {
262 k := 0
263 seen := make(map[string]bool, len(list))
264 for i := 0; i < len(list); i++ {
265 if seen[list[i]] {
266 continue
267 }
268 seen[list[i]] = true
269 list[k] = list[i]
270 k++
271 }
272 return list[:k]
273}
274
Colin Crossb6715442017-10-24 11:13:31 -0700275// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
276// each. It modifies the slice contents in place, and returns a subslice of the original slice.
277func LastUniqueStrings(list []string) []string {
278 totalSkip := 0
279 for i := len(list) - 1; i >= totalSkip; i-- {
280 skip := 0
281 for j := i - 1; j >= totalSkip; j-- {
282 if list[i] == list[j] {
283 skip++
284 } else {
285 list[j+skip] = list[j]
286 }
287 }
288 totalSkip += skip
289 }
290 return list[totalSkip:]
291}
292
Jooyung Hane1633032019-08-01 17:41:43 +0900293// SortedUniqueStrings returns what the name says
294func SortedUniqueStrings(list []string) []string {
295 unique := FirstUniqueStrings(list)
296 sort.Strings(unique)
297 return unique
298}
299
Dan Willemsenb1957a52016-06-23 23:44:54 -0700300// checkCalledFromInit panics if a Go package's init function is not on the
301// call stack.
302func checkCalledFromInit() {
303 for skip := 3; ; skip++ {
304 _, funcName, ok := callerName(skip)
305 if !ok {
306 panic("not called from an init func")
307 }
308
Colin Cross3020fee2019-03-19 15:05:17 -0700309 if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
310 strings.HasPrefix(funcName, "init.") {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700311 return
312 }
313 }
314}
315
Colin Cross3020fee2019-03-19 15:05:17 -0700316// A regex to find a package path within a function name. It finds the shortest string that is
317// followed by '.' and doesn't have any '/'s left.
318var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
319
Dan Willemsenb1957a52016-06-23 23:44:54 -0700320// callerName returns the package path and function name of the calling
321// function. The skip argument has the same meaning as the skip argument of
322// runtime.Callers.
323func callerName(skip int) (pkgPath, funcName string, ok bool) {
324 var pc [1]uintptr
325 n := runtime.Callers(skip+1, pc[:])
326 if n != 1 {
327 return "", "", false
328 }
329
Colin Cross3020fee2019-03-19 15:05:17 -0700330 f := runtime.FuncForPC(pc[0]).Name()
331 s := pkgPathRe.FindStringSubmatch(f)
332 if len(s) < 3 {
333 panic(fmt.Errorf("failed to extract package path and function name from %q", f))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700334 }
335
Colin Cross3020fee2019-03-19 15:05:17 -0700336 return s[1], s[2], true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700337}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900338
Sasha Smundak1e533922020-11-19 16:48:18 -0800339// GetNumericSdkVersion removes the first occurrence of system_ in a string,
340// which is assumed to be something like "system_1.2.3"
Sundong Ahn0926fae2017-10-17 16:34:51 +0900341func GetNumericSdkVersion(v string) string {
Sasha Smundak1e533922020-11-19 16:48:18 -0800342 return strings.Replace(v, "system_", "", 1)
Sundong Ahn0926fae2017-10-17 16:34:51 +0900343}
Jiyong Park7f67f482019-01-05 12:57:48 +0900344
345// copied from build/kati/strutil.go
346func substPattern(pat, repl, str string) string {
347 ps := strings.SplitN(pat, "%", 2)
348 if len(ps) != 2 {
349 if str == pat {
350 return repl
351 }
352 return str
353 }
354 in := str
Sasha Smundak1e533922020-11-19 16:48:18 -0800355 trimmed := str
Jiyong Park7f67f482019-01-05 12:57:48 +0900356 if ps[0] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800357 trimmed = strings.TrimPrefix(in, ps[0])
358 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900359 return str
360 }
361 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800362 in = trimmed
Jiyong Park7f67f482019-01-05 12:57:48 +0900363 if ps[1] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800364 trimmed = strings.TrimSuffix(in, ps[1])
365 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900366 return str
367 }
368 }
369
370 rs := strings.SplitN(repl, "%", 2)
371 if len(rs) != 2 {
372 return repl
373 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800374 return rs[0] + trimmed + rs[1]
Jiyong Park7f67f482019-01-05 12:57:48 +0900375}
376
377// copied from build/kati/strutil.go
378func matchPattern(pat, str string) bool {
379 i := strings.IndexByte(pat, '%')
380 if i < 0 {
381 return pat == str
382 }
383 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
384}
Ivan Lozano022a73b2019-09-09 20:29:31 -0700385
386var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
387
388// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
389// the file extension and the version number (e.g. "libexample"). suffix stands for the
390// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the
391// file extension after the version numbers are trimmed (e.g. ".so").
392func SplitFileExt(name string) (string, string, string) {
393 // Extract and trim the shared lib version number if the file name ends with dot digits.
394 suffix := ""
395 matches := shlibVersionPattern.FindAllStringIndex(name, -1)
396 if len(matches) > 0 {
397 lastMatch := matches[len(matches)-1]
398 if lastMatch[1] == len(name) {
399 suffix = name[lastMatch[0]:lastMatch[1]]
400 name = name[0:lastMatch[0]]
401 }
402 }
403
404 // Extract the file name root and the file extension.
405 ext := filepath.Ext(name)
406 root := strings.TrimSuffix(name, ext)
407 suffix = ext + suffix
408
409 return root, suffix, ext
410}
Colin Cross0a2f7192019-09-23 14:33:09 -0700411
412// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths.
413func ShardPaths(paths Paths, shardSize int) []Paths {
414 if len(paths) == 0 {
415 return nil
416 }
417 ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize)
418 for len(paths) > shardSize {
419 ret = append(ret, paths[0:shardSize])
420 paths = paths[shardSize:]
421 }
422 if len(paths) > 0 {
423 ret = append(ret, paths)
424 }
425 return ret
426}
427
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100428// ShardString takes a string and returns a slice of strings where the length of each one is
429// at most shardSize.
430func ShardString(s string, shardSize int) []string {
431 if len(s) == 0 {
432 return nil
433 }
434 ret := make([]string, 0, (len(s)+shardSize-1)/shardSize)
435 for len(s) > shardSize {
436 ret = append(ret, s[0:shardSize])
437 s = s[shardSize:]
438 }
439 if len(s) > 0 {
440 ret = append(ret, s)
441 }
442 return ret
443}
444
Colin Cross0a2f7192019-09-23 14:33:09 -0700445// ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize
446// elements.
447func ShardStrings(s []string, shardSize int) [][]string {
448 if len(s) == 0 {
449 return nil
450 }
451 ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize)
452 for len(s) > shardSize {
453 ret = append(ret, s[0:shardSize])
454 s = s[shardSize:]
455 }
456 if len(s) > 0 {
457 ret = append(ret, s)
458 }
459 return ret
460}
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700461
Sasha Smundak1e533922020-11-19 16:48:18 -0800462// CheckDuplicate checks if there are duplicates in given string list.
463// If there are, it returns first such duplicate and true.
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700464func CheckDuplicate(values []string) (duplicate string, found bool) {
465 seen := make(map[string]string)
466 for _, v := range values {
467 if duplicate, found = seen[v]; found {
Sasha Smundak1e533922020-11-19 16:48:18 -0800468 return duplicate, true
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700469 }
470 seen[v] = v
471 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800472 return "", false
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700473}