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, instead of a regexp
Browse files Browse the repository at this point in the history
Cherry-picked from #279
  • Loading branch information
pquerna authored and mbyczkowski committed Mar 8, 2020
1 parent 9f8649a commit 6fde6ca
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (
"encoding/binary"
"io"
"math/big"
"regexp"
"strings"
"unicode"

"github.com/square/go-jose/v3/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 +55,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 6fde6ca

Please sign in to comment.