blob: 50bf5aa540f7751a29306f3f56b4c2beee8f1d30 [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
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
Joe Onorato2f99c472023-06-21 18:10:28 -070045// JoinPathsWithPrefix converts the paths to strings, prefixes them
46// with prefix and then joins them separated by " ".
47func JoinPathsWithPrefix(paths []Path, prefix string) string {
48 strs := make([]string, len(paths))
49 for i := range paths {
50 strs[i] = paths[i].String()
51 }
52 return JoinWithPrefixAndSeparator(strs, prefix, " ")
53}
54
Sasha Smundak1e533922020-11-19 16:48:18 -080055// JoinWithPrefix prepends the prefix to each string in the list and
56// returns them joined together with " " as separator.
Colin Crossc0b06f12015-04-08 13:03:43 -070057func JoinWithPrefix(strs []string, prefix string) string {
Yu Liu8d82ac52022-05-17 15:13:28 -070058 return JoinWithPrefixAndSeparator(strs, prefix, " ")
59}
60
61// JoinWithPrefixAndSeparator prepends the prefix to each string in the list and
62// returns them joined together with the given separator.
63func JoinWithPrefixAndSeparator(strs []string, prefix string, sep string) string {
Colin Crossc0b06f12015-04-08 13:03:43 -070064 if len(strs) == 0 {
65 return ""
66 }
67
Sasha Smundak1e533922020-11-19 16:48:18 -080068 var buf strings.Builder
69 buf.WriteString(prefix)
70 buf.WriteString(strs[0])
71 for i := 1; i < len(strs); i++ {
Yu Liu8d82ac52022-05-17 15:13:28 -070072 buf.WriteString(sep)
Sasha Smundak1e533922020-11-19 16:48:18 -080073 buf.WriteString(prefix)
74 buf.WriteString(strs[i])
Colin Crossc0b06f12015-04-08 13:03:43 -070075 }
Sasha Smundak1e533922020-11-19 16:48:18 -080076 return buf.String()
Colin Crossc0b06f12015-04-08 13:03:43 -070077}
Colin Cross9b6826f2015-04-10 15:47:33 -070078
Cole Faust18994c72023-02-28 16:02:16 -080079// SortedStringKeys returns the keys of the given map in the ascending order.
80//
81// Deprecated: Use SortedKeys instead.
Cole Faust195c7812023-03-01 14:21:30 -080082func SortedStringKeys[V any](m map[string]V) []string {
83 return SortedKeys(m)
Colin Cross1f8c52b2015-06-16 16:38:17 -070084}
Dan Willemsenb1957a52016-06-23 23:44:54 -070085
Cole Faust18994c72023-02-28 16:02:16 -080086type Ordered interface {
87 ~string |
88 ~float32 | ~float64 |
89 ~int | ~int8 | ~int16 | ~int32 | ~int64 |
90 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
91}
92
93// SortedKeys returns the keys of the given map in the ascending order.
94func SortedKeys[T Ordered, V any](m map[T]V) []T {
95 if len(m) == 0 {
96 return nil
97 }
98 ret := make([]T, 0, len(m))
99 for k := range m {
100 ret = append(ret, k)
101 }
102 sort.Slice(ret, func(i, j int) bool {
103 return ret[i] < ret[j]
104 })
105 return ret
106}
107
Colin Cross9eb853b2022-02-17 11:13:37 -0800108// stringValues returns the values of the given string-valued map in randomized map order.
109func stringValues(m interface{}) []string {
110 v := reflect.ValueOf(m)
111 if v.Kind() != reflect.Map {
112 panic(fmt.Sprintf("%#v is not a map", m))
113 }
114 if v.Len() == 0 {
115 return nil
116 }
117 iter := v.MapRange()
118 s := make([]string, 0, v.Len())
119 for iter.Next() {
120 s = append(s, iter.Value().String())
121 }
122 return s
123}
124
125// SortedStringValues returns the values of the given string-valued map in the ascending order.
126func SortedStringValues(m interface{}) []string {
127 s := stringValues(m)
128 sort.Strings(s)
129 return s
130}
131
132// SortedUniqueStringValues returns the values of the given string-valued map in the ascending order
133// with duplicates removed.
134func SortedUniqueStringValues(m interface{}) []string {
135 s := stringValues(m)
136 return SortedUniqueStrings(s)
137}
138
Sasha Smundak1e533922020-11-19 16:48:18 -0800139// IndexList returns the index of the first occurrence of the given string in the list or -1
Colin Crossb4330e22017-12-22 15:47:09 -0800140func IndexList(s string, list []string) int {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700141 for i, l := range list {
142 if l == s {
143 return i
144 }
145 }
146
147 return -1
148}
149
Sasha Smundak1e533922020-11-19 16:48:18 -0800150// InList checks if the string belongs to the list
Colin Crossb4330e22017-12-22 15:47:09 -0800151func InList(s string, list []string) bool {
152 return IndexList(s, list) != -1
Dan Willemsenb1957a52016-06-23 23:44:54 -0700153}
154
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500155func setFromList[T comparable](l []T) map[T]bool {
156 m := make(map[T]bool, len(l))
157 for _, t := range l {
158 m[t] = true
159 }
160 return m
161}
162
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500163// ListSetDifference checks if the two lists contain the same elements. It returns
164// a boolean which is true if there is a difference, and then returns lists of elements
165// that are in l1 but not l2, and l2 but not l1.
166func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) {
167 listsDiffer := false
168 diff1 := []T{}
169 diff2 := []T{}
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500170 m1 := setFromList(l1)
171 m2 := setFromList(l2)
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500172 for t := range m1 {
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500173 if _, ok := m2[t]; !ok {
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500174 diff1 = append(diff1, t)
175 listsDiffer = true
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500176 }
177 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500178 for t := range m2 {
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500179 if _, ok := m1[t]; !ok {
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500180 diff2 = append(diff2, t)
181 listsDiffer = true
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500182 }
183 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500184 return listsDiffer, diff1, diff2
Sam Delmerico4e115cc2023-01-19 15:36:52 -0500185}
186
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800187// Returns true if the given string s is prefixed with any string in the given prefix list.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800188func HasAnyPrefix(s string, prefixList []string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800189 for _, prefix := range prefixList {
190 if strings.HasPrefix(s, prefix) {
191 return true
192 }
193 }
194 return false
195}
196
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800197// Returns true if any string in the given list has the given substring.
198func SubstringInList(list []string, substr string) bool {
199 for _, s := range list {
200 if strings.Contains(s, substr) {
201 return true
202 }
203 }
204 return false
205}
206
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800207// Returns true if any string in the given list has the given prefix.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800208func PrefixInList(list []string, prefix string) bool {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800209 for _, s := range list {
Ivan Lozano5f595532017-07-13 14:46:05 -0700210 if strings.HasPrefix(s, prefix) {
211 return true
212 }
213 }
214 return false
215}
216
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400217// Returns true if any string in the given list has the given suffix.
218func SuffixInList(list []string, suffix string) bool {
219 for _, s := range list {
220 if strings.HasSuffix(s, suffix) {
221 return true
222 }
223 }
224 return false
225}
226
Jooyung Han12df5fb2019-07-11 16:18:47 +0900227// IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element.
228func IndexListPred(pred func(s string) bool, list []string) int {
229 for i, l := range list {
230 if pred(l) {
231 return i
232 }
233 }
234
235 return -1
236}
237
Sasha Smundak1e533922020-11-19 16:48:18 -0800238// FilterList divides the string list into two lists: one with the strings belonging
239// to the given filter list, and the other with the remaining ones
Colin Crossb4330e22017-12-22 15:47:09 -0800240func FilterList(list []string, filter []string) (remainder []string, filtered []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800241 // InList is O(n). May be worth using more efficient lookup for longer lists.
Colin Crossb4330e22017-12-22 15:47:09 -0800242 for _, l := range list {
243 if InList(l, filter) {
244 filtered = append(filtered, l)
245 } else {
246 remainder = append(remainder, l)
247 }
248 }
249
250 return
251}
252
Martin Stjernholm1461c4d2021-03-27 19:04:05 +0000253// FilterListPred returns the elements of the given list for which the predicate
254// returns true. Order is kept.
255func FilterListPred(list []string, pred func(s string) bool) (filtered []string) {
256 for _, l := range list {
257 if pred(l) {
258 filtered = append(filtered, l)
259 }
260 }
261 return
262}
263
Sasha Smundak1e533922020-11-19 16:48:18 -0800264// RemoveListFromList removes the strings belonging to the filter list from the
265// given list and returns the result
Colin Crossb4330e22017-12-22 15:47:09 -0800266func RemoveListFromList(list []string, filter_out []string) (result []string) {
267 result = make([]string, 0, len(list))
268 for _, l := range list {
269 if !InList(l, filter_out) {
270 result = append(result, l)
271 }
272 }
273 return
274}
275
Sasha Smundak1e533922020-11-19 16:48:18 -0800276// RemoveFromList removes given string from the string list.
Colin Crossb4330e22017-12-22 15:47:09 -0800277func RemoveFromList(s string, list []string) (bool, []string) {
Sasha Smundak1e533922020-11-19 16:48:18 -0800278 result := make([]string, 0, len(list))
279 var removed bool
280 for _, item := range list {
281 if item != s {
282 result = append(result, item)
283 } else {
284 removed = true
Logan Chien7922ab82018-03-06 18:29:27 +0800285 }
286 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800287 return removed, result
Colin Crossb4330e22017-12-22 15:47:09 -0800288}
289
Colin Crossb6715442017-10-24 11:13:31 -0700290// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
Colin Cross48016d52023-06-27 09:45:26 -0700291// each. It does not modify the input slice.
Colin Crossb6715442017-10-24 11:13:31 -0700292func FirstUniqueStrings(list []string) []string {
Colin Crossc85750b2022-04-21 12:50:51 -0700293 return firstUnique(list)
Colin Cross27027c72020-02-28 15:34:17 -0800294}
295
Colin Crossc85750b2022-04-21 12:50:51 -0700296// firstUnique returns all unique elements of a slice, keeping the first copy of each. It
Colin Cross48016d52023-06-27 09:45:26 -0700297// does not modify the input slice.
Colin Crossc85750b2022-04-21 12:50:51 -0700298func firstUnique[T comparable](slice []T) []T {
Colin Cross48016d52023-06-27 09:45:26 -0700299 // Do not modify the input in-place, operate on a copy instead.
300 slice = CopyOf(slice)
301 return firstUniqueInPlace(slice)
302}
303
304// firstUniqueInPlace returns all unique elements of a slice, keeping the first copy of
305// each. It modifies the slice contents in place, and returns a subslice of the original
306// slice.
307func firstUniqueInPlace[T comparable](slice []T) []T {
308 // 128 was chosen based on BenchmarkFirstUniqueStrings results.
309 if len(slice) > 128 {
Colin Crossc85750b2022-04-21 12:50:51 -0700310 return firstUniqueMap(slice)
311 }
312 return firstUniqueList(slice)
313}
314
315// firstUniqueList is an implementation of firstUnique using an O(N^2) list comparison to look for
316// duplicates.
317func firstUniqueList[T any](in []T) []T {
318 writeIndex := 0
Colin Crossb6715442017-10-24 11:13:31 -0700319outer:
Colin Crossc85750b2022-04-21 12:50:51 -0700320 for readIndex := 0; readIndex < len(in); readIndex++ {
321 for compareIndex := 0; compareIndex < writeIndex; compareIndex++ {
322 if interface{}(in[readIndex]) == interface{}(in[compareIndex]) {
323 // The value at readIndex already exists somewhere in the output region
324 // of the slice before writeIndex, skip it.
Colin Crossb6715442017-10-24 11:13:31 -0700325 continue outer
326 }
327 }
Colin Crossc85750b2022-04-21 12:50:51 -0700328 if readIndex != writeIndex {
329 in[writeIndex] = in[readIndex]
330 }
331 writeIndex++
Colin Crossb6715442017-10-24 11:13:31 -0700332 }
Colin Crossc85750b2022-04-21 12:50:51 -0700333 return in[0:writeIndex]
Colin Crossb6715442017-10-24 11:13:31 -0700334}
335
Colin Crossc85750b2022-04-21 12:50:51 -0700336// firstUniqueMap is an implementation of firstUnique using an O(N) hash set lookup to look for
337// duplicates.
338func firstUniqueMap[T comparable](in []T) []T {
339 writeIndex := 0
340 seen := make(map[T]bool, len(in))
341 for readIndex := 0; readIndex < len(in); readIndex++ {
342 if _, exists := seen[in[readIndex]]; exists {
Colin Cross27027c72020-02-28 15:34:17 -0800343 continue
344 }
Colin Crossc85750b2022-04-21 12:50:51 -0700345 seen[in[readIndex]] = true
346 if readIndex != writeIndex {
347 in[writeIndex] = in[readIndex]
348 }
349 writeIndex++
Colin Cross27027c72020-02-28 15:34:17 -0800350 }
Colin Crossc85750b2022-04-21 12:50:51 -0700351 return in[0:writeIndex]
352}
353
354// reverseSliceInPlace reverses the elements of a slice in place.
355func reverseSliceInPlace[T any](in []T) {
356 for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 {
357 in[i], in[j] = in[j], in[i]
358 }
359}
360
361// reverseSlice returns a copy of a slice in reverse order.
362func reverseSlice[T any](in []T) []T {
363 out := make([]T, len(in))
364 for i := 0; i < len(in); i++ {
365 out[i] = in[len(in)-1-i]
366 }
367 return out
Colin Cross27027c72020-02-28 15:34:17 -0800368}
369
Colin Crossb6715442017-10-24 11:13:31 -0700370// LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of
371// each. It modifies the slice contents in place, and returns a subslice of the original slice.
372func LastUniqueStrings(list []string) []string {
373 totalSkip := 0
374 for i := len(list) - 1; i >= totalSkip; i-- {
375 skip := 0
376 for j := i - 1; j >= totalSkip; j-- {
377 if list[i] == list[j] {
378 skip++
379 } else {
380 list[j+skip] = list[j]
381 }
382 }
383 totalSkip += skip
384 }
385 return list[totalSkip:]
386}
387
Jooyung Hane1633032019-08-01 17:41:43 +0900388// SortedUniqueStrings returns what the name says
389func SortedUniqueStrings(list []string) []string {
Spandan Das8a8714c2023-04-25 18:03:54 +0000390 // FirstUniqueStrings creates a copy of `list`, so the input remains untouched.
Jooyung Hane1633032019-08-01 17:41:43 +0900391 unique := FirstUniqueStrings(list)
392 sort.Strings(unique)
393 return unique
394}
395
Dan Willemsenb1957a52016-06-23 23:44:54 -0700396// checkCalledFromInit panics if a Go package's init function is not on the
397// call stack.
398func checkCalledFromInit() {
399 for skip := 3; ; skip++ {
400 _, funcName, ok := callerName(skip)
401 if !ok {
402 panic("not called from an init func")
403 }
404
Colin Cross3020fee2019-03-19 15:05:17 -0700405 if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
406 strings.HasPrefix(funcName, "init.") {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700407 return
408 }
409 }
410}
411
Colin Cross3020fee2019-03-19 15:05:17 -0700412// A regex to find a package path within a function name. It finds the shortest string that is
413// followed by '.' and doesn't have any '/'s left.
414var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
415
Dan Willemsenb1957a52016-06-23 23:44:54 -0700416// callerName returns the package path and function name of the calling
417// function. The skip argument has the same meaning as the skip argument of
418// runtime.Callers.
419func callerName(skip int) (pkgPath, funcName string, ok bool) {
420 var pc [1]uintptr
421 n := runtime.Callers(skip+1, pc[:])
422 if n != 1 {
423 return "", "", false
424 }
425
Colin Cross3020fee2019-03-19 15:05:17 -0700426 f := runtime.FuncForPC(pc[0]).Name()
427 s := pkgPathRe.FindStringSubmatch(f)
428 if len(s) < 3 {
429 panic(fmt.Errorf("failed to extract package path and function name from %q", f))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700430 }
431
Colin Cross3020fee2019-03-19 15:05:17 -0700432 return s[1], s[2], true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700433}
Sundong Ahn0926fae2017-10-17 16:34:51 +0900434
Sasha Smundak1e533922020-11-19 16:48:18 -0800435// GetNumericSdkVersion removes the first occurrence of system_ in a string,
436// which is assumed to be something like "system_1.2.3"
Sundong Ahn0926fae2017-10-17 16:34:51 +0900437func GetNumericSdkVersion(v string) string {
Sasha Smundak1e533922020-11-19 16:48:18 -0800438 return strings.Replace(v, "system_", "", 1)
Sundong Ahn0926fae2017-10-17 16:34:51 +0900439}
Jiyong Park7f67f482019-01-05 12:57:48 +0900440
441// copied from build/kati/strutil.go
442func substPattern(pat, repl, str string) string {
443 ps := strings.SplitN(pat, "%", 2)
444 if len(ps) != 2 {
445 if str == pat {
446 return repl
447 }
448 return str
449 }
450 in := str
Sasha Smundak1e533922020-11-19 16:48:18 -0800451 trimmed := str
Jiyong Park7f67f482019-01-05 12:57:48 +0900452 if ps[0] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800453 trimmed = strings.TrimPrefix(in, ps[0])
454 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900455 return str
456 }
457 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800458 in = trimmed
Jiyong Park7f67f482019-01-05 12:57:48 +0900459 if ps[1] != "" {
Sasha Smundak1e533922020-11-19 16:48:18 -0800460 trimmed = strings.TrimSuffix(in, ps[1])
461 if trimmed == in {
Jiyong Park7f67f482019-01-05 12:57:48 +0900462 return str
463 }
464 }
465
466 rs := strings.SplitN(repl, "%", 2)
467 if len(rs) != 2 {
468 return repl
469 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800470 return rs[0] + trimmed + rs[1]
Jiyong Park7f67f482019-01-05 12:57:48 +0900471}
472
473// copied from build/kati/strutil.go
474func matchPattern(pat, str string) bool {
475 i := strings.IndexByte(pat, '%')
476 if i < 0 {
477 return pat == str
478 }
479 return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
480}
Ivan Lozano022a73b2019-09-09 20:29:31 -0700481
482var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
483
484// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
485// the file extension and the version number (e.g. "libexample"). suffix stands for the
486// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the
487// file extension after the version numbers are trimmed (e.g. ".so").
488func SplitFileExt(name string) (string, string, string) {
489 // Extract and trim the shared lib version number if the file name ends with dot digits.
490 suffix := ""
491 matches := shlibVersionPattern.FindAllStringIndex(name, -1)
492 if len(matches) > 0 {
493 lastMatch := matches[len(matches)-1]
494 if lastMatch[1] == len(name) {
495 suffix = name[lastMatch[0]:lastMatch[1]]
496 name = name[0:lastMatch[0]]
497 }
498 }
499
500 // Extract the file name root and the file extension.
501 ext := filepath.Ext(name)
502 root := strings.TrimSuffix(name, ext)
503 suffix = ext + suffix
504
505 return root, suffix, ext
506}
Colin Cross0a2f7192019-09-23 14:33:09 -0700507
508// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths.
509func ShardPaths(paths Paths, shardSize int) []Paths {
510 if len(paths) == 0 {
511 return nil
512 }
513 ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize)
514 for len(paths) > shardSize {
515 ret = append(ret, paths[0:shardSize])
516 paths = paths[shardSize:]
517 }
518 if len(paths) > 0 {
519 ret = append(ret, paths)
520 }
521 return ret
522}
523
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100524// ShardString takes a string and returns a slice of strings where the length of each one is
525// at most shardSize.
526func ShardString(s string, shardSize int) []string {
527 if len(s) == 0 {
528 return nil
529 }
530 ret := make([]string, 0, (len(s)+shardSize-1)/shardSize)
531 for len(s) > shardSize {
532 ret = append(ret, s[0:shardSize])
533 s = s[shardSize:]
534 }
535 if len(s) > 0 {
536 ret = append(ret, s)
537 }
538 return ret
539}
540
Colin Cross0a2f7192019-09-23 14:33:09 -0700541// ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize
542// elements.
543func ShardStrings(s []string, shardSize int) [][]string {
544 if len(s) == 0 {
545 return nil
546 }
547 ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize)
548 for len(s) > shardSize {
549 ret = append(ret, s[0:shardSize])
550 s = s[shardSize:]
551 }
552 if len(s) > 0 {
553 ret = append(ret, s)
554 }
555 return ret
556}
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700557
Sasha Smundak1e533922020-11-19 16:48:18 -0800558// CheckDuplicate checks if there are duplicates in given string list.
559// If there are, it returns first such duplicate and true.
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700560func CheckDuplicate(values []string) (duplicate string, found bool) {
561 seen := make(map[string]string)
562 for _, v := range values {
563 if duplicate, found = seen[v]; found {
Sasha Smundak1e533922020-11-19 16:48:18 -0800564 return duplicate, true
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700565 }
566 seen[v] = v
567 }
Sasha Smundak1e533922020-11-19 16:48:18 -0800568 return "", false
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700569}