| 1 | // Copyright 2022 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 report |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "regexp" |
| 20 | |
| 21 | "github.com/google/pprof/internal/graph" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | sepRE = regexp.MustCompile(`::|\.`) |
| 26 | fileSepRE = regexp.MustCompile(`/`) |
| 27 | ) |
| 28 | |
| 29 | // fileNameSuffixes returns a non-empty sequence of shortened file names |
| 30 | // (in decreasing preference) that can be used to represent name. |
| 31 | func fileNameSuffixes(name string) []string { |
| 32 | if name == "" { |
| 33 | // Avoid returning "." when symbol info is missing |
| 34 | return []string{""} |
| 35 | } |
| 36 | return allSuffixes(filepath.ToSlash(filepath.Clean(name)), fileSepRE) |
| 37 | } |
| 38 | |
| 39 | // shortNameList returns a non-empty sequence of shortened names |
| 40 | // (in decreasing preference) that can be used to represent name. |
| 41 | func shortNameList(name string) []string { |
| 42 | name = graph.ShortenFunctionName(name) |
| 43 | return allSuffixes(name, sepRE) |
| 44 | } |
| 45 | |
| 46 | // allSuffixes returns a list of suffixes (in order of decreasing length) |
| 47 | // found by splitting at re. |
| 48 | func allSuffixes(name string, re *regexp.Regexp) []string { |
| 49 | seps := re.FindAllStringIndex(name, -1) |
| 50 | result := make([]string, 0, len(seps)+1) |
| 51 | result = append(result, name) |
| 52 | for _, sep := range seps { |
| 53 | // Suffix starting just after sep |
| 54 | if sep[1] < len(name) { |
| 55 | result = append(result, name[sep[1]:]) |
| 56 | } |
| 57 | } |
| 58 | return result |
| 59 | } |