Skip to content

Commit

Permalink
Use range over integer support in Go 1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnet committed Apr 23, 2024
1 parent af2d506 commit 34e76e8
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions arshal_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ func makeSliceArshaler(t reflect.Type) *arshaler {
if mo.Marshalers != nil {
marshal, _ = mo.Marshalers.(*Marshalers).lookup(marshal, t.Elem())
}
for i := 0; i < n; i++ {
for i := range n {
v := addressableValue{va.Index(i)} // indexed slice element is always addressable
if err := marshal(enc, v, mo); err != nil {
return err
Expand Down Expand Up @@ -1372,7 +1372,7 @@ func makeArrayArshaler(t reflect.Type) *arshaler {
if mo.Marshalers != nil {
marshal, _ = mo.Marshalers.(*Marshalers).lookup(marshal, t.Elem())
}
for i := 0; i < n; i++ {
for i := range n {
v := addressableValue{va.Index(i)} // indexed array element is addressable if array is addressable
if err := marshal(enc, v, mo); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions arshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@ func TestMarshal(t *testing.T) {
name: jsontest.Name("Structs/OmitEmpty/PathologicalBreadth"),
in: func() any {
var fields []reflect.StructField
for i := 0; i < 100; i++ {
for i := range 100 {
fields = append(fields, reflect.StructField{
Name: fmt.Sprintf("X%d", i),
Type: reflect.TypeFor[stringMarshalEmpty](),
Expand Down Expand Up @@ -7897,7 +7897,7 @@ func TestUnmarshal(t *testing.T) {
if _, err := dec.ReadToken(); err != nil {
return err
}
for i := 0; i < len(*v); i++ {
for i := range len(*v) {
if err := UnmarshalDecode(dec, &(*v)[i], opts); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var arshalTestdata = []struct {
name: "Map/ManyEmpty",
raw: []byte(`[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]`),
val: addr(func() (out []map[string]string) {
for i := 0; i < 100; i++ {
for range 100 {
out = append(out, map[string]string{})
}
return out
Expand Down Expand Up @@ -109,7 +109,7 @@ var arshalTestdata = []struct {
name: "Slice/ManyEmpty",
raw: []byte(`[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]`),
val: addr(func() (out [][]string) {
for i := 0; i < 100; i++ {
for range 100 {
out = append(out, []string{})
}
return out
Expand Down Expand Up @@ -622,7 +622,7 @@ func runTestOrBench(tb testing.TB, name string, numBytes int64, run func(tb test
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(numBytes)
for i := 0; i < b.N; i++ {
for range b.N {
run(b)
}
})
Expand Down
2 changes: 1 addition & 1 deletion diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func TestMapDeterminism(t *testing.T) {
for _, json := range jsonPackages {
t.Run(path.Join("Marshal", json.Version), func(t *testing.T) {
outs := make(map[string]bool)
for i := 0; i < iterations; i++ {
for range iterations {
b, err := json.Marshal(in)
if err != nil {
t.Fatalf("json.Marshal error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func makeStructFields(root reflect.Type) (structFields, *SemanticError) {
namesIndex := make(map[string]int) // index of each field with a given JSON object name in current struct
var hasAnyJSONTag bool // whether any Go struct field has a `json` tag
var hasAnyJSONField bool // whether any JSON serializable fields exist in current struct
for i := 0; i < t.NumField(); i++ {
for i := range t.NumField() {
sf := t.Field(i)
_, hasTag := sf.Tag.Lookup("json")
hasAnyJSONTag = hasAnyJSONTag || hasTag
Expand Down
4 changes: 2 additions & 2 deletions fold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestFoldRune(t *testing.T) {
}

var foldSet []rune
for r := rune(0); r <= unicode.MaxRune; r++ {
for r := range rune(unicode.MaxRune + 1) {
// Derive all runes that are all part of the same fold set.
foldSet = foldSet[:0]
for r0 := r; r != r0 || len(foldSet) == 0; r = unicode.SimpleFold(r) {
Expand Down Expand Up @@ -117,7 +117,7 @@ func runUnmarshalUnknown(tb testing.TB) {
}

var fields []reflect.StructField
for i := 0; i < n; i++ {
for i := range n {
fields = append(fields, reflect.StructField{
Name: fmt.Sprintf("Name%d", i),
Type: reflect.TypeFor[int](),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// This package will regularly experience breaking changes.
module github.com/go-json-experiment/json

go 1.21
go 1.22
16 changes: 8 additions & 8 deletions intern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
func TestIntern(t *testing.T) {
var sc stringCache
const alphabet = "abcdefghijklmnopqrstuvwxyz"
for i := 0; i <= len(alphabet); i++ {
for i := range len(alphabet) + 1 {
want := alphabet[i:]
if got := makeString(&sc, []byte(want)); got != want {
t.Fatalf("make = %v, want %v", got, want)
}
}
for i := 0; i < 1000; i++ {
for i := range 1000 {
want := fmt.Sprintf("test%b", i)
if got := makeString(&sc, []byte(want)); got != want {
t.Fatalf("make = %v, want %v", got, want)
Expand Down Expand Up @@ -65,7 +65,7 @@ func BenchmarkIntern(b *testing.B) {
}{
// Best is the best case scenario where every string is the same.
{"Best", func() (out [][]byte) {
for i := 0; i < 1000; i++ {
for range 1000 {
out = append(out, []byte("hello, world!"))
}
return out
Expand All @@ -75,7 +75,7 @@ func BenchmarkIntern(b *testing.B) {
// This commonly occurs when unmarshaling a JSON array of JSON objects,
// where the set of all names is usually small.
{"Repeat", func() (out [][]byte) {
for i := 0; i < 100; i++ {
for range 100 {
for _, s := range []string{"first_name", "last_name", "age", "address", "street_address", "city", "state", "postal_code", "phone_numbers", "gender"} {
out = append(out, []byte(s))
}
Expand All @@ -92,7 +92,7 @@ func BenchmarkIntern(b *testing.B) {
// Worst is the worst case scenario where every string is different
// resulting in wasted time looking up a string that will never match.
{"Worst", func() (out [][]byte) {
for i := 0; i < 1000; i++ {
for i := range 1000 {
out = append(out, []byte(fmt.Sprintf("%016x", i)))
}
return out
Expand All @@ -105,7 +105,7 @@ func BenchmarkIntern(b *testing.B) {
// This provides an upper bound on the number of allocations.
b.Run("Alloc", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
for _, b := range tt.data {
sink = string(b)
}
Expand All @@ -116,7 +116,7 @@ func BenchmarkIntern(b *testing.B) {
// and also keeping the number of allocations closer to GoMap.
b.Run("Cache", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
var sc stringCache
for _, b := range tt.data {
sink = makeString(&sc, b)
Expand All @@ -127,7 +127,7 @@ func BenchmarkIntern(b *testing.B) {
// This provides a lower bound on the number of allocations.
b.Run("GoMap", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
m := make(map[string]string)
for _, b := range tt.data {
s, ok := m[string(b)]
Expand Down
6 changes: 3 additions & 3 deletions internal/jsonopts/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,23 @@ var sink struct {
func BenchmarkGetBool(b *testing.B) {
b.ReportAllocs()
opts := json.DefaultOptionsV2()
for i := 0; i < b.N; i++ {
for range b.N {
sink.Bool, sink.Bool = json.GetOption(opts, jsontext.AllowDuplicateNames)
}
}

func BenchmarkGetIndent(b *testing.B) {
b.ReportAllocs()
opts := json.DefaultOptionsV2()
for i := 0; i < b.N; i++ {
for range b.N {
sink.String, sink.Bool = json.GetOption(opts, jsontext.WithIndent)
}
}

func BenchmarkGetMarshalers(b *testing.B) {
b.ReportAllocs()
opts := json.JoinOptions(json.DefaultOptionsV2(), json.WithMarshalers(nil))
for i := 0; i < b.N; i++ {
for range b.N {
sink.Marshalers, sink.Bool = json.GetOption(opts, json.WithMarshalers)
}
}
4 changes: 2 additions & 2 deletions internal/jsonwire/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func AppendUnquote[Bytes ~[]byte | ~string](dst []byte, src Bytes) (v []byte, er
// hasEscapedUTF16Prefix reports whether b is possibly
// the truncated prefix of a \uFFFF escape sequence.
func hasEscapedUTF16Prefix[Bytes ~[]byte | ~string](b Bytes, lowerSurrogateHalf bool) bool {
for i := 0; i < len(b); i++ {
for i := range len(b) {
switch c := b[i]; {
case i == 0 && c != '\\':
return false
Expand Down Expand Up @@ -567,7 +567,7 @@ func parseHexUint16[Bytes ~[]byte | ~string](b Bytes) (v uint16, ok bool) {
if len(b) != 4 {
return 0, false
}
for i := 0; i < 4; i++ {
for i := range 4 {
c := b[i]
switch {
case '0' <= c && c <= '9':
Expand Down
20 changes: 10 additions & 10 deletions jsontext/coder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,10 @@ func TestCoderMaxDepth(t *testing.T) {
})
t.Run("ArraysValid/AllTokens", func(t *testing.T) {
dec.s.reset(trimArray(maxArrays), nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkReadToken(t, '[', nil)
}
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkReadToken(t, ']', nil)
}
})
Expand All @@ -637,7 +637,7 @@ func TestCoderMaxDepth(t *testing.T) {
})
t.Run("ArraysInvalid/AllTokens", func(t *testing.T) {
dec.s.reset(maxArrays, nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkReadToken(t, '[', nil)
}
checkReadToken(t, 0, errMaxDepth.withOffset(maxNestingDepth))
Expand All @@ -656,12 +656,12 @@ func TestCoderMaxDepth(t *testing.T) {
})
t.Run("ObjectsValid/AllTokens", func(t *testing.T) {
dec.s.reset(trimObject(maxObjects), nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkReadToken(t, '{', nil)
checkReadToken(t, '"', nil)
}
checkReadToken(t, '"', nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkReadToken(t, '}', nil)
}
})
Expand All @@ -678,7 +678,7 @@ func TestCoderMaxDepth(t *testing.T) {
})
t.Run("ObjectsInvalid/AllTokens", func(t *testing.T) {
dec.s.reset(maxObjects, nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkReadToken(t, '{', nil)
checkReadToken(t, '"', nil)
}
Expand Down Expand Up @@ -715,11 +715,11 @@ func TestCoderMaxDepth(t *testing.T) {
})
t.Run("Arrays/AllTokens", func(t *testing.T) {
enc.s.reset(enc.s.Buf[:0], nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkWriteToken(t, ArrayStart, nil)
}
checkWriteToken(t, ArrayStart, errMaxDepth.withOffset(maxNestingDepth))
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkWriteToken(t, ArrayEnd, nil)
}
})
Expand All @@ -739,15 +739,15 @@ func TestCoderMaxDepth(t *testing.T) {
})
t.Run("Objects/AllTokens", func(t *testing.T) {
enc.s.reset(enc.s.Buf[:0], nil)
for i := 0; i < maxNestingDepth-1; i++ {
for range maxNestingDepth - 1 {
checkWriteToken(t, ObjectStart, nil)
checkWriteToken(t, String(""), nil)
}
checkWriteToken(t, ObjectStart, nil)
checkWriteToken(t, String(""), nil)
checkWriteToken(t, ObjectStart, errMaxDepth.withOffset(maxNestingDepth*len64(`{"":`)))
checkWriteToken(t, String(""), nil)
for i := 0; i < maxNestingDepth; i++ {
for range maxNestingDepth {
checkWriteToken(t, ObjectEnd, nil)
}
})
Expand Down
2 changes: 1 addition & 1 deletion jsontext/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (m stateMachine) checkDelim(delim byte, next Kind) error {
// Mark the namespaces as invalid so that future method calls on
// Encoder or Decoder will return an error.
func (m *stateMachine) InvalidateDisabledNamespaces() {
for i := 0; i < m.Depth(); i++ {
for i := range m.Depth() {
e := m.index(i)
if !e.isActiveNamespace() {
e.invalidateNamespace()
Expand Down
6 changes: 3 additions & 3 deletions jsontext/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestStateMachine(t *testing.T) {
switch op := op.(type) {
case stackLengths:
var got []int64
for i := 0; i < state.Depth(); i++ {
for i := range state.Depth() {
e := state.index(i)
got = append(got, e.Length())
}
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestObjectNamespace(t *testing.T) {

// Check that the namespace is consistent.
gotNames := []string{}
for i := 0; i < ns.length(); i++ {
for i := range ns.length() {
gotNames = append(gotNames, string(ns.getUnquoted(i)))
}
if !reflect.DeepEqual(gotNames, wantNames) {
Expand All @@ -325,7 +325,7 @@ func TestObjectNamespace(t *testing.T) {
}

// Insert a large number of names.
for i := 0; i < 64; i++ {
for i := range 64 {
ns.InsertUnquoted([]byte(fmt.Sprintf(`name%d`, i)))
}

Expand Down

0 comments on commit 34e76e8

Please sign in to comment.