Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame^] | 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 | func 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 | } |