blob: 3eb8dcbce7139c775045ae6f3e1acf8e3ceed139 [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 Cross13aeb682023-07-06 15:02:56 -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 Cross13aeb682023-07-06 15:02:56 -070033 return append([]T{}, s...)
Colin Cross454c0872019-02-15 23:03:34 -080034}
35
Jiakai Zhang8fe3a412023-02-23 17:37:16 +000036// Concat returns a new slice concatenated from the two input slices. It does not change the input
37// slices.
38func Concat[T any](s1, s2 []T) []T {
39 res := make([]T, 0, len(s1)+len(s2))
40 res = append(res, s1...)
41 res = append(res, s2...)
42 return res
43}
44
Sasha Smundak1e533922020-11-19 16:48:18 -080045// JoinWithPrefix prepends the prefix to each string in the list and
46// returns them joined together with " " as separator.
Colin Crossc0b06f12015-04-08 13:03:43 -070047func JoinWithPrefix(strs []string, prefix string) string {
Yu Liu8d82ac52022-05-17 15:13:28 -070048 return JoinWithPrefixAndSeparator(strs, prefix, " ")
49}
50
51// JoinWithPrefixAndSeparator prepends the prefix to each string in the list and
52// returns them joined together with the given separator.
53func JoinWithPrefixAndSeparator(strs []string, prefix string, sep string) string {
Colin Crossc0b06f12015-04-08 13:03:43 -070054 if len(strs) == 0 {
55 return ""
56 }
57
Sasha Smundak1e533922020-11-19 16:48:18 -080058 var buf strings.Builder
59 buf.WriteString(prefix)
60 buf.WriteString(strs[0])
61 for i := 1; i < len(strs); i++ {
Yu Liu8d82ac52022-05-17 15:13:28 -070062 buf.WriteString(sep)
Sasha Smundak1e533922020-11-19 16:48:18 -080063 buf.WriteString(prefix)
64 buf.WriteString(strs[i])
Colin Crossc0b06f12015-04-08 13:03:43 -070065 }
Sasha Smundak1e533922020-11-19 16:48:18 -080066 return buf.String()
Colin Crossc0b06f12015-04-08 13:03:43 -070067}
Colin Cross9b6826f2015-04-10 15:47:33 -070068
Cole Faust18994c72023-02-28 16:02:16 -080069// SortedStringKeys returns the keys of the given map in the ascending order.
70//
71// Deprecated: Use SortedKeys instead.
Cole Faust195c7812023-03-01 14:21:30 -080072func SortedStringKeys[V any](m map[string]V) []string {
73 return SortedKeys(m)
Colin Cross1f8c52b2015-06-16 16:38:17 -070074}
Dan Willemsenb1957a52016-06-23 23:44:54 -070075
Cole Faust18994c72023-02-28 16:02:16 -080076type Ordered interface {
77 ~string |
78 ~float32 | ~float64 |
79 ~int | ~int8 | ~int16 | ~int32 | ~int64 |
80 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
81}
82
83// SortedKeys returns the keys of the given map in the ascending order.
84func SortedKeys[T Ordered, V any](m map[T]V) []T {
85 if len(m) == 0 {
86 return nil
87 }
88 ret := make([]T, 0, len(m))
89 for k := range m {
90 ret = append(ret, k)
91 }
92 sort.Slice(ret, func(i, j int) bool {
93 return ret[i] < ret[j]
94 })
95 return ret
96}
97
Colin Cross9eb853b2022-02-17 11:13:37 -080098// stringValues returns the values of the given string-valued map in randomized map order.
99func stringValues(m interface{}) []string {
100 v := reflect.ValueOf(m)
101 if v.Kind() != reflect.Map {
102 panic(fmt.Sprintf("%#v is not a map", m))
103 }
104 if v.Len() == 0 {
105 return nil
106 }
107 iter := v.MapRange()
108 s := make([]string, 0, v.Len())
109 for iter.Next() {
110 s = append(s, iter.Value().String())
111 }
112 return s
113}
114
115// SortedStringValues returns the values of the given string-valued map in the ascending order.
116func SortedStringValues(m interface{}) []string {
117 s := stringValues(m)
118 sort.Strings(s)
119 return s
120}
121
122// SortedUniqueStringValues returns the values of the given string-valued map in the ascending order
123// with duplicates removed.
124func SortedUniqueStringValues(m interface{}) []string {
125 s := stringValues(m)
126 return SortedUniqueStrings(s)
127}
128
Sasha Smundak1e533922020-11-19 16:48:18 -0800129// IndexList returns the index of the first occurrence of the given string in the list or -1
Colin Crossb4330e22017-12-22 15:47:09 -0800130func IndexList(s string, list []string) int {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700131 for i, l := range list {
132 if l == s {
133 return i
134 }
135 }
136
137 return -1
138}
139
Sasha Smundak1e533922020-11-19 16:48:18 -0800140// InList checks if the string belongs to the list
Colin Crossb4330e22017-12-22 15:47:09 -0800141func InList(s string, list []string) bool {
142 return IndexList(s, list) != -1
Dan Willemsenb1957a52016-06-23 23:44:54 -0700143}
144
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500145func setFromList[T comparable](l []T) map[T]bool {
146 m := make(map[T]bool, len(l))
147 for _, t := range l {
148 m[t] = true
149 }
150 return m
151}
152
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500153// ListSetDifference checks if the two lists contain the same elements. It returns
154// a boolean which is true if there is a difference, and then returns lists of elements
155// that are in l1 but not l2, and l2 but not l1.
156func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) {
157 listsDiffer := false
158 diff1 := []T{}
159 diff2 := []T{}
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500160 m1 := setFromList(l1)
161 m2 := setFromList(l2)
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500162 for t := range m1 {
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500163 if _, ok := m2[t]; !ok {
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500164 diff1 = append(diff1, t)
165 listsDiffer = true
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500166 }
167 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500168 for t := range m2 {
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500169 if _, ok := m1[t]; !ok {
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500170 diff2 = append(diff2, t)
171 listsDiffer = true
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500172 }
173 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500174 return listsDiffer, diff1, diff2
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500175}
176
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800177// Returns true if the given string s is prefixed with any string in the given prefix list.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800178func HasAnyPrefix(s string, prefixList []string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800179 for _, prefix := range prefixList {
180 if strings.HasPrefix(s, prefix) {
181 return true
182 }
183 }
184 return false
185}
186
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800187// Returns true if any string in the given list has the given substring.
188func SubstringInList(list []string, substr string) bool {
189 for _, s := range list {
190 if strings.Contains(s, substr) {
191 return true
192 }
193 }
194 return false
195}
196
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800197// Returns true if any string in the given list has the given prefix.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800198func PrefixInList(list []string, prefix string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800199 for _, s := range list {
Ivan Lozano5f595532017-07-13 14:46:05 -0700200 if strings.HasPrefix(s, prefix) {
201 return true
202 }
203 }
204 return false
205}
206
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400207// Returns true if any string in the given list has the given suffix.
208func SuffixInList(list []string, suffix string) bool {
209 for _, s := range list {
210 if strings.HasSuffix(s, suffix) {
211 return true
212 }
213 }
214 return false
215}
216
Jooyung Han12df5fb2019-07-11 16:18:47 +0900217// IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element.
218func IndexListPred(pred func(s string) bool, list []string) int {
219 for i, l := range list {
220 if pred(l) {
221 return i
222 }
223 }
224
225 return -1
226}
227
Sasha Smundak1e533922020-11-19 16:48:18 -0800228// FilterList divides the string list into two lists: one with the strings belonging
229// to the given filter list, and the other with the remaining ones
Colin Crossb4330e22017-12-22 15:47:09 -0800230func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800231 // InList is O(n). May be worth using more efficient lookup for longer lists.
Colin Crossb4330e22017-12-22 15:47:09 -0800232 for _, l := range list {
233 if InList(l, filter) {
234 filtered = append(filtered, l)
235 } else {
236 remainder = append(remainder, l)
237 }
238 }
239
240 return
241}
242
Martin Stjernholm1461c4d2021-03-27 19:04:05 +0000243// FilterListPred returns the elements of the given list for which the predicate
244// returns true. Order is kept.
245func FilterListPred(list []string, pred func(s string) bool) (filtered []string) {
246 for _, l := range list {
247 if pred(l) {
248 filtered = append(filtered, l)
249 }
250 }
251 return
252}
253
Sasha Smundak1e533922020-11-19 16:48:18 -0800254// RemoveListFromList removes the strings belonging to the filter list from the
255// given list and returns the result
Colin Crossb4330e22017-12-22 15:47:09 -0800256func RemoveListFromList(list []string, filter_out []string) (result []string) {
257 result = make([]string, 0, len(list))
258 for _, l := range list {
259 if !InList(l, filter_out) {
260 result = append(result, l)
261 }
262 }
263 return
264}
265
Sasha Smundak1e533922020-11-19 16:48:18 -0800266// RemoveFromList removes given string from the string list.
Colin Crossb4330e22017-12-22 15:47:09 -0800267func RemoveFromList(s string, list []string) (bool, []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800268 result := make([]string, 0, len(list))
269 var removed bool
270 for _, item := range list {
271 if item != s {
272 result = append(result, item)
273 } else {
274 removed = true
Logan Chien7922ab82018-03-06 18:29:27 +0800275 }
276 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800277 return removed, result
Colin Crossb4330e22017-12-22 15:47:09 -0800278}
279
Colin Crossb6715442017-10-24 11:13:31 -0700280// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
281// each. It modifies the slice contents in place, and returns a subslice of the original slice.
282func FirstUniqueStrings(list []string) []string {
Spandan Das8a8714c2023-04-25 18:03:54 +0000283 // Do not moodify the input in-place, operate on a copy instead.
284 list = CopyOf(list)
Colin Cross27027c72020-02-28 15:34:17 -0800285 // 128 was chosen based on BenchmarkFirstUniqueStrings results.
286 if len(list) > 128 {
Colin Crossc85750b2022-04-21 12:50:51 -0700287 return firstUnique(list)
Colin Cross27027c72020-02-28 15:34:17 -0800288 }
Colin Crossc85750b2022-04-21 12:50:51 -0700289 return firstUnique(list)
Colin Cross27027c72020-02-28 15:34:17 -0800290}
291
Colin Crossc85750b2022-04-21 12:50:51 -0700292// firstUnique returns all unique elements of a slice, keeping the first copy of each. It
293// modifies the slice contents in place, and returns a subslice of the original slice.
294func firstUnique[T comparable](slice []T) []T {
295 // 4 was chosen based on Benchmark_firstUnique results.
296 if len(slice) > 4 {
297 return firstUniqueMap(slice)
298 }
299 return firstUniqueList(slice)
300}
301
302// firstUniqueList is an implementation of firstUnique using an O(N^2) list comparison to look for
303// duplicates.
304func firstUniqueList[T any](in []T) []T {
305 writeIndex := 0
Colin Crossb6715442017-10-24 11:13:31 -0700306outer:
Colin Crossc85750b2022-04-21 12:50:51 -0700307 for readIndex := 0; readIndex < len(in); readIndex++ {
308 for compareIndex := 0; compareIndex < writeIndex; compareIndex++ {
309 if interface{}(in[readIndex]) == interface{}(in[compareIndex]) {
310 // The value at readIndex already exists somewhere in the output region
311 // of the slice before writeIndex, skip it.
Colin Crossb6715442017-10-24 11:13:31 -0700312 continue outer
313 }
314 }
Colin Crossc85750b2022-04-21 12:50:51 -0700315 if readIndex != writeIndex {
316 in[writeIndex] = in[readIndex]
317 }
318 writeIndex++
Colin Crossb6715442017-10-24 11:13:31 -0700319 }
Colin Crossc85750b2022-04-21 12:50:51 -0700320 return in[0:writeIndex]
Colin Crossb6715442017-10-24 11:13:31 -0700321}
322
Colin Crossc85750b2022-04-21 12:50:51 -0700323// firstUniqueMap is an implementation of firstUnique using an O(N) hash set lookup to look for
324// duplicates.
325func firstUniqueMap[T comparable](in []T) []T {
326 writeIndex := 0
327 seen := make(map[T]bool, len(in))
328 for readIndex := 0; readIndex < len(in); readIndex++ {
329 if _, exists := seen[in[readIndex]]; exists {
Colin Cross27027c72020-02-28 15:34:17 -0800330 continue
331 }
Colin Crossc85750b2022-04-21 12:50:51 -0700332 seen[in[readIndex]] = true
333 if readIndex != writeIndex {
334 in[writeIndex] = in[readIndex]
335 }
336 writeIndex++
Colin Cross27027c72020-02-28 15:34:17 -0800337 }
Colin Crossc85750b2022-04-21 12:50:51 -0700338 return in[0:writeIndex]
339}
340
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700341// ReverseSliceInPlace reverses the elements of a slice in place and returns it.
342func ReverseSliceInPlace[T any](in []T) []T {
Colin Crossc85750b2022-04-21 12:50:51 -0700343 for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 {
344 in[i], in[j] = in[j], in[i]
345 }
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700346 return in
Colin Crossc85750b2022-04-21 12:50:51 -0700347}
348
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700349// ReverseSlice returns a copy of a slice in reverse order.
350func ReverseSlice[T any](in []T) []T {
351 if in == nil {
352 return in
353 }
Colin Crossc85750b2022-04-21 12:50:51 -0700354 out := make([]T, len(in))
355 for i := 0; i < len(in); i++ {
356 out[i] = in[len(in)-1-i]
357 }
358 return out
Colin Cross27027c72020-02-28 15:34:17 -0800359}
360
Colin Crossb6715442017-10-24 11:13:31 -0700361// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
362// each. It modifies the slice contents in place, and returns a subslice of the original slice.
363func LastUniqueStrings(list []string) []string {
364 totalSkip := 0
365 for i := len(list) - 1; i >= totalSkip; i-- {
366 skip := 0
367 for j := i - 1; j >= totalSkip; j-- {
368 if list[i] == list[j] {
369 skip++
370 } else {
371 list[j+skip] = list[j]
372 }
373 }
374 totalSkip += skip
375 }
376 return list[totalSkip:]
377}
378
Jooyung Hane1633032019-08-01 17:41:43 +0900379// SortedUniqueStrings returns what the name says
380func SortedUniqueStrings(list []string) []string {
Spandan Das8a8714c2023-04-25 18:03:54 +0000381 // FirstUniqueStrings creates a copy of `list`, so the input remains untouched.
Jooyung Hane1633032019-08-01 17:41:43 +0900382 unique := FirstUniqueStrings(list)
383 sort.Strings(unique)
384 return unique
385}
386
Dan Willemsenb1957a52016-06-23 23:44:54 -0700387// checkCalledFromInit panics if a Go package's init function is not on the
388// call stack.
389func checkCalledFromInit() {
390 for skip := 3; ; skip++ {
391 _, funcName, ok := callerName(skip)
392 if !ok {
393 panic("not called from an init func")
394 }
395
Colin Cross3020fee2019-03-19 15:05:17 -0700396 if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
397 strings.HasPrefix(funcName, "init.") {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700398 return
399 }
400 }
401}
402
Colin Cross3020fee2019-03-19 15:05:17 -0700403// A regex to find a package path within a function name. It finds the shortest string that is
404// followed by '.' and doesn't have any '/'s left.
405var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
406
Dan Willemsenb1957a52016-06-23 23:44:54 -0700407// callerName returns the package path and function name of the calling
408// function. The skip argument has the same meaning as the skip argument of
409// runtime.Callers.
410func callerName(skip int) (pkgPath, funcName string, ok bool) {
411 var pc [1]uintptr
412 n := runtime.Callers(skip+1, pc[:])
413 if n != 1 {
414 return "", "", false
415 }
416
Colin Cross3020fee2019-03-19 15:05:17 -0700417 f := runtime.FuncForPC(pc[0]).Name()
418 s := pkgPathRe.FindStringSubmatch(f)
419 if len(s) < 3 {
420 panic(fmt.Errorf("failed to extract package path and function name from %q", f))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700421 }
422
Colin Cross3020fee2019-03-19 15:05:17 -0700423 return s[1], s[2], true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700424}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900425
Sasha Smundak1e533922020-11-19 16:48:18 -0800426// GetNumericSdkVersion removes the first occurrence of system_ in a string,
427// which is assumed to be something like "system_1.2.3"
Sundong Ahn0926fae2017-10-17 16:34:51 +0900428func GetNumericSdkVersion(v string) string {
Sasha Smundak1e533922020-11-19 16:48:18 -0800429 return strings.Replace(v, "system_", "", 1)
Sundong Ahn0926fae2017-10-17 16:34:51 +0900430}
Jiyong Park7f67f482019-01-05 12:57:48 +0900431
432// copied from build/kati/strutil.go
433func substPattern(pat, repl, str string) string {
434 ps := strings.SplitN(pat, "%", 2)
435 if len(ps) != 2 {
436 if str == pat {
437 return repl
438 }
439 return str
440 }
441 in := str
Sasha Smundak1e533922020-11-19 16:48:18 -0800442 trimmed := str
Jiyong Park7f67f482019-01-05 12:57:48 +0900443 if ps[0] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800444 trimmed = strings.TrimPrefix(in, ps[0])
445 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900446 return str
447 }
448 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800449 in = trimmed
Jiyong Park7f67f482019-01-05 12:57:48 +0900450 if ps[1] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800451 trimmed = strings.TrimSuffix(in, ps[1])
452 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900453 return str
454 }
455 }
456
457 rs := strings.SplitN(repl, "%", 2)
458 if len(rs) != 2 {
459 return repl
460 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800461 return rs[0] + trimmed + rs[1]
Jiyong Park7f67f482019-01-05 12:57:48 +0900462}
463
464// copied from build/kati/strutil.go
465func matchPattern(pat, str string) bool {
466 i := strings.IndexByte(pat, '%')
467 if i < 0 {
468 return pat == str
469 }
470 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
471}
Ivan Lozano022a73b2019-09-09 20:29:31 -0700472
473var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
474
475// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
476// the file extension and the version number (e.g. "libexample"). suffix stands for the
477// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the
478// file extension after the version numbers are trimmed (e.g. ".so").
479func SplitFileExt(name string) (string, string, string) {
480 // Extract and trim the shared lib version number if the file name ends with dot digits.
481 suffix := ""
482 matches := shlibVersionPattern.FindAllStringIndex(name, -1)
483 if len(matches) > 0 {
484 lastMatch := matches[len(matches)-1]
485 if lastMatch[1] == len(name) {
486 suffix = name[lastMatch[0]:lastMatch[1]]
487 name = name[0:lastMatch[0]]
488 }
489 }
490
491 // Extract the file name root and the file extension.
492 ext := filepath.Ext(name)
493 root := strings.TrimSuffix(name, ext)
494 suffix = ext + suffix
495
496 return root, suffix, ext
497}
Colin Cross0a2f7192019-09-23 14:33:09 -0700498
499// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths.
500func ShardPaths(paths Paths, shardSize int) []Paths {
501 if len(paths) == 0 {
502 return nil
503 }
504 ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize)
505 for len(paths) > shardSize {
506 ret = append(ret, paths[0:shardSize])
507 paths = paths[shardSize:]
508 }
509 if len(paths) > 0 {
510 ret = append(ret, paths)
511 }
512 return ret
513}
514
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100515// ShardString takes a string and returns a slice of strings where the length of each one is
516// at most shardSize.
517func ShardString(s string, shardSize int) []string {
518 if len(s) == 0 {
519 return nil
520 }
521 ret := make([]string, 0, (len(s)+shardSize-1)/shardSize)
522 for len(s) > shardSize {
523 ret = append(ret, s[0:shardSize])
524 s = s[shardSize:]
525 }
526 if len(s) > 0 {
527 ret = append(ret, s)
528 }
529 return ret
530}
531
Colin Cross0a2f7192019-09-23 14:33:09 -0700532// ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize
533// elements.
534func ShardStrings(s []string, shardSize int) [][]string {
535 if len(s) == 0 {
536 return nil
537 }
538 ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize)
539 for len(s) > shardSize {
540 ret = append(ret, s[0:shardSize])
541 s = s[shardSize:]
542 }
543 if len(s) > 0 {
544 ret = append(ret, s)
545 }
546 return ret
547}
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700548
Sasha Smundak1e533922020-11-19 16:48:18 -0800549// CheckDuplicate checks if there are duplicates in given string list.
550// If there are, it returns first such duplicate and true.
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700551func CheckDuplicate(values []string) (duplicate string, found bool) {
552 seen := make(map[string]string)
553 for _, v := range values {
554 if duplicate, found = seen[v]; found {
Sasha Smundak1e533922020-11-19 16:48:18 -0800555 return duplicate, true
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700556 }
557 seen[v] = v
558 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800559 return "", false
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700560}