blob: 97bec102fc5bc431e8624823dc9d19a7cb04ba10 [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"
Inseob Kim1a365c62019-06-08 15:47:51 +090019 "reflect"
Colin Cross3020fee2019-03-19 15:05:17 -070020 "regexp"
Dan Willemsenb1957a52016-06-23 23:44:54 -070021 "runtime"
22 "sort"
23 "strings"
24)
Colin Cross1f8c52b2015-06-16 16:38:17 -070025
Colin Cross454c0872019-02-15 23:03:34 -080026// CopyOf returns a new slice that has the same contents as s.
27func CopyOf(s []string) []string {
28 return append([]string(nil), s...)
29}
30
Colin Crossc0b06f12015-04-08 13:03:43 -070031func JoinWithPrefix(strs []string, prefix string) string {
32 if len(strs) == 0 {
33 return ""
34 }
35
36 if len(strs) == 1 {
37 return prefix + strs[0]
38 }
39
40 n := len(" ") * (len(strs) - 1)
41 for _, s := range strs {
42 n += len(prefix) + len(s)
43 }
44
45 ret := make([]byte, 0, n)
46 for i, s := range strs {
47 if i != 0 {
48 ret = append(ret, ' ')
49 }
50 ret = append(ret, prefix...)
51 ret = append(ret, s...)
52 }
53 return string(ret)
54}
Colin Cross9b6826f2015-04-10 15:47:33 -070055
Inseob Kim1f086e22019-05-09 13:29:15 +090056func JoinWithSuffix(strs []string, suffix string, separator string) string {
57 if len(strs) == 0 {
58 return ""
59 }
60
61 if len(strs) == 1 {
62 return strs[0] + suffix
63 }
64
65 n := len(" ") * (len(strs) - 1)
66 for _, s := range strs {
67 n += len(suffix) + len(s)
68 }
69
70 ret := make([]byte, 0, n)
71 for i, s := range strs {
72 if i != 0 {
73 ret = append(ret, separator...)
74 }
75 ret = append(ret, s...)
76 ret = append(ret, suffix...)
77 }
78 return string(ret)
79}
80
Inseob Kim1a365c62019-06-08 15:47:51 +090081func SortedStringKeys(m interface{}) []string {
82 v := reflect.ValueOf(m)
83 if v.Kind() != reflect.Map {
84 panic(fmt.Sprintf("%#v is not a map", m))
85 }
86 keys := v.MapKeys()
87 s := make([]string, 0, len(keys))
88 for _, key := range keys {
89 s = append(s, key.String())
Colin Cross1f8c52b2015-06-16 16:38:17 -070090 }
91 sort.Strings(s)
92 return s
93}
Dan Willemsenb1957a52016-06-23 23:44:54 -070094
Colin Crossb4330e22017-12-22 15:47:09 -080095func IndexList(s string, list []string) int {
Dan Willemsenb1957a52016-06-23 23:44:54 -070096 for i, l := range list {
97 if l == s {
98 return i
99 }
100 }
101
102 return -1
103}
104
Colin Crossb4330e22017-12-22 15:47:09 -0800105func InList(s string, list []string) bool {
106 return IndexList(s, list) != -1
Dan Willemsenb1957a52016-06-23 23:44:54 -0700107}
108
Colin Crossb4330e22017-12-22 15:47:09 -0800109func PrefixInList(s string, list []string) bool {
Ivan Lozano5f595532017-07-13 14:46:05 -0700110 for _, prefix := range list {
111 if strings.HasPrefix(s, prefix) {
112 return true
113 }
114 }
115 return false
116}
117
Jooyung Han12df5fb2019-07-11 16:18:47 +0900118// IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element.
119func IndexListPred(pred func(s string) bool, list []string) int {
120 for i, l := range list {
121 if pred(l) {
122 return i
123 }
124 }
125
126 return -1
127}
128
Colin Crossb4330e22017-12-22 15:47:09 -0800129func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
130 for _, l := range list {
131 if InList(l, filter) {
132 filtered = append(filtered, l)
133 } else {
134 remainder = append(remainder, l)
135 }
136 }
137
138 return
139}
140
141func RemoveListFromList(list []string, filter_out []string) (result []string) {
142 result = make([]string, 0, len(list))
143 for _, l := range list {
144 if !InList(l, filter_out) {
145 result = append(result, l)
146 }
147 }
148 return
149}
150
151func RemoveFromList(s string, list []string) (bool, []string) {
152 i := IndexList(s, list)
Logan Chien7922ab82018-03-06 18:29:27 +0800153 if i == -1 {
Colin Crossb4330e22017-12-22 15:47:09 -0800154 return false, list
155 }
Logan Chien7922ab82018-03-06 18:29:27 +0800156
157 result := make([]string, 0, len(list)-1)
158 result = append(result, list[:i]...)
159 for _, l := range list[i+1:] {
160 if l != s {
161 result = append(result, l)
162 }
163 }
164 return true, result
Colin Crossb4330e22017-12-22 15:47:09 -0800165}
166
Colin Crossb6715442017-10-24 11:13:31 -0700167// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
168// each. It modifies the slice contents in place, and returns a subslice of the original slice.
169func FirstUniqueStrings(list []string) []string {
170 k := 0
171outer:
172 for i := 0; i < len(list); i++ {
173 for j := 0; j < k; j++ {
174 if list[i] == list[j] {
175 continue outer
176 }
177 }
178 list[k] = list[i]
179 k++
180 }
181 return list[:k]
182}
183
184// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
185// each. It modifies the slice contents in place, and returns a subslice of the original slice.
186func LastUniqueStrings(list []string) []string {
187 totalSkip := 0
188 for i := len(list) - 1; i >= totalSkip; i-- {
189 skip := 0
190 for j := i - 1; j >= totalSkip; j-- {
191 if list[i] == list[j] {
192 skip++
193 } else {
194 list[j+skip] = list[j]
195 }
196 }
197 totalSkip += skip
198 }
199 return list[totalSkip:]
200}
201
Dan Willemsenb1957a52016-06-23 23:44:54 -0700202// checkCalledFromInit panics if a Go package's init function is not on the
203// call stack.
204func checkCalledFromInit() {
205 for skip := 3; ; skip++ {
206 _, funcName, ok := callerName(skip)
207 if !ok {
208 panic("not called from an init func")
209 }
210
Colin Cross3020fee2019-03-19 15:05:17 -0700211 if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
212 strings.HasPrefix(funcName, "init.") {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700213 return
214 }
215 }
216}
217
Colin Cross3020fee2019-03-19 15:05:17 -0700218// A regex to find a package path within a function name. It finds the shortest string that is
219// followed by '.' and doesn't have any '/'s left.
220var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
221
Dan Willemsenb1957a52016-06-23 23:44:54 -0700222// callerName returns the package path and function name of the calling
223// function. The skip argument has the same meaning as the skip argument of
224// runtime.Callers.
225func callerName(skip int) (pkgPath, funcName string, ok bool) {
226 var pc [1]uintptr
227 n := runtime.Callers(skip+1, pc[:])
228 if n != 1 {
229 return "", "", false
230 }
231
Colin Cross3020fee2019-03-19 15:05:17 -0700232 f := runtime.FuncForPC(pc[0]).Name()
233 s := pkgPathRe.FindStringSubmatch(f)
234 if len(s) < 3 {
235 panic(fmt.Errorf("failed to extract package path and function name from %q", f))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700236 }
237
Colin Cross3020fee2019-03-19 15:05:17 -0700238 return s[1], s[2], true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700239}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900240
241func GetNumericSdkVersion(v string) string {
242 if strings.Contains(v, "system_") {
243 return strings.Replace(v, "system_", "", 1)
244 }
245 return v
246}
Jiyong Park7f67f482019-01-05 12:57:48 +0900247
248// copied from build/kati/strutil.go
249func substPattern(pat, repl, str string) string {
250 ps := strings.SplitN(pat, "%", 2)
251 if len(ps) != 2 {
252 if str == pat {
253 return repl
254 }
255 return str
256 }
257 in := str
258 trimed := str
259 if ps[0] != "" {
260 trimed = strings.TrimPrefix(in, ps[0])
261 if trimed == in {
262 return str
263 }
264 }
265 in = trimed
266 if ps[1] != "" {
267 trimed = strings.TrimSuffix(in, ps[1])
268 if trimed == in {
269 return str
270 }
271 }
272
273 rs := strings.SplitN(repl, "%", 2)
274 if len(rs) != 2 {
275 return repl
276 }
277 return rs[0] + trimed + rs[1]
278}
279
280// copied from build/kati/strutil.go
281func matchPattern(pat, str string) bool {
282 i := strings.IndexByte(pat, '%')
283 if i < 0 {
284 return pat == str
285 }
286 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
287}