Skip to content

Commit

Permalink
perf: avoid unnecessary byte/string conversion
Browse files Browse the repository at this point in the history
We can use alternative functions/methods to avoid unnecessary
byte/string conversion calls.

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored and ashutosh-narkar committed May 23, 2023
1 parent e903f5d commit b3ae18d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/generate-cli-docs/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
// elements in markdown. This is undesirable, so let's remove them and prepend
// the line before that with ### to instead create a h3
for line, str := range lines {
if heading.Match([]byte(str)) {
if heading.MatchString(str) {
document[line-1-removed] = fmt.Sprintf("### %s\n", document[line-1-removed])
removed++
continue
Expand Down
4 changes: 2 additions & 2 deletions internal/gojsonschema/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func (v *SubSchema) validateString(currentSubSchema *SubSchema, value interface{

// minLength & maxLength:
if currentSubSchema.minLength != nil {
if utf8.RuneCount([]byte(stringValue)) < *currentSubSchema.minLength {
if utf8.RuneCountInString(stringValue) < *currentSubSchema.minLength {
result.addInternalError(
new(StringLengthGTEError),
context,
Expand All @@ -726,7 +726,7 @@ func (v *SubSchema) validateString(currentSubSchema *SubSchema, value interface{
}
}
if currentSubSchema.maxLength != nil {
if utf8.RuneCount([]byte(stringValue)) > *currentSubSchema.maxLength {
if utf8.RuneCountInString(stringValue) > *currentSubSchema.maxLength {
result.addInternalError(
new(StringLengthLTEError),
context,
Expand Down
6 changes: 3 additions & 3 deletions sdk/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (s *Server) handleOCIBundles(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
return
}
buf.WriteString(string(bs))
buf.Write(bs)
_, err = w.Write(buf.Bytes())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -327,7 +327,7 @@ func (s *Server) handleOCIBundles(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
return
}
buf.WriteString(string(bs))
buf.Write(bs)
_, err = w.Write(buf.Bytes())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -344,7 +344,7 @@ func (s *Server) handleOCIBundles(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
return
}
buf.WriteString(string(bs))
buf.Write(bs)
_, err = w.Write(buf.Bytes())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion topdown/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func builtinRegexMatch(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Te
if err != nil {
return err
}
return iter(ast.BooleanTerm(re.Match([]byte(s2))))
return iter(ast.BooleanTerm(re.MatchString(string(s2))))
}

func builtinRegexMatchTemplate(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
Expand Down

0 comments on commit b3ae18d

Please sign in to comment.