blob: 7f6af2d68241e8869c0e899b0593e21770be66e7 [file] [log] [blame]
Colin Crossc0b06f12015-04-08 13:03:43 -07001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Crossc0b06f12015-04-08 13:03:43 -070016
Dan Willemsenb1957a52016-06-23 23:44:54 -070017import (
Colin Cross3020fee2019-03-19 15:05:17 -070018 "fmt"
Ivan Lozano022a73b2019-09-09 20:29:31 -070019 "path/filepath"
Inseob Kim1a365c62019-06-08 15:47:51 +090020 "reflect"
Colin Cross3020fee2019-03-19 15:05:17 -070021 "regexp"
Dan Willemsenb1957a52016-06-23 23:44:54 -070022 "runtime"
23 "sort"
24 "strings"
25)
Colin Cross1f8c52b2015-06-16 16:38:17 -070026
Colin Cross454c0872019-02-15 23:03:34 -080027// CopyOf returns a new slice that has the same contents as s.
Colin Cross48016d52023-06-27 09:45:26 -070028func CopyOf[T any](s []T) []T {
Spandan Dascc4da762023-04-27 19:34:08 +000029 // If the input is nil, return nil and not an empty list
30 if s == nil {
31 return s
32 }
Colin Cross48016d52023-06-27 09:45:26 -070033 return append([]T{}, s...)
Colin Cross454c0872019-02-15 23:03:34 -080034}
35
Sam Delmericoa588d152023-06-16 10:28:04 -040036// Concat returns a new slice concatenated from the input slices. It does not change the input
Jiakai Zhang8fe3a412023-02-23 17:37:16 +000037// slices.
Sam Delmericoa588d152023-06-16 10:28:04 -040038func Concat[T any](slices ...[]T) []T {
39 newLength := 0
40 for _, s := range slices {
41 newLength += len(s)
42 }
43 res := make([]T, 0, newLength)
44 for _, s := range slices {
45 res = append(res, s...)
46 }
Jiakai Zhang8fe3a412023-02-23 17:37:16 +000047 return res
48}
49
Joe Onorato2f99c472023-06-21 18:10:28 -070050// JoinPathsWithPrefix converts the paths to strings, prefixes them
51// with prefix and then joins them separated by " ".
52func JoinPathsWithPrefix(paths []Path, prefix string) string {
53 strs := make([]string, len(paths))
54 for i := range paths {
55 strs[i] = paths[i].String()
56 }
57 return JoinWithPrefixAndSeparator(strs, prefix, " ")
58}
59
Sasha Smundak1e533922020-11-19 16:48:18 -080060// JoinWithPrefix prepends the prefix to each string in the list and
61// returns them joined together with " " as separator.
Colin Crossc0b06f12015-04-08 13:03:43 -070062func JoinWithPrefix(strs []string, prefix string) string {
Yu Liu8d82ac52022-05-17 15:13:28 -070063 return JoinWithPrefixAndSeparator(strs, prefix, " ")
64}
65
66// JoinWithPrefixAndSeparator prepends the prefix to each string in the list and
67// returns them joined together with the given separator.
68func JoinWithPrefixAndSeparator(strs []string, prefix string, sep string) string {
Colin Crossc0b06f12015-04-08 13:03:43 -070069 if len(strs) == 0 {
70 return ""
71 }
72
Sasha Smundak1e533922020-11-19 16:48:18 -080073 var buf strings.Builder
74 buf.WriteString(prefix)
75 buf.WriteString(strs[0])
76 for i := 1; i < len(strs); i++ {
Yu Liu8d82ac52022-05-17 15:13:28 -070077 buf.WriteString(sep)
Sasha Smundak1e533922020-11-19 16:48:18 -080078 buf.WriteString(prefix)
79 buf.WriteString(strs[i])
Colin Crossc0b06f12015-04-08 13:03:43 -070080 }
Sasha Smundak1e533922020-11-19 16:48:18 -080081 return buf.String()
Colin Crossc0b06f12015-04-08 13:03:43 -070082}
Colin Cross9b6826f2015-04-10 15:47:33 -070083
Cole Faust18994c72023-02-28 16:02:16 -080084// SortedStringKeys returns the keys of the given map in the ascending order.
85//
86// Deprecated: Use SortedKeys instead.
Cole Faust195c7812023-03-01 14:21:30 -080087func SortedStringKeys[V any](m map[string]V) []string {
88 return SortedKeys(m)
Colin Cross1f8c52b2015-06-16 16:38:17 -070089}
Dan Willemsenb1957a52016-06-23 23:44:54 -070090
Cole Faust18994c72023-02-28 16:02:16 -080091type Ordered interface {
92 ~string |
93 ~float32 | ~float64 |
94 ~int | ~int8 | ~int16 | ~int32 | ~int64 |
95 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
96}
97
98// SortedKeys returns the keys of the given map in the ascending order.
99func SortedKeys[T Ordered, V any](m map[T]V) []T {
100 if len(m) == 0 {
101 return nil
102 }
103 ret := make([]T, 0, len(m))
104 for k := range m {
105 ret = append(ret, k)
106 }
107 sort.Slice(ret, func(i, j int) bool {
108 return ret[i] < ret[j]
109 })
110 return ret
111}
112
Colin Cross9eb853b2022-02-17 11:13:37 -0800113// stringValues returns the values of the given string-valued map in randomized map order.
114func stringValues(m interface{}) []string {
115 v := reflect.ValueOf(m)
116 if v.Kind() != reflect.Map {
117 panic(fmt.Sprintf("%#v is not a map", m))
118 }
119 if v.Len() == 0 {
120 return nil
121 }
122 iter := v.MapRange()
123 s := make([]string, 0, v.Len())
124 for iter.Next() {
125 s = append(s, iter.Value().String())
126 }
127 return s
128}
129
130// SortedStringValues returns the values of the given string-valued map in the ascending order.
131func SortedStringValues(m interface{}) []string {
132 s := stringValues(m)
133 sort.Strings(s)
134 return s
135}
136
137// SortedUniqueStringValues returns the values of the given string-valued map in the ascending order
138// with duplicates removed.
139func SortedUniqueStringValues(m interface{}) []string {
140 s := stringValues(m)
141 return SortedUniqueStrings(s)
142}
143
Sasha Smundak1e533922020-11-19 16:48:18 -0800144// IndexList returns the index of the first occurrence of the given string in the list or -1
Sam Delmerico1717b3b2023-07-18 15:07:24 -0400145func IndexList[T comparable](t T, list []T) int {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700146 for i, l := range list {
Sam Delmerico1717b3b2023-07-18 15:07:24 -0400147 if l == t {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700148 return i
149 }
150 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700151 return -1
152}
153
Sam Delmerico1717b3b2023-07-18 15:07:24 -0400154func InList[T comparable](t T, list []T) bool {
155 return IndexList(t, list) != -1
Dan Willemsenb1957a52016-06-23 23:44:54 -0700156}
157
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500158func setFromList[T comparable](l []T) map[T]bool {
159 m := make(map[T]bool, len(l))
160 for _, t := range l {
161 m[t] = true
162 }
163 return m
164}
165
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500166// ListSetDifference checks if the two lists contain the same elements. It returns
167// a boolean which is true if there is a difference, and then returns lists of elements
168// that are in l1 but not l2, and l2 but not l1.
169func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) {
170 listsDiffer := false
171 diff1 := []T{}
172 diff2 := []T{}
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500173 m1 := setFromList(l1)
174 m2 := setFromList(l2)
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500175 for t := range m1 {
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500176 if _, ok := m2[t]; !ok {
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500177 diff1 = append(diff1, t)
178 listsDiffer = true
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500179 }
180 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500181 for t := range m2 {
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500182 if _, ok := m1[t]; !ok {
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500183 diff2 = append(diff2, t)
184 listsDiffer = true
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500185 }
186 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500187 return listsDiffer, diff1, diff2
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500188}
189
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800190// Returns true if the given string s is prefixed with any string in the given prefix list.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800191func HasAnyPrefix(s string, prefixList []string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800192 for _, prefix := range prefixList {
193 if strings.HasPrefix(s, prefix) {
194 return true
195 }
196 }
197 return false
198}
199
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800200// Returns true if any string in the given list has the given substring.
201func SubstringInList(list []string, substr string) bool {
202 for _, s := range list {
203 if strings.Contains(s, substr) {
204 return true
205 }
206 }
207 return false
208}
209
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800210// Returns true if any string in the given list has the given prefix.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800211func PrefixInList(list []string, prefix string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800212 for _, s := range list {
Ivan Lozano5f595532017-07-13 14:46:05 -0700213 if strings.HasPrefix(s, prefix) {
214 return true
215 }
216 }
217 return false
218}
219
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400220// Returns true if any string in the given list has the given suffix.
221func SuffixInList(list []string, suffix string) bool {
222 for _, s := range list {
223 if strings.HasSuffix(s, suffix) {
224 return true
225 }
226 }
227 return false
228}
229
Jooyung Han12df5fb2019-07-11 16:18:47 +0900230// IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element.
231func IndexListPred(pred func(s string) bool, list []string) int {
232 for i, l := range list {
233 if pred(l) {
234 return i
235 }
236 }
237
238 return -1
239}
240
Sasha Smundak1e533922020-11-19 16:48:18 -0800241// FilterList divides the string list into two lists: one with the strings belonging
242// to the given filter list, and the other with the remaining ones
Colin Crossb4330e22017-12-22 15:47:09 -0800243func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800244 // InList is O(n). May be worth using more efficient lookup for longer lists.
Colin Crossb4330e22017-12-22 15:47:09 -0800245 for _, l := range list {
246 if InList(l, filter) {
247 filtered = append(filtered, l)
248 } else {
249 remainder = append(remainder, l)
250 }
251 }
252
253 return
254}
255
Martin Stjernholm1461c4d2021-03-27 19:04:05 +0000256// FilterListPred returns the elements of the given list for which the predicate
257// returns true. Order is kept.
258func FilterListPred(list []string, pred func(s string) bool) (filtered []string) {
259 for _, l := range list {
260 if pred(l) {
261 filtered = append(filtered, l)
262 }
263 }
264 return
265}
266
Sasha Smundak1e533922020-11-19 16:48:18 -0800267// RemoveListFromList removes the strings belonging to the filter list from the
268// given list and returns the result
Colin Crossb4330e22017-12-22 15:47:09 -0800269func RemoveListFromList(list []string, filter_out []string) (result []string) {
270 result = make([]string, 0, len(list))
271 for _, l := range list {
272 if !InList(l, filter_out) {
273 result = append(result, l)
274 }
275 }
276 return
277}
278
Sasha Smundak1e533922020-11-19 16:48:18 -0800279// RemoveFromList removes given string from the string list.
Colin Crossb4330e22017-12-22 15:47:09 -0800280func RemoveFromList(s string, list []string) (bool, []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800281 result := make([]string, 0, len(list))
282 var removed bool
283 for _, item := range list {
284 if item != s {
285 result = append(result, item)
286 } else {
287 removed = true
Logan Chien7922ab82018-03-06 18:29:27 +0800288 }
289 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800290 return removed, result
Colin Crossb4330e22017-12-22 15:47:09 -0800291}
292
Colin Crossb6715442017-10-24 11:13:31 -0700293// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
Colin Cross48016d52023-06-27 09:45:26 -0700294// each. It does not modify the input slice.
Colin Crossb6715442017-10-24 11:13:31 -0700295func FirstUniqueStrings(list []string) []string {
Colin Crossc85750b2022-04-21 12:50:51 -0700296 return firstUnique(list)
Colin Cross27027c72020-02-28 15:34:17 -0800297}
298
Colin Crossc85750b2022-04-21 12:50:51 -0700299// firstUnique returns all unique elements of a slice, keeping the first copy of each. It
Colin Cross48016d52023-06-27 09:45:26 -0700300// does not modify the input slice.
Colin Crossc85750b2022-04-21 12:50:51 -0700301func firstUnique[T comparable](slice []T) []T {
Colin Cross48016d52023-06-27 09:45:26 -0700302 // Do not modify the input in-place, operate on a copy instead.
303 slice = CopyOf(slice)
304 return firstUniqueInPlace(slice)
305}
306
307// firstUniqueInPlace returns all unique elements of a slice, keeping the first copy of
308// each. It modifies the slice contents in place, and returns a subslice of the original
309// slice.
310func firstUniqueInPlace[T comparable](slice []T) []T {
311 // 128 was chosen based on BenchmarkFirstUniqueStrings results.
312 if len(slice) > 128 {
Colin Crossc85750b2022-04-21 12:50:51 -0700313 return firstUniqueMap(slice)
314 }
315 return firstUniqueList(slice)
316}
317
318// firstUniqueList is an implementation of firstUnique using an O(N^2) list comparison to look for
319// duplicates.
320func firstUniqueList[T any](in []T) []T {
321 writeIndex := 0
Colin Crossb6715442017-10-24 11:13:31 -0700322outer:
Colin Crossc85750b2022-04-21 12:50:51 -0700323 for readIndex := 0; readIndex < len(in); readIndex++ {
324 for compareIndex := 0; compareIndex < writeIndex; compareIndex++ {
325 if interface{}(in[readIndex]) == interface{}(in[compareIndex]) {
326 // The value at readIndex already exists somewhere in the output region
327 // of the slice before writeIndex, skip it.
Colin Crossb6715442017-10-24 11:13:31 -0700328 continue outer
329 }
330 }
Colin Crossc85750b2022-04-21 12:50:51 -0700331 if readIndex != writeIndex {
332 in[writeIndex] = in[readIndex]
333 }
334 writeIndex++
Colin Crossb6715442017-10-24 11:13:31 -0700335 }
Colin Crossc85750b2022-04-21 12:50:51 -0700336 return in[0:writeIndex]
Colin Crossb6715442017-10-24 11:13:31 -0700337}
338
Colin Crossc85750b2022-04-21 12:50:51 -0700339// firstUniqueMap is an implementation of firstUnique using an O(N) hash set lookup to look for
340// duplicates.
341func firstUniqueMap[T comparable](in []T) []T {
342 writeIndex := 0
343 seen := make(map[T]bool, len(in))
344 for readIndex := 0; readIndex < len(in); readIndex++ {
345 if _, exists := seen[in[readIndex]]; exists {
Colin Cross27027c72020-02-28 15:34:17 -0800346 continue
347 }
Colin Crossc85750b2022-04-21 12:50:51 -0700348 seen[in[readIndex]] = true
349 if readIndex != writeIndex {
350 in[writeIndex] = in[readIndex]
351 }
352 writeIndex++
Colin Cross27027c72020-02-28 15:34:17 -0800353 }
Colin Crossc85750b2022-04-21 12:50:51 -0700354 return in[0:writeIndex]
355}
356
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700357// ReverseSliceInPlace reverses the elements of a slice in place and returns it.
358func ReverseSliceInPlace[T any](in []T) []T {
Colin Crossc85750b2022-04-21 12:50:51 -0700359 for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 {
360 in[i], in[j] = in[j], in[i]
361 }
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700362 return in
Colin Crossc85750b2022-04-21 12:50:51 -0700363}
364
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700365// ReverseSlice returns a copy of a slice in reverse order.
366func ReverseSlice[T any](in []T) []T {
367 if in == nil {
368 return in
369 }
Colin Crossc85750b2022-04-21 12:50:51 -0700370 out := make([]T, len(in))
371 for i := 0; i < len(in); i++ {
372 out[i] = in[len(in)-1-i]
373 }
374 return out
Colin Cross27027c72020-02-28 15:34:17 -0800375}
376
Colin Crossb6715442017-10-24 11:13:31 -0700377// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
378// each. It modifies the slice contents in place, and returns a subslice of the original slice.
379func LastUniqueStrings(list []string) []string {
380 totalSkip := 0
381 for i := len(list) - 1; i >= totalSkip; i-- {
382 skip := 0
383 for j := i - 1; j >= totalSkip; j-- {
384 if list[i] == list[j] {
385 skip++
386 } else {
387 list[j+skip] = list[j]
388 }
389 }
390 totalSkip += skip
391 }
392 return list[totalSkip:]
393}
394
Jooyung Hane1633032019-08-01 17:41:43 +0900395// SortedUniqueStrings returns what the name says
396func SortedUniqueStrings(list []string) []string {
Spandan Das8a8714c2023-04-25 18:03:54 +0000397 // FirstUniqueStrings creates a copy of `list`, so the input remains untouched.
Jooyung Hane1633032019-08-01 17:41:43 +0900398 unique := FirstUniqueStrings(list)
399 sort.Strings(unique)
400 return unique
401}
402
Dan Willemsenb1957a52016-06-23 23:44:54 -0700403// checkCalledFromInit panics if a Go package's init function is not on the
404// call stack.
405func checkCalledFromInit() {
406 for skip := 3; ; skip++ {
407 _, funcName, ok := callerName(skip)
408 if !ok {
409 panic("not called from an init func")
410 }
411
Colin Cross3020fee2019-03-19 15:05:17 -0700412 if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
413 strings.HasPrefix(funcName, "init.") {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700414 return
415 }
416 }
417}
418
Colin Cross3020fee2019-03-19 15:05:17 -0700419// A regex to find a package path within a function name. It finds the shortest string that is
420// followed by '.' and doesn't have any '/'s left.
421var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
422
Dan Willemsenb1957a52016-06-23 23:44:54 -0700423// callerName returns the package path and function name of the calling
424// function. The skip argument has the same meaning as the skip argument of
425// runtime.Callers.
426func callerName(skip int) (pkgPath, funcName string, ok bool) {
427 var pc [1]uintptr
428 n := runtime.Callers(skip+1, pc[:])
429 if n != 1 {
430 return "", "", false
431 }
432
Colin Cross3020fee2019-03-19 15:05:17 -0700433 f := runtime.FuncForPC(pc[0]).Name()
434 s := pkgPathRe.FindStringSubmatch(f)
435 if len(s) < 3 {
436 panic(fmt.Errorf("failed to extract package path and function name from %q", f))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700437 }
438
Colin Cross3020fee2019-03-19 15:05:17 -0700439 return s[1], s[2], true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700440}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900441
Sasha Smundak1e533922020-11-19 16:48:18 -0800442// GetNumericSdkVersion removes the first occurrence of system_ in a string,
443// which is assumed to be something like "system_1.2.3"
Sundong Ahn0926fae2017-10-17 16:34:51 +0900444func GetNumericSdkVersion(v string) string {
Sasha Smundak1e533922020-11-19 16:48:18 -0800445 return strings.Replace(v, "system_", "", 1)
Sundong Ahn0926fae2017-10-17 16:34:51 +0900446}
Jiyong Park7f67f482019-01-05 12:57:48 +0900447
448// copied from build/kati/strutil.go
449func substPattern(pat, repl, str string) string {
450 ps := strings.SplitN(pat, "%", 2)
451 if len(ps) != 2 {
452 if str == pat {
453 return repl
454 }
455 return str
456 }
457 in := str
Sasha Smundak1e533922020-11-19 16:48:18 -0800458 trimmed := str
Jiyong Park7f67f482019-01-05 12:57:48 +0900459 if ps[0] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800460 trimmed = strings.TrimPrefix(in, ps[0])
461 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900462 return str
463 }
464 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800465 in = trimmed
Jiyong Park7f67f482019-01-05 12:57:48 +0900466 if ps[1] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800467 trimmed = strings.TrimSuffix(in, ps[1])
468 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900469 return str
470 }
471 }
472
473 rs := strings.SplitN(repl, "%", 2)
474 if len(rs) != 2 {
475 return repl
476 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800477 return rs[0] + trimmed + rs[1]
Jiyong Park7f67f482019-01-05 12:57:48 +0900478}
479
480// copied from build/kati/strutil.go
481func matchPattern(pat, str string) bool {
482 i := strings.IndexByte(pat, '%')
483 if i < 0 {
484 return pat == str
485 }
486 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
487}
Ivan Lozano022a73b2019-09-09 20:29:31 -0700488
489var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
490
491// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
492// the file extension and the version number (e.g. "libexample"). suffix stands for the
493// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the
494// file extension after the version numbers are trimmed (e.g. ".so").
495func SplitFileExt(name string) (string, string, string) {
496 // Extract and trim the shared lib version number if the file name ends with dot digits.
497 suffix := ""
498 matches := shlibVersionPattern.FindAllStringIndex(name, -1)
499 if len(matches) > 0 {
500 lastMatch := matches[len(matches)-1]
501 if lastMatch[1] == len(name) {
502 suffix = name[lastMatch[0]:lastMatch[1]]
503 name = name[0:lastMatch[0]]
504 }
505 }
506
507 // Extract the file name root and the file extension.
508 ext := filepath.Ext(name)
509 root := strings.TrimSuffix(name, ext)
510 suffix = ext + suffix
511
512 return root, suffix, ext
513}
Colin Cross0a2f7192019-09-23 14:33:09 -0700514
515// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths.
516func ShardPaths(paths Paths, shardSize int) []Paths {
517 if len(paths) == 0 {
518 return nil
519 }
520 ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize)
521 for len(paths) > shardSize {
522 ret = append(ret, paths[0:shardSize])
523 paths = paths[shardSize:]
524 }
525 if len(paths) > 0 {
526 ret = append(ret, paths)
527 }
528 return ret
529}
530
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100531// ShardString takes a string and returns a slice of strings where the length of each one is
532// at most shardSize.
533func ShardString(s string, shardSize int) []string {
534 if len(s) == 0 {
535 return nil
536 }
537 ret := make([]string, 0, (len(s)+shardSize-1)/shardSize)
538 for len(s) > shardSize {
539 ret = append(ret, s[0:shardSize])
540 s = s[shardSize:]
541 }
542 if len(s) > 0 {
543 ret = append(ret, s)
544 }
545 return ret
546}
547
Colin Cross0a2f7192019-09-23 14:33:09 -0700548// ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize
549// elements.
550func ShardStrings(s []string, shardSize int) [][]string {
551 if len(s) == 0 {
552 return nil
553 }
554 ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize)
555 for len(s) > shardSize {
556 ret = append(ret, s[0:shardSize])
557 s = s[shardSize:]
558 }
559 if len(s) > 0 {
560 ret = append(ret, s)
561 }
562 return ret
563}
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700564
Sasha Smundak1e533922020-11-19 16:48:18 -0800565// CheckDuplicate checks if there are duplicates in given string list.
566// If there are, it returns first such duplicate and true.
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700567func CheckDuplicate(values []string) (duplicate string, found bool) {
568 seen := make(map[string]string)
569 for _, v := range values {
570 if duplicate, found = seen[v]; found {
Sasha Smundak1e533922020-11-19 16:48:18 -0800571 return duplicate, true
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700572 }
573 seen[v] = v
574 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800575 return "", false
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700576}