nipN is an internal function that removes the nth item on the stack and returns it. Stack transformation: nipN(0): [... x1 x2 x3] -> [... x1 x2] nipN(1): [... x1 x2 x3] -> [... x1 x3] nipN(2): [... x1 x2 x3] -> [... x2 x3]
(idx int32)
| 144 | // nipN(1): [... x1 x2 x3] -> [... x1 x3] |
| 145 | // nipN(2): [... x1 x2 x3] -> [... x2 x3] |
| 146 | func (s *stack) nipN(idx int32) ([]byte, error) { |
| 147 | sz := int32(len(s.stk)) |
| 148 | if idx < 0 || idx > sz-1 { |
| 149 | str := fmt.Sprintf("index %d is invalid for stack size %d", idx, |
| 150 | sz) |
| 151 | return nil, scriptError(ErrInvalidStackOperation, str) |
| 152 | } |
| 153 | |
| 154 | so := s.stk[sz-idx-1] |
| 155 | if idx == 0 { |
| 156 | s.stk = s.stk[:sz-1] |
| 157 | } else if idx == sz-1 { |
| 158 | s1 := make([][]byte, sz-1) |
| 159 | copy(s1, s.stk[1:]) |
| 160 | s.stk = s1 |
| 161 | } else { |
| 162 | s1 := s.stk[sz-idx : sz] |
| 163 | s.stk = s.stk[:sz-idx-1] |
| 164 | s.stk = append(s.stk, s1...) |
| 165 | } |
| 166 | return so, nil |
| 167 | } |
| 168 | |
| 169 | // NipN removes the Nth object on the stack |
| 170 | // |
no test coverage detected