blob: 599b3b17a1f711034436465962d7ddcfbe580925 [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
15package common
16
17func JoinWithPrefix(strs []string, prefix string) string {
18 if len(strs) == 0 {
19 return ""
20 }
21
22 if len(strs) == 1 {
23 return prefix + strs[0]
24 }
25
26 n := len(" ") * (len(strs) - 1)
27 for _, s := range strs {
28 n += len(prefix) + len(s)
29 }
30
31 ret := make([]byte, 0, n)
32 for i, s := range strs {
33 if i != 0 {
34 ret = append(ret, ' ')
35 }
36 ret = append(ret, prefix...)
37 ret = append(ret, s...)
38 }
39 return string(ret)
40}
Colin Cross9b6826f2015-04-10 15:47:33 -070041
42func JoinWithPrefixAndQuote(strs []string, prefix string) string {
43 if len(strs) == 0 {
44 return ""
45 }
46
47 if len(strs) == 1 {
48 return prefix + `"` + strs[0] + `"`
49 }
50
51 n := len(" ") * (len(strs) - 1)
52 for _, s := range strs {
53 n += len(prefix) + len(s) + len(`""`)
54 }
55
56 ret := make([]byte, 0, n)
57 for i, s := range strs {
58 if i != 0 {
59 ret = append(ret, ' ')
60 }
61 ret = append(ret, prefix...)
62 ret = append(ret, '"')
63 ret = append(ret, s...)
64 ret = append(ret, '"')
65 }
66 return string(ret)
67}