1 // 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 15 package common 16 17 import "sort" 18 19 func 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 } 43 44 func 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 } 70 71 func 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 } 79