This function is equivalent to lua_next ( http://www.lua.org/manual/5.1/manual.html#lua_next ).
(key LValue)
| 349 | |
| 350 | // This function is equivalent to lua_next ( http://www.lua.org/manual/5.1/manual.html#lua_next ). |
| 351 | func (tb *LTable) Next(key LValue) (LValue, LValue) { |
| 352 | init := false |
| 353 | if key == LNil { |
| 354 | key = LNumber(0) |
| 355 | init = true |
| 356 | } |
| 357 | |
| 358 | if init || key != LNumber(0) { |
| 359 | if kv, ok := key.(LNumber); ok && isInteger(kv) && int(kv) >= 0 && kv < LNumber(MaxArrayIndex) { |
| 360 | index := int(kv) |
| 361 | if tb.array != nil { |
| 362 | for ; index < len(tb.array); index++ { |
| 363 | if v := tb.array[index]; v != LNil { |
| 364 | return LNumber(index + 1), v |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | if tb.array == nil || index == len(tb.array) { |
| 369 | if (tb.dict == nil || len(tb.dict) == 0) && (tb.strdict == nil || len(tb.strdict) == 0) { |
| 370 | return LNil, LNil |
| 371 | } |
| 372 | key = tb.keys[0] |
| 373 | if v := tb.RawGetH(key); v != LNil { |
| 374 | return key, v |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | for i := tb.k2i[key] + 1; i < len(tb.keys); i++ { |
| 381 | key := tb.keys[i] |
| 382 | if v := tb.RawGetH(key); v != LNil { |
| 383 | return key, v |
| 384 | } |
| 385 | } |
| 386 | return LNil, LNil |
| 387 | } |