Skip to content

Commit

Permalink
all: use simpler for loop iterations
Browse files Browse the repository at this point in the history
Two of them can just use a boolean condition, and some others can use
the new integer range language feature.

While here, replace an unnecessary "string contains byte" func.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I6a33e9d751e6d076b2e15c1e7cece061d92434ec
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198838
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mvdan committed Aug 5, 2024
1 parent dfe07b8 commit 0d9d541
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func runDef(cmd *Command, args []string) error {

iter := b.instances()
defer iter.close()
for i := 0; iter.scan(); i++ {
for iter.scan() {
var err error
if f := iter.file(); f != nil {
err = e.EncodeFile(f)
Expand Down
3 changes: 1 addition & 2 deletions cue/ast/astutil/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,7 @@ func (f *applier) After(c Cursor) bool {

func (f *applier) visitComments(p Cursor, pos int8) {
c := &f.current
for i := 0; i < len(c.cg); i++ {
cg := c.cg[i]
for i, cg := range c.cg {
if cg.Position == pos {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions cue/literal/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (f Form) Append(buf []byte, s string) []byte {
copy(nBuf, buf)
buf = nBuf
}
for i := 0; i < f.hashCount; i++ {
for range f.hashCount {
buf = append(buf, '#')
}
if f.multiline {
Expand All @@ -169,7 +169,7 @@ func (f Form) Append(buf []byte, s string) []byte {
} else {
buf = append(buf, f.quote)
}
for i := 0; i < f.hashCount; i++ {
for range f.hashCount {
buf = append(buf, '#')
}

Expand Down Expand Up @@ -280,7 +280,7 @@ func (f *Form) appendEscapedRune(buf []byte, r rune) []byte {

func (f *Form) appendEscape(buf []byte) []byte {
buf = append(buf, '\\')
for i := 0; i < f.hashCount; i++ {
for range f.hashCount {
buf = append(buf, '#')
}
return buf
Expand Down
12 changes: 1 addition & 11 deletions cue/literal/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func ParseQuotes(start, end string) (q QuoteInfo, nStart, nEnd int, err error) {
// are replaced with the corresponding non-surrogate code points.
func (q QuoteInfo) Unquote(s string) (string, error) {
if len(s) > 0 && !q.multiline {
if contains(s, '\n') || contains(s, '\r') {
if strings.ContainsAny(s, "\n\r") {
return "", errSyntax
}

Expand Down Expand Up @@ -266,16 +266,6 @@ func isSimple(s string, quote rune) bool {
return true
}

// contains reports whether the string contains the byte c.
func contains(s string, c byte) bool {
for i := 0; i < len(s); i++ {
if s[i] == c {
return true
}
}
return false
}

const (
terminatedByQuote = rune(-1)
terminatedByExpr = rune(-2)
Expand Down
4 changes: 2 additions & 2 deletions cue/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ exit:
//
// Must be compliant with https://tools.ietf.org/html/rfc4627.
func (s *Scanner) scanEscape(quote quoteInfo) (ok, interpolation bool) {
for i := 0; i < quote.numHash; i++ {
for range quote.numHash {
if s.ch != '#' {
return true, false
}
Expand Down Expand Up @@ -519,7 +519,7 @@ func (s *Scanner) consumeStringClose(ch rune, quote quoteInfo) (next rune, atEnd
}

func (s *Scanner) scanHashes(maxHash int) int {
for i := 0; i < maxHash; i++ {
for i := range maxHash {
if s.ch != '#' {
return i
}
Expand Down
6 changes: 3 additions & 3 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (o *hiddenStructValue) Lookup(key string) Value {
func (o *structValue) marshalJSON() (b []byte, err errors.Error) {
b = append(b, '{')
n := o.Len()
for i := 0; i < n; i++ {
for i := range n {
k, v := o.At(i)
s, err := internaljson.Marshal(k)
if err != nil {
Expand Down Expand Up @@ -1771,7 +1771,7 @@ func (v Value) FillPath(p Path, x interface{}) Value {
list := &adt.ListLit{}
any := &adt.Top{}
// TODO(perf): make this a constant thing. This will be possible with the query extension.
for k := 0; k < i; k++ {
for range i {
list.Elems = append(list.Elems, any)
}
list.Elems = append(list.Elems, expr, &adt.Ellipsis{})
Expand Down Expand Up @@ -2299,7 +2299,7 @@ func (v Value) Walk(before func(Value) bool, after func(Value)) {
omitHidden: true,
omitDefinitions: true,
})
for i := 0; i < obj.Len(); i++ {
for i := range obj.Len() {
_, v := obj.At(i)
// TODO: should we error on required fields, or visit them anyway?
// Walk is not designed to error at this moment, though.
Expand Down
2 changes: 1 addition & 1 deletion encoding/toml/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (d *Decoder) findArrayPrefix(key rootedKey) *openTableArray {
// appending to the given parent key and returning the unquoted string elements.
func decodeKey(key rootedKey, iter toml.Iterator) (rootedKey, []string) {
var elems []string
for i := 0; iter.Next(); i++ {
for iter.Next() {
name := string(iter.Node().Data)
// TODO(mvdan): use an append-like API once we have benchmarks
if len(key) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/par/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (w *Work[T]) Do(n int, f func(item T)) {
w.f = f
w.wait.L = &w.mu

for i := 0; i < n-1; i++ {
for range n - 1 {
go w.runner()
}
w.runner()
Expand Down
2 changes: 1 addition & 1 deletion mod/modzip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ func (e *zipError) Unwrap() error {
func strToFold(s string) string {
// Fast path: all ASCII, no upper case.
// Most paths look like this already.
for i := 0; i < len(s); i++ {
for i := range len(s) {
c := s[i]
if c >= utf8.RuneSelf || 'A' <= c && c <= 'Z' {
goto Slow
Expand Down
2 changes: 1 addition & 1 deletion pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func Repeat(x []cue.Value, count int) ([]cue.Value, error) {
return nil, fmt.Errorf("negative count")
}
var a []cue.Value
for i := 0; i < count; i++ {
for range count {
a = append(a, x...)
}
return a, nil
Expand Down
2 changes: 1 addition & 1 deletion tools/flow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *Controller) updateTaskResults(t *Task) bool {
list := &adt.ListLit{}
any := &adt.Top{}
// TODO(perf): make this a constant thing. This will be possible with the query extension.
for k := 0; k < i; k++ {
for range i {
list.Elems = append(list.Elems, any)
}
list.Elems = append(list.Elems, expr, &adt.Ellipsis{})
Expand Down

0 comments on commit 0d9d541

Please sign in to comment.