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 15package android 16 17import ( 18 "fmt" 19 "path/filepath" 20 "reflect" 21 "regexp" 22 "runtime" 23 "sort" 24 "strings" 25) 26 27// CopyOf returns a new slice that has the same contents as s. 28func CopyOf(s []string) []string { 29 return append([]string(nil), s...) 30} 31 32// JoinWithPrefix prepends the prefix to each string in the list and 33// returns them joined together with " " as separator. 34func JoinWithPrefix(strs []string, prefix string) string { 35 if len(strs) == 0 { 36 return "" 37 } 38 39 var buf strings.Builder 40 buf.WriteString(prefix) 41 buf.WriteString(strs[0]) 42 for i := 1; i < len(strs); i++ { 43 buf.WriteString(" ") 44 buf.WriteString(prefix) 45 buf.WriteString(strs[i]) 46 } 47 return buf.String() 48} 49 50// JoinWithSuffix appends the suffix to each string in the list and 51// returns them joined together with given separator. 52func JoinWithSuffix(strs []string, suffix string, separator string) string { 53 if len(strs) == 0 { 54 return "" 55 } 56 57 var buf strings.Builder 58 buf.WriteString(strs[0]) 59 buf.WriteString(suffix) 60 for i := 1; i < len(strs); i++ { 61 buf.WriteString(separator) 62 buf.WriteString(strs[i]) 63 buf.WriteString(suffix) 64 } 65 return buf.String() 66} 67 68// SorterStringKeys returns the keys of the given string-keyed map in the ascending order. 69func SortedStringKeys(m interface{}) []string { 70 v := reflect.ValueOf(m) 71 if v.Kind() != reflect.Map { 72 panic(fmt.Sprintf("%#v is not a map", m)) 73 } 74 if v.Len() == 0 { 75 return nil 76 } 77 iter := v.MapRange() 78 s := make([]string, 0, v.Len()) 79 for iter.Next() { 80 s = append(s, iter.Key().String()) 81 } 82 sort.Strings(s) 83 return s 84} 85 86// stringValues returns the values of the given string-valued map in randomized map order. 87func stringValues(m interface{}) []string { 88 v := reflect.ValueOf(m) 89 if v.Kind() != reflect.Map { 90 panic(fmt.Sprintf("%#v is not a map", m)) 91 } 92 if v.Len() == 0 { 93 return nil 94 } 95 iter := v.MapRange() 96 s := make([]string, 0, v.Len()) 97 for iter.Next() { 98 s = append(s, iter.Value().String()) 99 } 100 return s 101} 102 103// SortedStringValues returns the values of the given string-valued map in the ascending order. 104func SortedStringValues(m interface{}) []string { 105 s := stringValues(m) 106 sort.Strings(s) 107 return s 108} 109 110// SortedUniqueStringValues returns the values of the given string-valued map in the ascending order 111// with duplicates removed. 112func SortedUniqueStringValues(m interface{}) []string { 113 s := stringValues(m) 114 return SortedUniqueStrings(s) 115} 116 117// IndexList returns the index of the first occurrence of the given string in the list or -1 118func IndexList(s string, list []string) int { 119 for i, l := range list { 120 if l == s { 121 return i 122 } 123 } 124 125 return -1 126} 127 128// InList checks if the string belongs to the list 129func InList(s string, list []string) bool { 130 return IndexList(s, list) != -1 131} 132 133// Returns true if the given string s is prefixed with any string in the given prefix list. 134func HasAnyPrefix(s string, prefixList []string) bool { 135 for _, prefix := range prefixList { 136 if strings.HasPrefix(s, prefix) { 137 return true 138 } 139 } 140 return false 141} 142 143// Returns true if any string in the given list has the given substring. 144func SubstringInList(list []string, substr string) bool { 145 for _, s := range list { 146 if strings.Contains(s, substr) { 147 return true 148 } 149 } 150 return false 151} 152 153// Returns true if any string in the given list has the given prefix. 154func PrefixInList(list []string, prefix string) bool { 155 for _, s := range list { 156 if strings.HasPrefix(s, prefix) { 157 return true 158 } 159 } 160 return false 161} 162 163// Returns true if any string in the given list has the given suffix. 164func SuffixInList(list []string, suffix string) bool { 165 for _, s := range list { 166 if strings.HasSuffix(s, suffix) { 167 return true 168 } 169 } 170 return false 171} 172 173// IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element. 174func IndexListPred(pred func(s string) bool, list []string) int { 175 for i, l := range list { 176 if pred(l) { 177 return i 178 } 179 } 180 181 return -1 182} 183 184// FilterList divides the string list into two lists: one with the strings belonging 185// to the given filter list, and the other with the remaining ones 186func FilterList(list []string, filter []string) (remainder []string, filtered []string) { 187 // InList is O(n). May be worth using more efficient lookup for longer lists. 188 for _, l := range list { 189 if InList(l, filter) { 190 filtered = append(filtered, l) 191 } else { 192 remainder = append(remainder, l) 193 } 194 } 195 196 return 197} 198 199// FilterListPred returns the elements of the given list for which the predicate 200// returns true. Order is kept. 201func FilterListPred(list []string, pred func(s string) bool) (filtered []string) { 202 for _, l := range list { 203 if pred(l) { 204 filtered = append(filtered, l) 205 } 206 } 207 return 208} 209 210// RemoveListFromList removes the strings belonging to the filter list from the 211// given list and returns the result 212func RemoveListFromList(list []string, filter_out []string) (result []string) { 213 result = make([]string, 0, len(list)) 214 for _, l := range list { 215 if !InList(l, filter_out) { 216 result = append(result, l) 217 } 218 } 219 return 220} 221 222// RemoveFromList removes given string from the string list. 223func RemoveFromList(s string, list []string) (bool, []string) { 224 result := make([]string, 0, len(list)) 225 var removed bool 226 for _, item := range list { 227 if item != s { 228 result = append(result, item) 229 } else { 230 removed = true 231 } 232 } 233 return removed, result 234} 235 236// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of 237// each. It modifies the slice contents in place, and returns a subslice of the original slice. 238func FirstUniqueStrings(list []string) []string { 239 // 128 was chosen based on BenchmarkFirstUniqueStrings results. 240 if len(list) > 128 { 241 return firstUniqueStringsMap(list) 242 } 243 return firstUniqueStringsList(list) 244} 245 246func firstUniqueStringsList(list []string) []string { 247 k := 0 248outer: 249 for i := 0; i < len(list); i++ { 250 for j := 0; j < k; j++ { 251 if list[i] == list[j] { 252 continue outer 253 } 254 } 255 list[k] = list[i] 256 k++ 257 } 258 return list[:k] 259} 260 261func firstUniqueStringsMap(list []string) []string { 262 k := 0 263 seen := make(map[string]bool, len(list)) 264 for i := 0; i < len(list); i++ { 265 if seen[list[i]] { 266 continue 267 } 268 seen[list[i]] = true 269 list[k] = list[i] 270 k++ 271 } 272 return list[:k] 273} 274 275// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of 276// each. It modifies the slice contents in place, and returns a subslice of the original slice. 277func LastUniqueStrings(list []string) []string { 278 totalSkip := 0 279 for i := len(list) - 1; i >= totalSkip; i-- { 280 skip := 0 281 for j := i - 1; j >= totalSkip; j-- { 282 if list[i] == list[j] { 283 skip++ 284 } else { 285 list[j+skip] = list[j] 286 } 287 } 288 totalSkip += skip 289 } 290 return list[totalSkip:] 291} 292 293// SortedUniqueStrings returns what the name says 294func SortedUniqueStrings(list []string) []string { 295 unique := FirstUniqueStrings(list) 296 sort.Strings(unique) 297 return unique 298} 299 300// checkCalledFromInit panics if a Go package's init function is not on the 301// call stack. 302func checkCalledFromInit() { 303 for skip := 3; ; skip++ { 304 _, funcName, ok := callerName(skip) 305 if !ok { 306 panic("not called from an init func") 307 } 308 309 if funcName == "init" || strings.HasPrefix(funcName, "init·") || 310 strings.HasPrefix(funcName, "init.") { 311 return 312 } 313 } 314} 315 316// A regex to find a package path within a function name. It finds the shortest string that is 317// followed by '.' and doesn't have any '/'s left. 318var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) 319 320// callerName returns the package path and function name of the calling 321// function. The skip argument has the same meaning as the skip argument of 322// runtime.Callers. 323func callerName(skip int) (pkgPath, funcName string, ok bool) { 324 var pc [1]uintptr 325 n := runtime.Callers(skip+1, pc[:]) 326 if n != 1 { 327 return "", "", false 328 } 329 330 f := runtime.FuncForPC(pc[0]).Name() 331 s := pkgPathRe.FindStringSubmatch(f) 332 if len(s) < 3 { 333 panic(fmt.Errorf("failed to extract package path and function name from %q", f)) 334 } 335 336 return s[1], s[2], true 337} 338 339// GetNumericSdkVersion removes the first occurrence of system_ in a string, 340// which is assumed to be something like "system_1.2.3" 341func GetNumericSdkVersion(v string) string { 342 return strings.Replace(v, "system_", "", 1) 343} 344 345// copied from build/kati/strutil.go 346func substPattern(pat, repl, str string) string { 347 ps := strings.SplitN(pat, "%", 2) 348 if len(ps) != 2 { 349 if str == pat { 350 return repl 351 } 352 return str 353 } 354 in := str 355 trimmed := str 356 if ps[0] != "" { 357 trimmed = strings.TrimPrefix(in, ps[0]) 358 if trimmed == in { 359 return str 360 } 361 } 362 in = trimmed 363 if ps[1] != "" { 364 trimmed = strings.TrimSuffix(in, ps[1]) 365 if trimmed == in { 366 return str 367 } 368 } 369 370 rs := strings.SplitN(repl, "%", 2) 371 if len(rs) != 2 { 372 return repl 373 } 374 return rs[0] + trimmed + rs[1] 375} 376 377// copied from build/kati/strutil.go 378func matchPattern(pat, str string) bool { 379 i := strings.IndexByte(pat, '%') 380 if i < 0 { 381 return pat == str 382 } 383 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:]) 384} 385 386var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+") 387 388// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without 389// the file extension and the version number (e.g. "libexample"). suffix stands for the 390// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the 391// file extension after the version numbers are trimmed (e.g. ".so"). 392func SplitFileExt(name string) (string, string, string) { 393 // Extract and trim the shared lib version number if the file name ends with dot digits. 394 suffix := "" 395 matches := shlibVersionPattern.FindAllStringIndex(name, -1) 396 if len(matches) > 0 { 397 lastMatch := matches[len(matches)-1] 398 if lastMatch[1] == len(name) { 399 suffix = name[lastMatch[0]:lastMatch[1]] 400 name = name[0:lastMatch[0]] 401 } 402 } 403 404 // Extract the file name root and the file extension. 405 ext := filepath.Ext(name) 406 root := strings.TrimSuffix(name, ext) 407 suffix = ext + suffix 408 409 return root, suffix, ext 410} 411 412// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths. 413func ShardPaths(paths Paths, shardSize int) []Paths { 414 if len(paths) == 0 { 415 return nil 416 } 417 ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize) 418 for len(paths) > shardSize { 419 ret = append(ret, paths[0:shardSize]) 420 paths = paths[shardSize:] 421 } 422 if len(paths) > 0 { 423 ret = append(ret, paths) 424 } 425 return ret 426} 427 428// ShardString takes a string and returns a slice of strings where the length of each one is 429// at most shardSize. 430func ShardString(s string, shardSize int) []string { 431 if len(s) == 0 { 432 return nil 433 } 434 ret := make([]string, 0, (len(s)+shardSize-1)/shardSize) 435 for len(s) > shardSize { 436 ret = append(ret, s[0:shardSize]) 437 s = s[shardSize:] 438 } 439 if len(s) > 0 { 440 ret = append(ret, s) 441 } 442 return ret 443} 444 445// ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize 446// elements. 447func ShardStrings(s []string, shardSize int) [][]string { 448 if len(s) == 0 { 449 return nil 450 } 451 ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize) 452 for len(s) > shardSize { 453 ret = append(ret, s[0:shardSize]) 454 s = s[shardSize:] 455 } 456 if len(s) > 0 { 457 ret = append(ret, s) 458 } 459 return ret 460} 461 462// CheckDuplicate checks if there are duplicates in given string list. 463// If there are, it returns first such duplicate and true. 464func CheckDuplicate(values []string) (duplicate string, found bool) { 465 seen := make(map[string]string) 466 for _, v := range values { 467 if duplicate, found = seen[v]; found { 468 return duplicate, true 469 } 470 seen[v] = v 471 } 472 return "", false 473} 474