DupN duplicates the top N items on the stack. Stack transformation: DupN(1): [... x1 x2] -> [... x1 x2 x2] DupN(2): [... x1 x2] -> [... x1 x2 x1 x2]
(n int32)
| 223 | // DupN(1): [... x1 x2] -> [... x1 x2 x2] |
| 224 | // DupN(2): [... x1 x2] -> [... x1 x2 x1 x2] |
| 225 | func (s *stack) DupN(n int32) error { |
| 226 | if n < 1 { |
| 227 | str := fmt.Sprintf("attempt to dup %d stack items", n) |
| 228 | return scriptError(ErrInvalidStackOperation, str) |
| 229 | } |
| 230 | |
| 231 | // Iteratively duplicate the value n-1 down the stack n times. |
| 232 | // This leaves an in-order duplicate of the top n items on the stack. |
| 233 | for i := n; i > 0; i-- { |
| 234 | so, err := s.PeekByteArray(n - 1) |
| 235 | if err != nil { |
| 236 | return err |
| 237 | } |
| 238 | s.PushByteArray(so) |
| 239 | } |
| 240 | return nil |
| 241 | } |
| 242 | |
| 243 | // RotN rotates the top 3N items on the stack to the left N times. |
| 244 | // |