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 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 68 | // SortedIntKeys returns the keys of the given integer-keyed map in the ascending order |
| 69 | // TODO(asmundak): once Go has generics, combine this with SortedStringKeys below. |
Ulya Trafimovich | b8063c6 | 2020-08-20 11:33:12 +0100 | [diff] [blame] | 70 | func SortedIntKeys(m interface{}) []int { |
| 71 | v := reflect.ValueOf(m) |
| 72 | if v.Kind() != reflect.Map { |
| 73 | panic(fmt.Sprintf("%#v is not a map", m)) |
| 74 | } |
| 75 | keys := v.MapKeys() |
| 76 | s := make([]int, 0, len(keys)) |
| 77 | for _, key := range keys { |
| 78 | s = append(s, int(key.Int())) |
| 79 | } |
| 80 | sort.Ints(s) |
| 81 | return s |
| 82 | } |
| 83 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 84 | // 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] | 85 | func SortedStringKeys(m interface{}) []string { |
| 86 | v := reflect.ValueOf(m) |
| 87 | if v.Kind() != reflect.Map { |
| 88 | panic(fmt.Sprintf("%#v is not a map", m)) |
| 89 | } |
| 90 | keys := v.MapKeys() |
| 91 | s := make([]string, 0, len(keys)) |
| 92 | for _, key := range keys { |
| 93 | s = append(s, key.String()) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 94 | } |
| 95 | sort.Strings(s) |
| 96 | return s |
| 97 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 98 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 99 | // SortedStringMapValues returns the values of the string-values map in the ascending order |
Jooyung Han | 0302a84 | 2019-10-30 18:43:49 +0900 | [diff] [blame] | 100 | func SortedStringMapValues(m interface{}) []string { |
| 101 | v := reflect.ValueOf(m) |
| 102 | if v.Kind() != reflect.Map { |
| 103 | panic(fmt.Sprintf("%#v is not a map", m)) |
| 104 | } |
| 105 | keys := v.MapKeys() |
| 106 | s := make([]string, 0, len(keys)) |
| 107 | for _, key := range keys { |
| 108 | s = append(s, v.MapIndex(key).String()) |
| 109 | } |
| 110 | sort.Strings(s) |
| 111 | return s |
| 112 | } |
| 113 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 114 | // 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] | 115 | func IndexList(s string, list []string) int { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 116 | for i, l := range list { |
| 117 | if l == s { |
| 118 | return i |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return -1 |
| 123 | } |
| 124 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 125 | // InList checks if the string belongs to the list |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 126 | func InList(s string, list []string) bool { |
| 127 | return IndexList(s, list) != -1 |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 130 | // 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] | 131 | func HasAnyPrefix(s string, prefixList []string) bool { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 132 | for _, prefix := range prefixList { |
| 133 | if strings.HasPrefix(s, prefix) { |
| 134 | return true |
| 135 | } |
| 136 | } |
| 137 | return false |
| 138 | } |
| 139 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame^] | 140 | // Returns true if any string in the given list has the given substring. |
| 141 | func SubstringInList(list []string, substr string) bool { |
| 142 | for _, s := range list { |
| 143 | if strings.Contains(s, substr) { |
| 144 | return true |
| 145 | } |
| 146 | } |
| 147 | return false |
| 148 | } |
| 149 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 150 | // 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] | 151 | func PrefixInList(list []string, prefix string) bool { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 152 | for _, s := range list { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 153 | if strings.HasPrefix(s, prefix) { |
| 154 | return true |
| 155 | } |
| 156 | } |
| 157 | return false |
| 158 | } |
| 159 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 160 | // Returns true if any string in the given list has the given suffix. |
| 161 | func SuffixInList(list []string, suffix string) bool { |
| 162 | for _, s := range list { |
| 163 | if strings.HasSuffix(s, suffix) { |
| 164 | return true |
| 165 | } |
| 166 | } |
| 167 | return false |
| 168 | } |
| 169 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 170 | // IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element. |
| 171 | func IndexListPred(pred func(s string) bool, list []string) int { |
| 172 | for i, l := range list { |
| 173 | if pred(l) { |
| 174 | return i |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return -1 |
| 179 | } |
| 180 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 181 | // FilterList divides the string list into two lists: one with the strings belonging |
| 182 | // to the given filter list, and the other with the remaining ones |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 183 | func FilterList(list []string, filter []string) (remainder []string, filtered []string) { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 184 | // 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] | 185 | for _, l := range list { |
| 186 | if InList(l, filter) { |
| 187 | filtered = append(filtered, l) |
| 188 | } else { |
| 189 | remainder = append(remainder, l) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return |
| 194 | } |
| 195 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 196 | // RemoveListFromList removes the strings belonging to the filter list from the |
| 197 | // given list and returns the result |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 198 | func RemoveListFromList(list []string, filter_out []string) (result []string) { |
| 199 | result = make([]string, 0, len(list)) |
| 200 | for _, l := range list { |
| 201 | if !InList(l, filter_out) { |
| 202 | result = append(result, l) |
| 203 | } |
| 204 | } |
| 205 | return |
| 206 | } |
| 207 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 208 | // RemoveFromList removes given string from the string list. |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 209 | func RemoveFromList(s string, list []string) (bool, []string) { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 210 | result := make([]string, 0, len(list)) |
| 211 | var removed bool |
| 212 | for _, item := range list { |
| 213 | if item != s { |
| 214 | result = append(result, item) |
| 215 | } else { |
| 216 | removed = true |
Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 217 | } |
| 218 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 219 | return removed, result |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 222 | // FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of |
| 223 | // each. It modifies the slice contents in place, and returns a subslice of the original slice. |
| 224 | func FirstUniqueStrings(list []string) []string { |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 225 | // 128 was chosen based on BenchmarkFirstUniqueStrings results. |
| 226 | if len(list) > 128 { |
| 227 | return firstUniqueStringsMap(list) |
| 228 | } |
| 229 | return firstUniqueStringsList(list) |
| 230 | } |
| 231 | |
| 232 | func firstUniqueStringsList(list []string) []string { |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 233 | k := 0 |
| 234 | outer: |
| 235 | for i := 0; i < len(list); i++ { |
| 236 | for j := 0; j < k; j++ { |
| 237 | if list[i] == list[j] { |
| 238 | continue outer |
| 239 | } |
| 240 | } |
| 241 | list[k] = list[i] |
| 242 | k++ |
| 243 | } |
| 244 | return list[:k] |
| 245 | } |
| 246 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 247 | func firstUniqueStringsMap(list []string) []string { |
| 248 | k := 0 |
| 249 | seen := make(map[string]bool, len(list)) |
| 250 | for i := 0; i < len(list); i++ { |
| 251 | if seen[list[i]] { |
| 252 | continue |
| 253 | } |
| 254 | seen[list[i]] = true |
| 255 | list[k] = list[i] |
| 256 | k++ |
| 257 | } |
| 258 | return list[:k] |
| 259 | } |
| 260 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 261 | // LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of |
| 262 | // each. It modifies the slice contents in place, and returns a subslice of the original slice. |
| 263 | func LastUniqueStrings(list []string) []string { |
| 264 | totalSkip := 0 |
| 265 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 266 | skip := 0 |
| 267 | for j := i - 1; j >= totalSkip; j-- { |
| 268 | if list[i] == list[j] { |
| 269 | skip++ |
| 270 | } else { |
| 271 | list[j+skip] = list[j] |
| 272 | } |
| 273 | } |
| 274 | totalSkip += skip |
| 275 | } |
| 276 | return list[totalSkip:] |
| 277 | } |
| 278 | |
Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 279 | // SortedUniqueStrings returns what the name says |
| 280 | func SortedUniqueStrings(list []string) []string { |
| 281 | unique := FirstUniqueStrings(list) |
| 282 | sort.Strings(unique) |
| 283 | return unique |
| 284 | } |
| 285 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 286 | // checkCalledFromInit panics if a Go package's init function is not on the |
| 287 | // call stack. |
| 288 | func checkCalledFromInit() { |
| 289 | for skip := 3; ; skip++ { |
| 290 | _, funcName, ok := callerName(skip) |
| 291 | if !ok { |
| 292 | panic("not called from an init func") |
| 293 | } |
| 294 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 295 | if funcName == "init" || strings.HasPrefix(funcName, "init·") || |
| 296 | strings.HasPrefix(funcName, "init.") { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 297 | return |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 302 | // A regex to find a package path within a function name. It finds the shortest string that is |
| 303 | // followed by '.' and doesn't have any '/'s left. |
| 304 | var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) |
| 305 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 306 | // callerName returns the package path and function name of the calling |
| 307 | // function. The skip argument has the same meaning as the skip argument of |
| 308 | // runtime.Callers. |
| 309 | func callerName(skip int) (pkgPath, funcName string, ok bool) { |
| 310 | var pc [1]uintptr |
| 311 | n := runtime.Callers(skip+1, pc[:]) |
| 312 | if n != 1 { |
| 313 | return "", "", false |
| 314 | } |
| 315 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 316 | f := runtime.FuncForPC(pc[0]).Name() |
| 317 | s := pkgPathRe.FindStringSubmatch(f) |
| 318 | if len(s) < 3 { |
| 319 | 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] | 320 | } |
| 321 | |
Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 322 | return s[1], s[2], true |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 323 | } |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 324 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 325 | // GetNumericSdkVersion removes the first occurrence of system_ in a string, |
| 326 | // which is assumed to be something like "system_1.2.3" |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 327 | func GetNumericSdkVersion(v string) string { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 328 | return strings.Replace(v, "system_", "", 1) |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 329 | } |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 330 | |
| 331 | // copied from build/kati/strutil.go |
| 332 | func substPattern(pat, repl, str string) string { |
| 333 | ps := strings.SplitN(pat, "%", 2) |
| 334 | if len(ps) != 2 { |
| 335 | if str == pat { |
| 336 | return repl |
| 337 | } |
| 338 | return str |
| 339 | } |
| 340 | in := str |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 341 | trimmed := str |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 342 | if ps[0] != "" { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 343 | trimmed = strings.TrimPrefix(in, ps[0]) |
| 344 | if trimmed == in { |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 345 | return str |
| 346 | } |
| 347 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 348 | in = trimmed |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 349 | if ps[1] != "" { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 350 | trimmed = strings.TrimSuffix(in, ps[1]) |
| 351 | if trimmed == in { |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 352 | return str |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | rs := strings.SplitN(repl, "%", 2) |
| 357 | if len(rs) != 2 { |
| 358 | return repl |
| 359 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 360 | return rs[0] + trimmed + rs[1] |
Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // copied from build/kati/strutil.go |
| 364 | func matchPattern(pat, str string) bool { |
| 365 | i := strings.IndexByte(pat, '%') |
| 366 | if i < 0 { |
| 367 | return pat == str |
| 368 | } |
| 369 | return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:]) |
| 370 | } |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 371 | |
| 372 | var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+") |
| 373 | |
| 374 | // splitFileExt splits a file name into root, suffix and ext. root stands for the file name without |
| 375 | // the file extension and the version number (e.g. "libexample"). suffix stands for the |
| 376 | // concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the |
| 377 | // file extension after the version numbers are trimmed (e.g. ".so"). |
| 378 | func SplitFileExt(name string) (string, string, string) { |
| 379 | // Extract and trim the shared lib version number if the file name ends with dot digits. |
| 380 | suffix := "" |
| 381 | matches := shlibVersionPattern.FindAllStringIndex(name, -1) |
| 382 | if len(matches) > 0 { |
| 383 | lastMatch := matches[len(matches)-1] |
| 384 | if lastMatch[1] == len(name) { |
| 385 | suffix = name[lastMatch[0]:lastMatch[1]] |
| 386 | name = name[0:lastMatch[0]] |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Extract the file name root and the file extension. |
| 391 | ext := filepath.Ext(name) |
| 392 | root := strings.TrimSuffix(name, ext) |
| 393 | suffix = ext + suffix |
| 394 | |
| 395 | return root, suffix, ext |
| 396 | } |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 397 | |
| 398 | // ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths. |
| 399 | func ShardPaths(paths Paths, shardSize int) []Paths { |
| 400 | if len(paths) == 0 { |
| 401 | return nil |
| 402 | } |
| 403 | ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize) |
| 404 | for len(paths) > shardSize { |
| 405 | ret = append(ret, paths[0:shardSize]) |
| 406 | paths = paths[shardSize:] |
| 407 | } |
| 408 | if len(paths) > 0 { |
| 409 | ret = append(ret, paths) |
| 410 | } |
| 411 | return ret |
| 412 | } |
| 413 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 414 | // ShardString takes a string and returns a slice of strings where the length of each one is |
| 415 | // at most shardSize. |
| 416 | func ShardString(s string, shardSize int) []string { |
| 417 | if len(s) == 0 { |
| 418 | return nil |
| 419 | } |
| 420 | ret := make([]string, 0, (len(s)+shardSize-1)/shardSize) |
| 421 | for len(s) > shardSize { |
| 422 | ret = append(ret, s[0:shardSize]) |
| 423 | s = s[shardSize:] |
| 424 | } |
| 425 | if len(s) > 0 { |
| 426 | ret = append(ret, s) |
| 427 | } |
| 428 | return ret |
| 429 | } |
| 430 | |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 431 | // ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize |
| 432 | // elements. |
| 433 | func ShardStrings(s []string, shardSize int) [][]string { |
| 434 | if len(s) == 0 { |
| 435 | return nil |
| 436 | } |
| 437 | ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize) |
| 438 | for len(s) > shardSize { |
| 439 | ret = append(ret, s[0:shardSize]) |
| 440 | s = s[shardSize:] |
| 441 | } |
| 442 | if len(s) > 0 { |
| 443 | ret = append(ret, s) |
| 444 | } |
| 445 | return ret |
| 446 | } |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 447 | |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 448 | // CheckDuplicate checks if there are duplicates in given string list. |
| 449 | // If there are, it returns first such duplicate and true. |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 450 | func CheckDuplicate(values []string) (duplicate string, found bool) { |
| 451 | seen := make(map[string]string) |
| 452 | for _, v := range values { |
| 453 | if duplicate, found = seen[v]; found { |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 454 | return duplicate, true |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 455 | } |
| 456 | seen[v] = v |
| 457 | } |
Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 458 | return "", false |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 459 | } |