Skip to content

Commit

Permalink
chore: improve escaping performance
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <kzantow@gmail.com>
  • Loading branch information
kzantow committed Jan 4, 2023
1 parent a072fa3 commit 31803f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packageurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,20 @@ func validQualifierKey(key string) bool {
// pathEscape Make any purl type-specific adjustments to the url encoding.
// See https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#character-encoding
func pathEscape(s string) string {
escapeCount := 0
for _, c := range s {
switch {
case c == '@' || c == '?' || c == '#' || c == ' ':
escapeCount += 1
case c > unicode.MaxASCII:
escapeCount += 3
}
}
if escapeCount == 0 {
return s
}
var t strings.Builder
t.Grow(len(s) + (escapeCount * 3))
for _, c := range s {
switch {
case c == '@':
Expand Down
5 changes: 5 additions & 0 deletions packageurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ func TestEncoding(t *testing.T) {
input: "pkg:type/%3E%41%22space/name@version?key=value#sub/path",
expected: "pkg:type/>A\"space/name@version?key=value#sub/path",
},
{
name: "unicode characters are encoded",
input: "pkg:type/ē/name@version?key=value#sub/path",
expected: "pkg:type/%C4%93/name@version?key=value#sub/path",
},
{
name: "pre-encoded namespace segment is unchanged",
input: "pkg:type/name/spac%20e/name@version?key=value#sub/path",
Expand Down

0 comments on commit 31803f2

Please sign in to comment.