Skip to content

Commit 81aeb13

Browse files
committed
commands: lint - flag rune operations
Casting `string`s to `[]rune` causes an allocation that we don't need. This can be replaced with a call to `utf8.DecodeRuneInString`. The size check in `WriteRune` can be avoided for constant `rune`s that are a single `byte`.
1 parent befe075 commit 81aeb13

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

internal/commands/flag.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212
"time"
13+
"unicode/utf8"
1314
"unsafe"
1415

1516
"github.com/djdv/go-filesystem-utils/internal/generic"
@@ -267,7 +268,7 @@ func evalOp(operation string, mode fs.FileMode) (string, rune, fs.FileMode, erro
267268
}
268269

269270
func parseOp(clauseOp string) (rune, string, error) {
270-
switch op := []rune(clauseOp)[0]; op {
271+
switch op, _ := utf8.DecodeRuneInString(clauseOp); op {
271272
case permOpAdd, permOpSub, permOpSet:
272273
const opOffset = 1 // WARN: ASCII-ism.
273274
return op, clauseOp[opOffset:], nil
@@ -322,7 +323,7 @@ func parsePermcopy(mode fs.FileMode, clauseFragment string) (string, bool, fs.Fi
322323
userShift = 6
323324
)
324325
var permissions fs.FileMode
325-
switch who := []rune(clauseFragment)[0]; who {
326+
switch who, _ := utf8.DecodeRuneInString(clauseFragment); who {
326327
case permWhoUser:
327328
permissions = (mode & permUserBits) >> userShift
328329
case permWhoGroup:
@@ -437,7 +438,7 @@ func modeToSymbolicPermissions(mode fs.FileMode) string {
437438
for i, pair := range pairs {
438439
writePermSymbols(&sb, mode, pair.whoMask, pair.specMask, pair.whoSymbol, pair.specSymbol)
439440
if i != len(pairs)-1 && sb.Len() != previousLen {
440-
sb.WriteRune(',')
441+
sb.WriteByte(',')
441442
}
442443
previousLen = sb.Len() // No writes, no separator.
443444
}
@@ -476,7 +477,7 @@ func writePermSymbols(sb *strings.Builder, mode, who, special fs.FileMode, whoSy
476477
return
477478
}
478479
sb.WriteRune(whoSym)
479-
sb.WriteRune('=')
480+
sb.WriteByte('=')
480481
for _, r := range runes {
481482
sb.WriteRune(r)
482483
}

0 commit comments

Comments
 (0)