blob: 29bb9b106a1d0ae2ab0edbcbcf7db01bfd78df70 [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 Crossc0b06f12015-04-08 13:03:43 -070023func JoinWithPrefix(strs []string, prefix string) string {
24 if len(strs) == 0 {
25 return ""
26 }
27
28 if len(strs) == 1 {
29 return prefix + strs[0]
30 }
31
32 n := len(" ") * (len(strs) - 1)
33 for _, s := range strs {
34 n += len(prefix) + len(s)
35 }
36
37 ret := make([]byte, 0, n)
38 for i, s := range strs {
39 if i != 0 {
40 ret = append(ret, ' ')
41 }
42 ret = append(ret, prefix...)
43 ret = append(ret, s...)
44 }
45 return string(ret)
46}
Colin Cross9b6826f2015-04-10 15:47:33 -070047
Colin Cross1f8c52b2015-06-16 16:38:17 -070048func sortedKeys(m map[string][]string) []string {
49 s := make([]string, 0, len(m))
50 for k := range m {
51 s = append(s, k)
52 }
53 sort.Strings(s)
54 return s
55}
Dan Willemsenb1957a52016-06-23 23:44:54 -070056
57func indexList(s string, list []string) int {
58 for i, l := range list {
59 if l == s {
60 return i
61 }
62 }
63
64 return -1
65}
66
67func inList(s string, list []string) bool {
68 return indexList(s, list) != -1
69}
70
Ivan Lozano5f595532017-07-13 14:46:05 -070071func prefixInList(s string, list []string) bool {
72 for _, prefix := range list {
73 if strings.HasPrefix(s, prefix) {
74 return true
75 }
76 }
77 return false
78}
79
Colin Crossb6715442017-10-24 11:13:31 -070080// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
81// each. It modifies the slice contents in place, and returns a subslice of the original slice.
82func FirstUniqueStrings(list []string) []string {
83 k := 0
84outer:
85 for i := 0; i < len(list); i++ {
86 for j := 0; j < k; j++ {
87 if list[i] == list[j] {
88 continue outer
89 }
90 }
91 list[k] = list[i]
92 k++
93 }
94 return list[:k]
95}
96
97// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
98// each. It modifies the slice contents in place, and returns a subslice of the original slice.
99func LastUniqueStrings(list []string) []string {
100 totalSkip := 0
101 for i := len(list) - 1; i >= totalSkip; i-- {
102 skip := 0
103 for j := i - 1; j >= totalSkip; j-- {
104 if list[i] == list[j] {
105 skip++
106 } else {
107 list[j+skip] = list[j]
108 }
109 }
110 totalSkip += skip
111 }
112 return list[totalSkip:]
113}
114
Dan Willemsenb1957a52016-06-23 23:44:54 -0700115// checkCalledFromInit panics if a Go package's init function is not on the
116// call stack.
117func checkCalledFromInit() {
118 for skip := 3; ; skip++ {
119 _, funcName, ok := callerName(skip)
120 if !ok {
121 panic("not called from an init func")
122 }
123
124 if funcName == "init" || strings.HasPrefix(funcName, "init·") {
125 return
126 }
127 }
128}
129
130// callerName returns the package path and function name of the calling
131// function. The skip argument has the same meaning as the skip argument of
132// runtime.Callers.
133func callerName(skip int) (pkgPath, funcName string, ok bool) {
134 var pc [1]uintptr
135 n := runtime.Callers(skip+1, pc[:])
136 if n != 1 {
137 return "", "", false
138 }
139
140 f := runtime.FuncForPC(pc[0])
141 fullName := f.Name()
142
143 lastDotIndex := strings.LastIndex(fullName, ".")
144 if lastDotIndex == -1 {
145 panic("unable to distinguish function name from package")
146 }
147
148 if fullName[lastDotIndex-1] == ')' {
149 // The caller is a method on some type, so it's name looks like
150 // "pkg/path.(type).method". We need to go back one dot farther to get
151 // to the package name.
152 lastDotIndex = strings.LastIndex(fullName[:lastDotIndex], ".")
153 }
154
155 pkgPath = fullName[:lastDotIndex]
156 funcName = fullName[lastDotIndex+1:]
157 ok = true
158 return
159}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900160
161func GetNumericSdkVersion(v string) string {
162 if strings.Contains(v, "system_") {
163 return strings.Replace(v, "system_", "", 1)
164 }
165 return v
166}