|
| 1 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package reflect |
| 6 | + |
| 7 | +import "iter" |
| 8 | + |
| 9 | +func rangeNum[T int8 | int16 | int32 | int64 | int | |
| 10 | + uint8 | uint16 | uint32 | uint64 | uint | |
| 11 | + uintptr, N int64 | uint64](v N) iter.Seq[Value] { |
| 12 | + return func(yield func(v Value) bool) { |
| 13 | + // cannot use range T(v) because no core type. |
| 14 | + for i := T(0); i < T(v); i++ { |
| 15 | + if !yield(ValueOf(i)) { |
| 16 | + return |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Seq returns an iter.Seq[Value] that loops over the elements of v. |
| 23 | +// If v's kind is Func, it must be a function that has no results and |
| 24 | +// that takes a single argument of type func(T) bool for some type T. |
| 25 | +// If v's kind is Pointer, the pointer element type must have kind Array. |
| 26 | +// Otherwise v's kind must be Int, Int8, Int16, Int32, Int64, |
| 27 | +// Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, |
| 28 | +// Array, Chan, Map, Slice, or String. |
| 29 | +func (v Value) Seq() iter.Seq[Value] { |
| 30 | + // TODO: canRangeFunc |
| 31 | + // if canRangeFunc(v.typ()) { |
| 32 | + // return func(yield func(Value) bool) { |
| 33 | + // rf := MakeFunc(v.Type().In(0), func(in []Value) []Value { |
| 34 | + // return []Value{ValueOf(yield(in[0]))} |
| 35 | + // }) |
| 36 | + // v.Call([]Value{rf}) |
| 37 | + // } |
| 38 | + // } |
| 39 | + switch v.Kind() { |
| 40 | + case Int: |
| 41 | + return rangeNum[int](v.Int()) |
| 42 | + case Int8: |
| 43 | + return rangeNum[int8](v.Int()) |
| 44 | + case Int16: |
| 45 | + return rangeNum[int16](v.Int()) |
| 46 | + case Int32: |
| 47 | + return rangeNum[int32](v.Int()) |
| 48 | + case Int64: |
| 49 | + return rangeNum[int64](v.Int()) |
| 50 | + case Uint: |
| 51 | + return rangeNum[uint](v.Uint()) |
| 52 | + case Uint8: |
| 53 | + return rangeNum[uint8](v.Uint()) |
| 54 | + case Uint16: |
| 55 | + return rangeNum[uint16](v.Uint()) |
| 56 | + case Uint32: |
| 57 | + return rangeNum[uint32](v.Uint()) |
| 58 | + case Uint64: |
| 59 | + return rangeNum[uint64](v.Uint()) |
| 60 | + case Uintptr: |
| 61 | + return rangeNum[uintptr](v.Uint()) |
| 62 | + case Pointer: |
| 63 | + if v.Elem().Kind() != Array { |
| 64 | + break |
| 65 | + } |
| 66 | + return func(yield func(Value) bool) { |
| 67 | + v = v.Elem() |
| 68 | + for i := 0; i < v.Len(); i++ { |
| 69 | + if !yield(ValueOf(i)) { |
| 70 | + return |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + case Array, Slice: |
| 75 | + return func(yield func(Value) bool) { |
| 76 | + for i := 0; i < v.Len(); i++ { |
| 77 | + if !yield(ValueOf(i)) { |
| 78 | + return |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + case String: |
| 83 | + return func(yield func(Value) bool) { |
| 84 | + for i := range v.String() { |
| 85 | + if !yield(ValueOf(i)) { |
| 86 | + return |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + case Map: |
| 91 | + return func(yield func(Value) bool) { |
| 92 | + i := v.MapRange() |
| 93 | + for i.Next() { |
| 94 | + if !yield(i.Key()) { |
| 95 | + return |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + case Chan: |
| 100 | + return func(yield func(Value) bool) { |
| 101 | + for value, ok := v.Recv(); ok; value, ok = v.Recv() { |
| 102 | + if !yield(value) { |
| 103 | + return |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + panic("reflect: " + v.Type().String() + " cannot produce iter.Seq[Value]") |
| 109 | +} |
| 110 | + |
| 111 | +// Seq2 returns an iter.Seq2[Value, Value] that loops over the elements of v. |
| 112 | +// If v's kind is Func, it must be a function that has no results and |
| 113 | +// that takes a single argument of type func(K, V) bool for some type K, V. |
| 114 | +// If v's kind is Pointer, the pointer element type must have kind Array. |
| 115 | +// Otherwise v's kind must be Array, Map, Slice, or String. |
| 116 | +func (v Value) Seq2() iter.Seq2[Value, Value] { |
| 117 | + // TODO: canRangeFunc2 |
| 118 | + // if canRangeFunc2(v.typ()) { |
| 119 | + // return func(yield func(Value, Value) bool) { |
| 120 | + // rf := MakeFunc(v.Type().In(0), func(in []Value) []Value { |
| 121 | + // return []Value{ValueOf(yield(in[0], in[1]))} |
| 122 | + // }) |
| 123 | + // v.Call([]Value{rf}) |
| 124 | + // } |
| 125 | + // } |
| 126 | + switch v.Kind() { |
| 127 | + case Pointer: |
| 128 | + if v.Elem().Kind() != Array { |
| 129 | + break |
| 130 | + } |
| 131 | + return func(yield func(Value, Value) bool) { |
| 132 | + v = v.Elem() |
| 133 | + for i := 0; i < v.Len(); i++ { |
| 134 | + if !yield(ValueOf(i), v.Index(i)) { |
| 135 | + return |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + case Array, Slice: |
| 140 | + return func(yield func(Value, Value) bool) { |
| 141 | + for i := 0; i < v.Len(); i++ { |
| 142 | + if !yield(ValueOf(i), v.Index(i)) { |
| 143 | + return |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + case String: |
| 148 | + return func(yield func(Value, Value) bool) { |
| 149 | + for i, v := range v.String() { |
| 150 | + if !yield(ValueOf(i), ValueOf(v)) { |
| 151 | + return |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + case Map: |
| 156 | + return func(yield func(Value, Value) bool) { |
| 157 | + i := v.MapRange() |
| 158 | + for i.Next() { |
| 159 | + if !yield(i.Key(), i.Value()) { |
| 160 | + return |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + panic("reflect: " + v.Type().String() + " cannot produce iter.Seq2[Value, Value]") |
| 166 | +} |
0 commit comments