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