blob: 3b8bc783d997f50d98e6e7d44af37e4da3114d63 [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
Colin Crossb4330e22017-12-22 15:47:09 -0800118func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
119 for _, l := range list {
120 if InList(l, filter) {
121 filtered = append(filtered, l)
122 } else {
123 remainder = append(remainder, l)
124 }
125 }
126
127 return
128}
129
130func RemoveListFromList(list []string, filter_out []string) (result []string) {
131 result = make([]string, 0, len(list))
132 for _, l := range list {
133 if !InList(l, filter_out) {
134 result = append(result, l)
135 }
136 }
137 return
138}
139
140func RemoveFromList(s string, list []string) (bool, []string) {
141 i := IndexList(s, list)
Logan Chien7922ab82018-03-06 18:29:27 +0800142 if i == -1 {
Colin Crossb4330e22017-12-22 15:47:09 -0800143 return false, list
144 }
Logan Chien7922ab82018-03-06 18:29:27 +0800145
146 result := make([]string, 0, len(list)-1)
147 result = append(result, list[:i]...)
148 for _, l := range list[i+1:] {
149 if l != s {
150 result = append(result, l)
151 }
152 }
153 return true, result
Colin Crossb4330e22017-12-22 15:47:09 -0800154}
155
Colin Crossb6715442017-10-24 11:13:31 -0700156// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
157// each. It modifies the slice contents in place, and returns a subslice of the original slice.
158func FirstUniqueStrings(list []string) []string {
159 k := 0
160outer:
161 for i := 0; i < len(list); i++ {
162 for j := 0; j < k; j++ {
163 if list[i] == list[j] {
164 continue outer
165 }
166 }
167 list[k] = list[i]
168 k++
169 }
170 return list[:k]
171}
172
173// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
174// each. It modifies the slice contents in place, and returns a subslice of the original slice.
175func LastUniqueStrings(list []string) []string {
176 totalSkip := 0
177 for i := len(list) - 1; i >= totalSkip; i-- {
178 skip := 0
179 for j := i - 1; j >= totalSkip; j-- {
180 if list[i] == list[j] {
181 skip++
182 } else {
183 list[j+skip] = list[j]
184 }
185 }
186 totalSkip += skip
187 }
188 return list[totalSkip:]
189}
190
Dan Willemsenb1957a52016-06-23 23:44:54 -0700191// checkCalledFromInit panics if a Go package's init function is not on the
192// call stack.
193func checkCalledFromInit() {
194 for skip := 3; ; skip++ {
195 _, funcName, ok := callerName(skip)
196 if !ok {
197 panic("not called from an init func")
198 }
199
Colin Cross3020fee2019-03-19 15:05:17 -0700200 if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
201 strings.HasPrefix(funcName, "init.") {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700202 return
203 }
204 }
205}
206
Colin Cross3020fee2019-03-19 15:05:17 -0700207// A regex to find a package path within a function name. It finds the shortest string that is
208// followed by '.' and doesn't have any '/'s left.
209var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
210
Dan Willemsenb1957a52016-06-23 23:44:54 -0700211// callerName returns the package path and function name of the calling
212// function. The skip argument has the same meaning as the skip argument of
213// runtime.Callers.
214func callerName(skip int) (pkgPath, funcName string, ok bool) {
215 var pc [1]uintptr
216 n := runtime.Callers(skip+1, pc[:])
217 if n != 1 {
218 return "", "", false
219 }
220
Colin Cross3020fee2019-03-19 15:05:17 -0700221 f := runtime.FuncForPC(pc[0]).Name()
222 s := pkgPathRe.FindStringSubmatch(f)
223 if len(s) < 3 {
224 panic(fmt.Errorf("failed to extract package path and function name from %q", f))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700225 }
226
Colin Cross3020fee2019-03-19 15:05:17 -0700227 return s[1], s[2], true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700228}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900229
230func GetNumericSdkVersion(v string) string {
231 if strings.Contains(v, "system_") {
232 return strings.Replace(v, "system_", "", 1)
233 }
234 return v
235}
Jiyong Park7f67f482019-01-05 12:57:48 +0900236
237// copied from build/kati/strutil.go
238func substPattern(pat, repl, str string) string {
239 ps := strings.SplitN(pat, "%", 2)
240 if len(ps) != 2 {
241 if str == pat {
242 return repl
243 }
244 return str
245 }
246 in := str
247 trimed := str
248 if ps[0] != "" {
249 trimed = strings.TrimPrefix(in, ps[0])
250 if trimed == in {
251 return str
252 }
253 }
254 in = trimed
255 if ps[1] != "" {
256 trimed = strings.TrimSuffix(in, ps[1])
257 if trimed == in {
258 return str
259 }
260 }
261
262 rs := strings.SplitN(repl, "%", 2)
263 if len(rs) != 2 {
264 return repl
265 }
266 return rs[0] + trimed + rs[1]
267}
268
269// copied from build/kati/strutil.go
270func matchPattern(pat, str string) bool {
271 i := strings.IndexByte(pat, '%')
272 if i < 0 {
273 return pat == str
274 }
275 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
276}