blob: 8fc159dc8758e14f64ad65498dc9f4c4cb6a02ea [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 (
18 "runtime"
19 "sort"
20 "strings"
21)
Colin Cross1f8c52b2015-06-16 16:38:17 -070022
Colin Cross454c0872019-02-15 23:03:34 -080023// CopyOf returns a new slice that has the same contents as s.
24func CopyOf(s []string) []string {
25 return append([]string(nil), s...)
26}
27
Colin Crossc0b06f12015-04-08 13:03:43 -070028func JoinWithPrefix(strs []string, prefix string) string {
29 if len(strs) == 0 {
30 return ""
31 }
32
33 if len(strs) == 1 {
34 return prefix + strs[0]
35 }
36
37 n := len(" ") * (len(strs) - 1)
38 for _, s := range strs {
39 n += len(prefix) + len(s)
40 }
41
42 ret := make([]byte, 0, n)
43 for i, s := range strs {
44 if i != 0 {
45 ret = append(ret, ' ')
46 }
47 ret = append(ret, prefix...)
48 ret = append(ret, s...)
49 }
50 return string(ret)
51}
Colin Cross9b6826f2015-04-10 15:47:33 -070052
Colin Cross1f8c52b2015-06-16 16:38:17 -070053func sortedKeys(m map[string][]string) []string {
54 s := make([]string, 0, len(m))
55 for k := range m {
56 s = append(s, k)
57 }
58 sort.Strings(s)
59 return s
60}
Dan Willemsenb1957a52016-06-23 23:44:54 -070061
Colin Crossb4330e22017-12-22 15:47:09 -080062func IndexList(s string, list []string) int {
Dan Willemsenb1957a52016-06-23 23:44:54 -070063 for i, l := range list {
64 if l == s {
65 return i
66 }
67 }
68
69 return -1
70}
71
Colin Crossb4330e22017-12-22 15:47:09 -080072func InList(s string, list []string) bool {
73 return IndexList(s, list) != -1
Dan Willemsenb1957a52016-06-23 23:44:54 -070074}
75
Colin Crossb4330e22017-12-22 15:47:09 -080076func PrefixInList(s string, list []string) bool {
Ivan Lozano5f595532017-07-13 14:46:05 -070077 for _, prefix := range list {
78 if strings.HasPrefix(s, prefix) {
79 return true
80 }
81 }
82 return false
83}
84
Colin Crossb4330e22017-12-22 15:47:09 -080085func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
86 for _, l := range list {
87 if InList(l, filter) {
88 filtered = append(filtered, l)
89 } else {
90 remainder = append(remainder, l)
91 }
92 }
93
94 return
95}
96
97func RemoveListFromList(list []string, filter_out []string) (result []string) {
98 result = make([]string, 0, len(list))
99 for _, l := range list {
100 if !InList(l, filter_out) {
101 result = append(result, l)
102 }
103 }
104 return
105}
106
107func RemoveFromList(s string, list []string) (bool, []string) {
108 i := IndexList(s, list)
Logan Chien7922ab82018-03-06 18:29:27 +0800109 if i == -1 {
Colin Crossb4330e22017-12-22 15:47:09 -0800110 return false, list
111 }
Logan Chien7922ab82018-03-06 18:29:27 +0800112
113 result := make([]string, 0, len(list)-1)
114 result = append(result, list[:i]...)
115 for _, l := range list[i+1:] {
116 if l != s {
117 result = append(result, l)
118 }
119 }
120 return true, result
Colin Crossb4330e22017-12-22 15:47:09 -0800121}
122
Colin Crossb6715442017-10-24 11:13:31 -0700123// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
124// each. It modifies the slice contents in place, and returns a subslice of the original slice.
125func FirstUniqueStrings(list []string) []string {
126 k := 0
127outer:
128 for i := 0; i < len(list); i++ {
129 for j := 0; j < k; j++ {
130 if list[i] == list[j] {
131 continue outer
132 }
133 }
134 list[k] = list[i]
135 k++
136 }
137 return list[:k]
138}
139
140// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
141// each. It modifies the slice contents in place, and returns a subslice of the original slice.
142func LastUniqueStrings(list []string) []string {
143 totalSkip := 0
144 for i := len(list) - 1; i >= totalSkip; i-- {
145 skip := 0
146 for j := i - 1; j >= totalSkip; j-- {
147 if list[i] == list[j] {
148 skip++
149 } else {
150 list[j+skip] = list[j]
151 }
152 }
153 totalSkip += skip
154 }
155 return list[totalSkip:]
156}
157
Dan Willemsenb1957a52016-06-23 23:44:54 -0700158// checkCalledFromInit panics if a Go package's init function is not on the
159// call stack.
160func checkCalledFromInit() {
161 for skip := 3; ; skip++ {
162 _, funcName, ok := callerName(skip)
163 if !ok {
164 panic("not called from an init func")
165 }
166
167 if funcName == "init" || strings.HasPrefix(funcName, "init·") {
168 return
169 }
170 }
171}
172
173// callerName returns the package path and function name of the calling
174// function. The skip argument has the same meaning as the skip argument of
175// runtime.Callers.
176func callerName(skip int) (pkgPath, funcName string, ok bool) {
177 var pc [1]uintptr
178 n := runtime.Callers(skip+1, pc[:])
179 if n != 1 {
180 return "", "", false
181 }
182
183 f := runtime.FuncForPC(pc[0])
184 fullName := f.Name()
185
186 lastDotIndex := strings.LastIndex(fullName, ".")
187 if lastDotIndex == -1 {
188 panic("unable to distinguish function name from package")
189 }
190
191 if fullName[lastDotIndex-1] == ')' {
192 // The caller is a method on some type, so it's name looks like
193 // "pkg/path.(type).method". We need to go back one dot farther to get
194 // to the package name.
195 lastDotIndex = strings.LastIndex(fullName[:lastDotIndex], ".")
196 }
197
198 pkgPath = fullName[:lastDotIndex]
199 funcName = fullName[lastDotIndex+1:]
200 ok = true
201 return
202}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900203
204func GetNumericSdkVersion(v string) string {
205 if strings.Contains(v, "system_") {
206 return strings.Replace(v, "system_", "", 1)
207 }
208 return v
209}
Jiyong Park7f67f482019-01-05 12:57:48 +0900210
211// copied from build/kati/strutil.go
212func substPattern(pat, repl, str string) string {
213 ps := strings.SplitN(pat, "%", 2)
214 if len(ps) != 2 {
215 if str == pat {
216 return repl
217 }
218 return str
219 }
220 in := str
221 trimed := str
222 if ps[0] != "" {
223 trimed = strings.TrimPrefix(in, ps[0])
224 if trimed == in {
225 return str
226 }
227 }
228 in = trimed
229 if ps[1] != "" {
230 trimed = strings.TrimSuffix(in, ps[1])
231 if trimed == in {
232 return str
233 }
234 }
235
236 rs := strings.SplitN(repl, "%", 2)
237 if len(rs) != 2 {
238 return repl
239 }
240 return rs[0] + trimed + rs[1]
241}
242
243// copied from build/kati/strutil.go
244func matchPattern(pat, str string) bool {
245 i := strings.IndexByte(pat, '%')
246 if i < 0 {
247 return pat == str
248 }
249 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
250}