iterator.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package tengo
  2. // Iterator represents an iterator for underlying data type.
  3. type Iterator interface {
  4. Object
  5. // Next returns true if there are more elements to iterate.
  6. Next() bool
  7. // Key returns the key or index value of the current element.
  8. Key() Object
  9. // Value returns the value of the current element.
  10. Value() Object
  11. }
  12. // ArrayIterator is an iterator for an array.
  13. type ArrayIterator struct {
  14. ObjectImpl
  15. v []Object
  16. i int
  17. l int
  18. }
  19. // TypeName returns the name of the type.
  20. func (i *ArrayIterator) TypeName() string {
  21. return "array-iterator"
  22. }
  23. func (i *ArrayIterator) String() string {
  24. return "<array-iterator>"
  25. }
  26. // IsFalsy returns true if the value of the type is falsy.
  27. func (i *ArrayIterator) IsFalsy() bool {
  28. return true
  29. }
  30. // Equals returns true if the value of the type is equal to the value of
  31. // another object.
  32. func (i *ArrayIterator) Equals(Object) bool {
  33. return false
  34. }
  35. // Copy returns a copy of the type.
  36. func (i *ArrayIterator) Copy() Object {
  37. return &ArrayIterator{v: i.v, i: i.i, l: i.l}
  38. }
  39. // Next returns true if there are more elements to iterate.
  40. func (i *ArrayIterator) Next() bool {
  41. i.i++
  42. return i.i <= i.l
  43. }
  44. // Key returns the key or index value of the current element.
  45. func (i *ArrayIterator) Key() Object {
  46. return &Int{Value: int64(i.i - 1)}
  47. }
  48. // Value returns the value of the current element.
  49. func (i *ArrayIterator) Value() Object {
  50. return i.v[i.i-1]
  51. }
  52. // BytesIterator represents an iterator for a string.
  53. type BytesIterator struct {
  54. ObjectImpl
  55. v []byte
  56. i int
  57. l int
  58. }
  59. // TypeName returns the name of the type.
  60. func (i *BytesIterator) TypeName() string {
  61. return "bytes-iterator"
  62. }
  63. func (i *BytesIterator) String() string {
  64. return "<bytes-iterator>"
  65. }
  66. // Equals returns true if the value of the type is equal to the value of
  67. // another object.
  68. func (i *BytesIterator) Equals(Object) bool {
  69. return false
  70. }
  71. // Copy returns a copy of the type.
  72. func (i *BytesIterator) Copy() Object {
  73. return &BytesIterator{v: i.v, i: i.i, l: i.l}
  74. }
  75. // Next returns true if there are more elements to iterate.
  76. func (i *BytesIterator) Next() bool {
  77. i.i++
  78. return i.i <= i.l
  79. }
  80. // Key returns the key or index value of the current element.
  81. func (i *BytesIterator) Key() Object {
  82. return &Int{Value: int64(i.i - 1)}
  83. }
  84. // Value returns the value of the current element.
  85. func (i *BytesIterator) Value() Object {
  86. return &Int{Value: int64(i.v[i.i-1])}
  87. }
  88. // MapIterator represents an iterator for the map.
  89. type MapIterator struct {
  90. ObjectImpl
  91. v map[string]Object
  92. k []string
  93. i int
  94. l int
  95. }
  96. // TypeName returns the name of the type.
  97. func (i *MapIterator) TypeName() string {
  98. return "map-iterator"
  99. }
  100. func (i *MapIterator) String() string {
  101. return "<map-iterator>"
  102. }
  103. // IsFalsy returns true if the value of the type is falsy.
  104. func (i *MapIterator) IsFalsy() bool {
  105. return true
  106. }
  107. // Equals returns true if the value of the type is equal to the value of
  108. // another object.
  109. func (i *MapIterator) Equals(Object) bool {
  110. return false
  111. }
  112. // Copy returns a copy of the type.
  113. func (i *MapIterator) Copy() Object {
  114. return &MapIterator{v: i.v, k: i.k, i: i.i, l: i.l}
  115. }
  116. // Next returns true if there are more elements to iterate.
  117. func (i *MapIterator) Next() bool {
  118. i.i++
  119. return i.i <= i.l
  120. }
  121. // Key returns the key or index value of the current element.
  122. func (i *MapIterator) Key() Object {
  123. k := i.k[i.i-1]
  124. return &String{Value: k}
  125. }
  126. // Value returns the value of the current element.
  127. func (i *MapIterator) Value() Object {
  128. k := i.k[i.i-1]
  129. return i.v[k]
  130. }
  131. // StringIterator represents an iterator for a string.
  132. type StringIterator struct {
  133. ObjectImpl
  134. v []rune
  135. i int
  136. l int
  137. }
  138. // TypeName returns the name of the type.
  139. func (i *StringIterator) TypeName() string {
  140. return "string-iterator"
  141. }
  142. func (i *StringIterator) String() string {
  143. return "<string-iterator>"
  144. }
  145. // IsFalsy returns true if the value of the type is falsy.
  146. func (i *StringIterator) IsFalsy() bool {
  147. return true
  148. }
  149. // Equals returns true if the value of the type is equal to the value of
  150. // another object.
  151. func (i *StringIterator) Equals(Object) bool {
  152. return false
  153. }
  154. // Copy returns a copy of the type.
  155. func (i *StringIterator) Copy() Object {
  156. return &StringIterator{v: i.v, i: i.i, l: i.l}
  157. }
  158. // Next returns true if there are more elements to iterate.
  159. func (i *StringIterator) Next() bool {
  160. i.i++
  161. return i.i <= i.l
  162. }
  163. // Key returns the key or index value of the current element.
  164. func (i *StringIterator) Key() Object {
  165. return &Int{Value: int64(i.i - 1)}
  166. }
  167. // Value returns the value of the current element.
  168. func (i *StringIterator) Value() Object {
  169. return &Char{Value: i.v[i.i-1]}
  170. }