aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/objabi/path.go
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2023-09-15 13:49:56 -0400
committerMichael Pratt <mpratt@google.com>2023-10-13 13:56:25 +0000
commit696fb5ead03a4b84e11f2d195bc91838fdd029b2 (patch)
treee9755fdf7ed3c5ccdeb5823ce2d2bcd523c3bbad /src/cmd/internal/objabi/path.go
parentd1ef967306b401474aafb2a0d351e972bdfeb54c (diff)
downloadgo-696fb5ead03a4b84e11f2d195bc91838fdd029b2.tar.xz
cmd/internal/objabi: add inverse of PathToPrefix
Add PrefixToPath, which can be used to convert a package path in a symbol name back to the original package path. For #61577. Change-Id: Ifbe8c852a7f41ff9b81ad48b92a26a0e1b046777 Reviewed-on: https://go-review.googlesource.com/c/go/+/529557 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd/internal/objabi/path.go')
-rw-r--r--src/cmd/internal/objabi/path.go39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/cmd/internal/objabi/path.go b/src/cmd/internal/objabi/path.go
index 2a42179a36..30301b15f1 100644
--- a/src/cmd/internal/objabi/path.go
+++ b/src/cmd/internal/objabi/path.go
@@ -4,7 +4,11 @@
package objabi
-import "strings"
+import (
+ "fmt"
+ "strconv"
+ "strings"
+)
// PathToPrefix converts raw string to the prefix that will be used in the
// symbol table. All control characters, space, '%' and '"', as well as
@@ -39,3 +43,36 @@ func PathToPrefix(s string) string {
return string(p)
}
+
+// PrefixToPath is the inverse of PathToPrefix, replacing escape sequences with
+// the original character.
+func PrefixToPath(s string) (string, error) {
+ percent := strings.IndexByte(s, '%')
+ if percent == -1 {
+ return s, nil
+ }
+
+ p := make([]byte, 0, len(s))
+ for i := 0; i < len(s); {
+ if s[i] != '%' {
+ p = append(p, s[i])
+ i++
+ continue
+ }
+ if i+2 >= len(s) {
+ // Not enough characters remaining to be a valid escape
+ // sequence.
+ return "", fmt.Errorf("malformed prefix %q: escape sequence must contain two hex digits", s)
+ }
+
+ b, err := strconv.ParseUint(s[i+1:i+3], 16, 8)
+ if err != nil {
+ // Not a valid escape sequence.
+ return "", fmt.Errorf("malformed prefix %q: escape sequence %q must contain two hex digits", s, s[i:i+3])
+ }
+
+ p = append(p, byte(b))
+ i += 3
+ }
+ return string(p), nil
+}