Source file src/cmd/internal/objabi/path.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package objabi
     6  
     7  import "strings"
     8  
     9  // PathToPrefix converts raw string to the prefix that will be used in the
    10  // symbol table. All control characters, space, '%' and '"', as well as
    11  // non-7-bit clean bytes turn into %xx. The period needs escaping only in the
    12  // last segment of the path, and it makes for happier users if we escape that as
    13  // little as possible.
    14  func PathToPrefix(s string) string {
    15  	slash := strings.LastIndex(s, "/")
    16  	// check for chars that need escaping
    17  	n := 0
    18  	for r := 0; r < len(s); r++ {
    19  		if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F {
    20  			n++
    21  		}
    22  	}
    23  
    24  	// quick exit
    25  	if n == 0 {
    26  		return s
    27  	}
    28  
    29  	// escape
    30  	const hex = "0123456789abcdef"
    31  	p := make([]byte, 0, len(s)+2*n)
    32  	for r := 0; r < len(s); r++ {
    33  		if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F {
    34  			p = append(p, '%', hex[c>>4], hex[c&0xF])
    35  		} else {
    36  			p = append(p, c)
    37  		}
    38  	}
    39  
    40  	return string(p)
    41  }
    42  
    43  // IsRuntimePackagePath examines 'pkgpath' and returns TRUE if it
    44  // belongs to the collection of "runtime-related" packages, including
    45  // "runtime" itself, "reflect", "syscall", and the
    46  // "runtime/internal/*" packages. The compiler and/or assembler in
    47  // some cases need to be aware of when they are building such a
    48  // package, for example to enable features such as ABI selectors in
    49  // assembly sources.
    50  //
    51  // Keep in sync with cmd/dist/build.go:IsRuntimePackagePath.
    52  func IsRuntimePackagePath(pkgpath string) bool {
    53  	rval := false
    54  	switch pkgpath {
    55  	case "runtime":
    56  		rval = true
    57  	case "reflect":
    58  		rval = true
    59  	case "syscall":
    60  		rval = true
    61  	case "internal/bytealg":
    62  		rval = true
    63  	default:
    64  		rval = strings.HasPrefix(pkgpath, "runtime/internal")
    65  	}
    66  	return rval
    67  }
    68  

View as plain text