Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Use string.Builder to remove whitespace, isntead of a regeexp
Browse files Browse the repository at this point in the history
  • Loading branch information
pquerna committed Nov 16, 2019
1 parent d22cbe1 commit b24ea32
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"io"
"math/big"
"regexp"
"strings"
"unicode"

"gopkg.in/square/go-jose.v2/json"
)

var stripWhitespaceRegex = regexp.MustCompile("\\s")

// Helper function to serialize known-good objects.
// Precondition: value is not a nil pointer.
func mustSerializeJSON(value interface{}) []byte {
Expand All @@ -56,7 +56,14 @@ func mustSerializeJSON(value interface{}) []byte {

// Strip all newlines and whitespace
func stripWhitespace(data string) string {
return stripWhitespaceRegex.ReplaceAllString(data, "")
buf := strings.Builder{}
buf.Grow(len(data))
for _, r := range data {
if !unicode.IsSpace(r) {
buf.WriteRune(r)
}
}
return buf.String()
}

// Perform compression based on algorithm
Expand Down

0 comments on commit b24ea32

Please sign in to comment.