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" |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 19 | "path/filepath" |
Inseob Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame] | 20 | "reflect" |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 21 | "regexp" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 22 | "runtime" |
| 23 | "sort" |
| 24 | "strings" |
| 25 | ) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 26 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 27 | // CopyOf returns a new slice that has the same contents as s. |
| 28 | func CopyOf(s []string) []string { |
| 29 | return append([]string(nil), s...) |
| 30 | } |
| 31 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 32 | // JoinWithPrefix prepends the prefix to each string in the list and |
| 33 | // returns them joined together with " " as separator. |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 34 | func JoinWithPrefix(strs []string, prefix string) string { |
| 35 | if len(strs) == 0 { |
| 36 | return "" |
| 37 | } |
| 38 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 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]) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 46 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 47 | return buf.String() |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 48 | } |
Colin Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 49 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 50 | // JoinWithSuffix appends the suffix to each string in the list and |
| 51 | // returns them joined together with given separator. |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 52 | func JoinWithSuffix(strs []string, suffix string, separator string) string { |
| 53 | if len(strs) == 0 { |
| 54 | return "" |
| 55 | } |
| 56 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 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) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 64 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 65 | return buf.String() |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Colin Cross | 9eb853b | 2022-02-17 11:13:37 -0800 | [diff] [blame^] | 68 | // SorterStringKeys returns the keys of the given string-keyed map in the ascending order. |
Inseob Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame] | 69 | func 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 | } |
Colin Cross | 9eb853b | 2022-02-17 11:13:37 -0800 | [diff] [blame^] | 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()) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 81 | } |
| 82 | sort.Strings(s) |
| 83 | return s |
| 84 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 85 | |
Colin Cross | 9eb853b | 2022-02-17 11:13:37 -0800 | [diff] [blame^] | 86 | // stringValues returns the values of the given string-valued map in randomized map order. |
| 87 | func 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. |
| 104 | func 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. |
| 112 | func SortedUniqueStringValues(m interface{}) []string { |
| 113 | s := stringValues(m) |
| 114 | return SortedUniqueStrings(s) |
| 115 | } |
| 116 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 117 | // IndexList returns the index of the first occurrence of the given string in the list or -1 |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 118 | func IndexList(s string, list []string) int { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 119 | for i, l := range list { |
| 120 | if l == s { |
| 121 | return i |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return -1 |
| 126 | } |
| 127 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 128 | // InList checks if the string belongs to the list |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 129 | func InList(s string, list []string) bool { |
| 130 | return IndexList(s, list) != -1 |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 133 | // Returns true if the given string s is prefixed with any string in the given prefix list. |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 134 | func HasAnyPrefix(s string, prefixList []string) bool { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 135 | for _, prefix := range prefixList { |
| 136 | if strings.HasPrefix(s, prefix) { |
| 137 | return true |
| 138 | } |
| 139 | } |
| 140 | return false |
| 141 | } |
| 142 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 143 | // Returns true if any string in the given list has the given substring. |
| 144 | func 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 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 153 | // Returns true if any string in the given list has the given prefix. |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 154 | func PrefixInList(list []string, prefix string) bool { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 155 | for _, s := range list { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 156 | if strings.HasPrefix(s, prefix) { |
| 157 | return true |
| 158 | } |
| 159 | } |
| 160 | return false |
| 161 | } |
| 162 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 163 | // Returns true if any string in the given list has the given suffix. |
| 164 | func 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 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 173 | // IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element. |
| 174 | func 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 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 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 |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 186 | func FilterList(list []string, filter []string) (remainder []string, filtered []string) { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 187 | // InList is O(n). May be worth using more efficient lookup for longer lists. |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 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 | |
Martin Stjernholm | 1461c4d | 2021-03-27 19:04:05 +0000 | [diff] [blame] | 199 | // FilterListPred returns the elements of the given list for which the predicate |
| 200 | // returns true. Order is kept. |
| 201 | func 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 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 210 | // RemoveListFromList removes the strings belonging to the filter list from the |
| 211 | // given list and returns the result |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 212 | func 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 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 222 | // RemoveFromList removes given string from the string list. |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 223 | func RemoveFromList(s string, list []string) (bool, []string) { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 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 |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 231 | } |
| 232 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 233 | return removed, result |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 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. |
| 238 | func FirstUniqueStrings(list []string) []string { |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 239 | // 128 was chosen based on BenchmarkFirstUniqueStrings results. |
| 240 | if len(list) > 128 { |
| 241 | return firstUniqueStringsMap(list) |
| 242 | } |
| 243 | return firstUniqueStringsList(list) |
| 244 | } |
| 245 | |
| 246 | func firstUniqueStringsList(list []string) []string { |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 247 | k := 0 |
| 248 | outer: |
| 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 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 261 | func 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 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 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. |
| 277 | func 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 | |
Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 293 | // SortedUniqueStrings returns what the name says |
| 294 | func SortedUniqueStrings(list []string) []string { |
| 295 | unique := FirstUniqueStrings(list) |
| 296 | sort.Strings(unique) |
| 297 | return unique |
| 298 | } |
| 299 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 300 | // checkCalledFromInit panics if a Go package's init function is not on the |
| 301 | // call stack. |
| 302 | func checkCalledFromInit() { |
| 303 | for skip := 3; ; skip++ { |
| 304 | _, funcName, ok := callerName(skip) |
| 305 | if !ok { |
| 306 | panic("not called from an init func") |
| 307 | } |
| 308 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 309 | if funcName == "init" || strings.HasPrefix(funcName, "init·") || |
| 310 | strings.HasPrefix(funcName, "init.") { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 311 | return |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 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. |
| 318 | var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) |
| 319 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 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. |
| 323 | func 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 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 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)) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 336 | return s[1], s[2], true |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 337 | } |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 338 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 339 | // GetNumericSdkVersion removes the first occurrence of system_ in a string, |
| 340 | // which is assumed to be something like "system_1.2.3" |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 341 | func GetNumericSdkVersion(v string) string { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 342 | return strings.Replace(v, "system_", "", 1) |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 343 | } |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 344 | |
| 345 | // copied from build/kati/strutil.go |
| 346 | func 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 |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 355 | trimmed := str |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 356 | if ps[0] != "" { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 357 | trimmed = strings.TrimPrefix(in, ps[0]) |
| 358 | if trimmed == in { |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 359 | return str |
| 360 | } |
| 361 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 362 | in = trimmed |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 363 | if ps[1] != "" { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 364 | trimmed = strings.TrimSuffix(in, ps[1]) |
| 365 | if trimmed == in { |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 366 | return str |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | rs := strings.SplitN(repl, "%", 2) |
| 371 | if len(rs) != 2 { |
| 372 | return repl |
| 373 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 374 | return rs[0] + trimmed + rs[1] |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // copied from build/kati/strutil.go |
| 378 | func 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 | } |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 385 | |
| 386 | var 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"). |
| 392 | func 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 | } |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 411 | |
| 412 | // ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths. |
| 413 | func 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 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 428 | // ShardString takes a string and returns a slice of strings where the length of each one is |
| 429 | // at most shardSize. |
| 430 | func 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 | |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 445 | // ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize |
| 446 | // elements. |
| 447 | func 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 | } |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 461 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 462 | // CheckDuplicate checks if there are duplicates in given string list. |
| 463 | // If there are, it returns first such duplicate and true. |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 464 | func 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 { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 468 | return duplicate, true |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 469 | } |
| 470 | seen[v] = v |
| 471 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 472 | return "", false |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 473 | } |