Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 16 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "runtime" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | ) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 22 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame^] | 23 | // CopyOf returns a new slice that has the same contents as s. |
| 24 | func CopyOf(s []string) []string { |
| 25 | return append([]string(nil), s...) |
| 26 | } |
| 27 | |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 28 | func 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 Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 52 | |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 53 | func 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 Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 61 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 62 | func IndexList(s string, list []string) int { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 63 | for i, l := range list { |
| 64 | if l == s { |
| 65 | return i |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return -1 |
| 70 | } |
| 71 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 72 | func InList(s string, list []string) bool { |
| 73 | return IndexList(s, list) != -1 |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 76 | func PrefixInList(s string, list []string) bool { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 77 | for _, prefix := range list { |
| 78 | if strings.HasPrefix(s, prefix) { |
| 79 | return true |
| 80 | } |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 85 | func 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 | |
| 97 | func 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 | |
| 107 | func RemoveFromList(s string, list []string) (bool, []string) { |
| 108 | i := IndexList(s, list) |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 109 | if i == -1 { |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 110 | return false, list |
| 111 | } |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 112 | |
| 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 Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 123 | // 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. |
| 125 | func FirstUniqueStrings(list []string) []string { |
| 126 | k := 0 |
| 127 | outer: |
| 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. |
| 142 | func 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 Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 158 | // checkCalledFromInit panics if a Go package's init function is not on the |
| 159 | // call stack. |
| 160 | func 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. |
| 176 | func 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 Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 203 | |
| 204 | func GetNumericSdkVersion(v string) string { |
| 205 | if strings.Contains(v, "system_") { |
| 206 | return strings.Replace(v, "system_", "", 1) |
| 207 | } |
| 208 | return v |
| 209 | } |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 210 | |
| 211 | // copied from build/kati/strutil.go |
| 212 | func 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 |
| 244 | func 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 | } |