Chore: Avoid unnecessary byte/string conversions (#69001)

Avoid unnecessary byte/string conversion

We can use alternative functions/methods to avoid unnecessary
byte/string conversion calls.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2023-05-26 18:08:50 +08:00 committed by GitHub
parent 515270f5fd
commit 7f7b03d794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 13 deletions

View File

@ -238,10 +238,10 @@ func generateConflictUsersFile(r *ConflictResolver) (*os.File, error) {
if err != nil {
return nil, err
}
if _, err := tmpFile.Write([]byte(getDocumentationForFile())); err != nil {
if _, err := tmpFile.WriteString(getDocumentationForFile()); err != nil {
return nil, err
}
if _, err := tmpFile.Write([]byte(r.ToStringPresentation())); err != nil {
if _, err := tmpFile.WriteString(r.ToStringPresentation()); err != nil {
return nil, err
}
return tmpFile, nil
@ -280,7 +280,7 @@ func getValidConflictUsers(r *ConflictResolver, b []byte) error {
continue
}
entryRow := matchingExpression.Match([]byte(row))
entryRow := matchingExpression.MatchString(row)
// not an entry row -> is a conflict block row
if !entryRow {
// check for malformed row

View File

@ -97,7 +97,7 @@ func (ss *sqlStore) Update(ctx context.Context, cmd folder.UpdateFolderCommand)
}
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
sql := strings.Builder{}
sql.Write([]byte("UPDATE folder SET "))
sql.WriteString("UPDATE folder SET ")
columnsToUpdate := []string{"updated = ?"}
args := []interface{}{updated}
if cmd.NewDescription != nil {
@ -129,8 +129,8 @@ func (ss *sqlStore) Update(ctx context.Context, cmd folder.UpdateFolderCommand)
return folder.ErrBadRequest.Errorf("no columns to update")
}
sql.Write([]byte(strings.Join(columnsToUpdate, ", ")))
sql.Write([]byte(" WHERE uid = ? AND org_id = ?"))
sql.WriteString(strings.Join(columnsToUpdate, ", "))
sql.WriteString(" WHERE uid = ? AND org_id = ?")
args = append(args, cmd.UID, cmd.OrgID)
args = append([]interface{}{sql.String()}, args...)
@ -244,10 +244,10 @@ func (ss *sqlStore) GetChildren(ctx context.Context, q folder.GetChildrenQuery)
sql := strings.Builder{}
args := make([]interface{}, 0, 2)
if q.UID == "" {
sql.Write([]byte("SELECT * FROM folder WHERE parent_uid IS NULL AND org_id=? ORDER BY title ASC"))
sql.WriteString("SELECT * FROM folder WHERE parent_uid IS NULL AND org_id=? ORDER BY title ASC")
args = append(args, q.OrgID)
} else {
sql.Write([]byte("SELECT * FROM folder WHERE parent_uid=? AND org_id=? ORDER BY title ASC"))
sql.WriteString("SELECT * FROM folder WHERE parent_uid=? AND org_id=? ORDER BY title ASC")
args = append(args, q.UID, q.OrgID)
}
@ -256,7 +256,7 @@ func (ss *sqlStore) GetChildren(ctx context.Context, q folder.GetChildrenQuery)
if q.Page > 0 {
offset = q.Limit * (q.Page - 1)
}
sql.Write([]byte(ss.db.GetDialect().LimitOffset(q.Limit, offset)))
sql.WriteString(ss.db.GetDialect().LimitOffset(q.Limit, offset))
}
err := sess.SQL(sql.String(), args...).Find(&folders)
if err != nil {

View File

@ -160,7 +160,7 @@ func (m *kqlMacroEngine) ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str str
result := ""
lastIndex := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
for _, v := range re.FindAllStringSubmatchIndex(str, -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
if v[i] < 0 {

View File

@ -60,7 +60,7 @@ func (query *Query) renderTags() []string {
// If the operator is missing we fall back to sensible defaults
if tag.Operator == "" {
if regexpOperatorPattern.Match([]byte(tag.Value)) {
if regexpOperatorPattern.MatchString(tag.Value) {
tag.Operator = "=~"
} else {
tag.Operator = "="
@ -124,7 +124,7 @@ func (query *Query) renderMeasurement() string {
measurement := query.Measurement
if !regexpMeasurementPattern.Match([]byte(measurement)) {
if !regexpMeasurementPattern.MatchString(measurement) {
measurement = fmt.Sprintf(`"%s"`, measurement)
}

View File

@ -1019,7 +1019,7 @@ func (m *SQLMacroEngineBase) ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str
result := ""
lastIndex := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
for _, v := range re.FindAllStringSubmatchIndex(str, -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
groups = append(groups, str[v[i]:v[i+1]])