blob: 503fbbd496f9126525e82953c1101bd489363b2f [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 (
18 "runtime"
19 "sort"
20 "strings"
21)
Colin Cross1f8c52b2015-06-16 16:38:17 -070022
Colin Crossc0b06f12015-04-08 13:03:43 -070023func JoinWithPrefix(strs []string, prefix string) string {
24 if len(strs) == 0 {
25 return ""
26 }
27
28 if len(strs) == 1 {
29 return prefix + strs[0]
30 }
31
32 n := len(" ") * (len(strs) - 1)
33 for _, s := range strs {
34 n += len(prefix) + len(s)
35 }
36
37 ret := make([]byte, 0, n)
38 for i, s := range strs {
39 if i != 0 {
40 ret = append(ret, ' ')
41 }
42 ret = append(ret, prefix...)
43 ret = append(ret, s...)
44 }
45 return string(ret)
46}
Colin Cross9b6826f2015-04-10 15:47:33 -070047
48func JoinWithPrefixAndQuote(strs []string, prefix string) string {
49 if len(strs) == 0 {
50 return ""
51 }
52
53 if len(strs) == 1 {
54 return prefix + `"` + strs[0] + `"`
55 }
56
57 n := len(" ") * (len(strs) - 1)
58 for _, s := range strs {
59 n += len(prefix) + len(s) + len(`""`)
60 }
61
62 ret := make([]byte, 0, n)
63 for i, s := range strs {
64 if i != 0 {
65 ret = append(ret, ' ')
66 }
67 ret = append(ret, prefix...)
68 ret = append(ret, '"')
69 ret = append(ret, s...)
70 ret = append(ret, '"')
71 }
72 return string(ret)
73}
Colin Cross1f8c52b2015-06-16 16:38:17 -070074
75func sortedKeys(m map[string][]string) []string {
76 s := make([]string, 0, len(m))
77 for k := range m {
78 s = append(s, k)
79 }
80 sort.Strings(s)
81 return s
82}
Dan Willemsenb1957a52016-06-23 23:44:54 -070083
84func indexList(s string, list []string) int {
85 for i, l := range list {
86 if l == s {
87 return i
88 }
89 }
90
91 return -1
92}
93
94func inList(s string, list []string) bool {
95 return indexList(s, list) != -1
96}
97
98// checkCalledFromInit panics if a Go package's init function is not on the
99// call stack.
100func checkCalledFromInit() {
101 for skip := 3; ; skip++ {
102 _, funcName, ok := callerName(skip)
103 if !ok {
104 panic("not called from an init func")
105 }
106
107 if funcName == "init" || strings.HasPrefix(funcName, "init·") {
108 return
109 }
110 }
111}
112
113// callerName returns the package path and function name of the calling
114// function. The skip argument has the same meaning as the skip argument of
115// runtime.Callers.
116func callerName(skip int) (pkgPath, funcName string, ok bool) {
117 var pc [1]uintptr
118 n := runtime.Callers(skip+1, pc[:])
119 if n != 1 {
120 return "", "", false
121 }
122
123 f := runtime.FuncForPC(pc[0])
124 fullName := f.Name()
125
126 lastDotIndex := strings.LastIndex(fullName, ".")
127 if lastDotIndex == -1 {
128 panic("unable to distinguish function name from package")
129 }
130
131 if fullName[lastDotIndex-1] == ')' {
132 // The caller is a method on some type, so it's name looks like
133 // "pkg/path.(type).method". We need to go back one dot farther to get
134 // to the package name.
135 lastDotIndex = strings.LastIndex(fullName[:lastDotIndex], ".")
136 }
137
138 pkgPath = fullName[:lastDotIndex]
139 funcName = fullName[lastDotIndex+1:]
140 ok = true
141 return
142}