diff options
| author | Todd Neal <todd@tneal.org> | 2017-04-17 18:46:09 -0500 |
|---|---|---|
| committer | Todd Neal <todd@tneal.org> | 2017-04-19 11:47:45 +0000 |
| commit | dc444418d919b72f7744a521cc898252f3f355df (patch) | |
| tree | 5045f7a7a8032f9c293fcbca967c9a905dc72321 /src/cmd/internal/objabi/path.go | |
| parent | eb40f0621f37e247597020e316febe701e377b51 (diff) | |
| download | go-dc444418d919b72f7744a521cc898252f3f355df.tar.xz | |
cmd/internal: remove duplicate pathToPrefix function
goobj.importPathToPrefix is 3x faster than gc.pathToPrefix so rename and
move it to cmd/internal/objabi which is already imported by both goobj and
gc.
Change-Id: I10eda5bce95ef6d5d888818c5c47258c2833ea45
Reviewed-on: https://go-review.googlesource.com/40875
Run-TryBot: Todd Neal <todd@tneal.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/internal/objabi/path.go')
| -rw-r--r-- | src/cmd/internal/objabi/path.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cmd/internal/objabi/path.go b/src/cmd/internal/objabi/path.go new file mode 100644 index 0000000000..2a42179a36 --- /dev/null +++ b/src/cmd/internal/objabi/path.go @@ -0,0 +1,41 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package objabi + +import "strings" + +// PathToPrefix converts raw string to the prefix that will be used in the +// symbol table. All control characters, space, '%' and '"', as well as +// non-7-bit clean bytes turn into %xx. The period needs escaping only in the +// last segment of the path, and it makes for happier users if we escape that as +// little as possible. +func PathToPrefix(s string) string { + slash := strings.LastIndex(s, "/") + // check for chars that need escaping + n := 0 + for r := 0; r < len(s); r++ { + if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F { + n++ + } + } + + // quick exit + if n == 0 { + return s + } + + // escape + const hex = "0123456789abcdef" + p := make([]byte, 0, len(s)+2*n) + for r := 0; r < len(s); r++ { + if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F { + p = append(p, '%', hex[c>>4], hex[c&0xF]) + } else { + p = append(p, c) + } + } + + return string(p) +} |
