blob: 60c8a63af8a405f8066e8fcf1b0cfafe4801a005 [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
Colin Cross1f8c52b2015-06-16 16:38:17 -070017import "sort"
18
Colin Crossc0b06f12015-04-08 13:03:43 -070019func JoinWithPrefix(strs []string, prefix string) string {
20 if len(strs) == 0 {
21 return ""
22 }
23
24 if len(strs) == 1 {
25 return prefix + strs[0]
26 }
27
28 n := len(" ") * (len(strs) - 1)
29 for _, s := range strs {
30 n += len(prefix) + len(s)
31 }
32
33 ret := make([]byte, 0, n)
34 for i, s := range strs {
35 if i != 0 {
36 ret = append(ret, ' ')
37 }
38 ret = append(ret, prefix...)
39 ret = append(ret, s...)
40 }
41 return string(ret)
42}
Colin Cross9b6826f2015-04-10 15:47:33 -070043
44func JoinWithPrefixAndQuote(strs []string, prefix string) string {
45 if len(strs) == 0 {
46 return ""
47 }
48
49 if len(strs) == 1 {
50 return prefix + `"` + strs[0] + `"`
51 }
52
53 n := len(" ") * (len(strs) - 1)
54 for _, s := range strs {
55 n += len(prefix) + len(s) + len(`""`)
56 }
57
58 ret := make([]byte, 0, n)
59 for i, s := range strs {
60 if i != 0 {
61 ret = append(ret, ' ')
62 }
63 ret = append(ret, prefix...)
64 ret = append(ret, '"')
65 ret = append(ret, s...)
66 ret = append(ret, '"')
67 }
68 return string(ret)
69}
Colin Cross1f8c52b2015-06-16 16:38:17 -070070
71func sortedKeys(m map[string][]string) []string {
72 s := make([]string, 0, len(m))
73 for k := range m {
74 s = append(s, k)
75 }
76 sort.Strings(s)
77 return s
78}