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 ( |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 18 | "fmt" |
Inseob Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame^] | 19 | "reflect" |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 20 | "regexp" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 21 | "runtime" |
| 22 | "sort" |
| 23 | "strings" |
| 24 | ) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 25 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 26 | // CopyOf returns a new slice that has the same contents as s. |
| 27 | func CopyOf(s []string) []string { |
| 28 | return append([]string(nil), s...) |
| 29 | } |
| 30 | |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 31 | func 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 Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 55 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 56 | func 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 Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame^] | 81 | func 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 Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 90 | } |
| 91 | sort.Strings(s) |
| 92 | return s |
| 93 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 94 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 95 | func IndexList(s string, list []string) int { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 96 | for i, l := range list { |
| 97 | if l == s { |
| 98 | return i |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return -1 |
| 103 | } |
| 104 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 105 | func InList(s string, list []string) bool { |
| 106 | return IndexList(s, list) != -1 |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 109 | func PrefixInList(s string, list []string) bool { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 110 | for _, prefix := range list { |
| 111 | if strings.HasPrefix(s, prefix) { |
| 112 | return true |
| 113 | } |
| 114 | } |
| 115 | return false |
| 116 | } |
| 117 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 118 | func 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 | |
| 130 | func 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 | |
| 140 | func RemoveFromList(s string, list []string) (bool, []string) { |
| 141 | i := IndexList(s, list) |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 142 | if i == -1 { |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 143 | return false, list |
| 144 | } |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 145 | |
| 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 Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 156 | // 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. |
| 158 | func FirstUniqueStrings(list []string) []string { |
| 159 | k := 0 |
| 160 | outer: |
| 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. |
| 175 | func 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 Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 191 | // checkCalledFromInit panics if a Go package's init function is not on the |
| 192 | // call stack. |
| 193 | func 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 Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 200 | if funcName == "init" || strings.HasPrefix(funcName, "init·") || |
| 201 | strings.HasPrefix(funcName, "init.") { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 202 | return |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 207 | // 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. |
| 209 | var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) |
| 210 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 211 | // 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. |
| 214 | func 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 Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 221 | 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 Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 227 | return s[1], s[2], true |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 228 | } |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 229 | |
| 230 | func GetNumericSdkVersion(v string) string { |
| 231 | if strings.Contains(v, "system_") { |
| 232 | return strings.Replace(v, "system_", "", 1) |
| 233 | } |
| 234 | return v |
| 235 | } |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 236 | |
| 237 | // copied from build/kati/strutil.go |
| 238 | func 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 |
| 270 | func 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 | } |