This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate win32 into ansiperl
[perl5.git] / av.c
CommitLineData
a0d0e21e 1/* av.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "...for the Entwives desired order, and plenty, and peace (by which they
12 * meant that things should remain where they had set them)." --Treebeard
79072805
LW
13 */
14
15#include "EXTERN.h"
16#include "perl.h"
17
fb73857a 18void
8ac85365 19av_reify(AV *av)
a0d0e21e
LW
20{
21 I32 key;
22 SV* sv;
fb73857a
PP
23
24 if (AvREAL(av))
25 return;
a0d0e21e
LW
26 key = AvMAX(av) + 1;
27 while (key > AvFILL(av) + 1)
28 AvARRAY(av)[--key] = &sv_undef;
29 while (key) {
30 sv = AvARRAY(av)[--key];
31 assert(sv);
11343788
MB
32 if (sv != &sv_undef) {
33 dTHR;
a0d0e21e 34 (void)SvREFCNT_inc(sv);
11343788 35 }
a0d0e21e 36 }
29de640a
CS
37 key = AvARRAY(av) - AvALLOC(av);
38 while (key)
39 AvALLOC(av)[--key] = &sv_undef;
a0d0e21e
LW
40 AvREAL_on(av);
41}
42
43void
8ac85365 44av_extend(AV *av, I32 key)
a0d0e21e 45{
11343788 46 dTHR; /* only necessary if we have to extend stack */
a0d0e21e
LW
47 if (key > AvMAX(av)) {
48 SV** ary;
49 I32 tmp;
50 I32 newmax;
51
52 if (AvALLOC(av) != AvARRAY(av)) {
53 ary = AvALLOC(av) + AvFILL(av) + 1;
54 tmp = AvARRAY(av) - AvALLOC(av);
55 Move(AvARRAY(av), AvALLOC(av), AvFILL(av)+1, SV*);
56 AvMAX(av) += tmp;
57 SvPVX(av) = (char*)AvALLOC(av);
58 if (AvREAL(av)) {
59 while (tmp)
60 ary[--tmp] = &sv_undef;
61 }
62
63 if (key > AvMAX(av) - 10) {
64 newmax = key + AvMAX(av);
65 goto resize;
66 }
67 }
68 else {
69 if (AvALLOC(av)) {
c07a80fd 70#ifndef STRANGE_MALLOC
4633a7c4 71 U32 bytes;
c07a80fd 72#endif
4633a7c4 73
a0d0e21e
LW
74 newmax = key + AvMAX(av) / 5;
75 resize:
4633a7c4 76#ifdef STRANGE_MALLOC
a0d0e21e 77 Renew(AvALLOC(av),newmax+1, SV*);
4633a7c4
LW
78#else
79 bytes = (newmax + 1) * sizeof(SV*);
80#define MALLOC_OVERHEAD 16
81 tmp = MALLOC_OVERHEAD;
82 while (tmp - MALLOC_OVERHEAD < bytes)
83 tmp += tmp;
84 tmp -= MALLOC_OVERHEAD;
85 tmp /= sizeof(SV*);
86 assert(tmp > newmax);
87 newmax = tmp - 1;
88 New(2,ary, newmax+1, SV*);
89 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
fba3b22e
MB
90 if (AvMAX(av) > 64)
91 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
4633a7c4
LW
92 else
93 Safefree(AvALLOC(av));
94 AvALLOC(av) = ary;
95#endif
a0d0e21e
LW
96 ary = AvALLOC(av) + AvMAX(av) + 1;
97 tmp = newmax - AvMAX(av);
7d55f622 98 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
a0d0e21e
LW
99 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
100 stack_base = AvALLOC(av);
101 stack_max = stack_base + newmax;
102 }
103 }
104 else {
105 newmax = key < 4 ? 4 : key;
106 New(2,AvALLOC(av), newmax+1, SV*);
107 ary = AvALLOC(av) + 1;
108 tmp = newmax;
109 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
110 }
111 if (AvREAL(av)) {
112 while (tmp)
113 ary[--tmp] = &sv_undef;
114 }
115
116 SvPVX(av) = (char*)AvALLOC(av);
117 AvMAX(av) = newmax;
118 }
119 }
120}
121
79072805 122SV**
8ac85365 123av_fetch(register AV *av, I32 key, I32 lval)
79072805
LW
124{
125 SV *sv;
126
a0d0e21e
LW
127 if (!av)
128 return 0;
129
8990e307 130 if (SvRMAGICAL(av)) {
463ee0b2 131 if (mg_find((SV*)av,'P')) {
11343788 132 dTHR;
8990e307 133 sv = sv_newmortal();
463ee0b2 134 mg_copy((SV*)av, sv, 0, key);
463ee0b2
LW
135 Sv = sv;
136 return &Sv;
137 }
138 }
139
a0d0e21e
LW
140 if (key < 0) {
141 key += AvFILL(av) + 1;
142 if (key < 0)
143 return 0;
144 }
145 else if (key > AvFILL(av)) {
146 if (!lval)
147 return 0;
148 if (AvREALISH(av))
149 sv = NEWSV(5,0);
150 else
151 sv = sv_newmortal();
152 return av_store(av,key,sv);
79072805 153 }
a0d0e21e 154 if (AvARRAY(av)[key] == &sv_undef) {
4dbf4341 155 emptyness:
79072805
LW
156 if (lval) {
157 sv = NEWSV(6,0);
463ee0b2 158 return av_store(av,key,sv);
79072805
LW
159 }
160 return 0;
161 }
4dbf4341
PP
162 else if (AvREIFY(av)
163 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
164 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
165 AvARRAY(av)[key] = &sv_undef; /* 1/2 reify */
166 goto emptyness;
167 }
463ee0b2 168 return &AvARRAY(av)[key];
79072805
LW
169}
170
171SV**
8ac85365 172av_store(register AV *av, I32 key, SV *val)
79072805 173{
79072805
LW
174 SV** ary;
175
a0d0e21e
LW
176 if (!av)
177 return 0;
43fcc5d2
CS
178 if (!val)
179 val = &sv_undef;
463ee0b2 180
8990e307 181 if (SvRMAGICAL(av)) {
463ee0b2 182 if (mg_find((SV*)av,'P')) {
43fcc5d2
CS
183 if (val != &sv_undef)
184 mg_copy((SV*)av, val, 0, key);
463ee0b2
LW
185 return 0;
186 }
187 }
188
a0d0e21e
LW
189 if (key < 0) {
190 key += AvFILL(av) + 1;
191 if (key < 0)
192 return 0;
79072805 193 }
43fcc5d2
CS
194 if (SvREADONLY(av) && key >= AvFILL(av))
195 croak(no_modify);
49beac48 196 if (!AvREAL(av) && AvREIFY(av))
a0d0e21e 197 av_reify(av);
a0d0e21e
LW
198 if (key > AvMAX(av))
199 av_extend(av,key);
463ee0b2 200 ary = AvARRAY(av);
a0d0e21e
LW
201 if (AvFILL(av) < key) {
202 if (!AvREAL(av)) {
11343788 203 dTHR;
7d55f622 204 if (av == curstack && key > stack_sp - stack_base)
a0d0e21e
LW
205 stack_sp = stack_base + key; /* XPUSH in disguise */
206 do
207 ary[++AvFILL(av)] = &sv_undef;
208 while (AvFILL(av) < key);
79072805 209 }
a0d0e21e 210 AvFILL(av) = key;
79072805 211 }
a0d0e21e
LW
212 else if (AvREAL(av))
213 SvREFCNT_dec(ary[key]);
79072805 214 ary[key] = val;
8990e307 215 if (SvSMAGICAL(av)) {
a0d0e21e
LW
216 if (val != &sv_undef) {
217 MAGIC* mg = SvMAGIC(av);
218 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
219 }
463ee0b2
LW
220 mg_set((SV*)av);
221 }
79072805
LW
222 return &ary[key];
223}
224
225AV *
8ac85365 226newAV(void)
79072805 227{
463ee0b2 228 register AV *av;
79072805 229
a0d0e21e
LW
230 av = (AV*)NEWSV(3,0);
231 sv_upgrade((SV *)av, SVt_PVAV);
463ee0b2
LW
232 AvREAL_on(av);
233 AvALLOC(av) = 0;
234 SvPVX(av) = 0;
235 AvMAX(av) = AvFILL(av) = -1;
236 return av;
79072805
LW
237}
238
239AV *
8ac85365 240av_make(register I32 size, register SV **strp)
79072805 241{
463ee0b2 242 register AV *av;
79072805
LW
243 register I32 i;
244 register SV** ary;
245
a0d0e21e
LW
246 av = (AV*)NEWSV(8,0);
247 sv_upgrade((SV *) av,SVt_PVAV);
a0d0e21e 248 AvFLAGS(av) = AVf_REAL;
573fa4ea
TB
249 if (size) { /* `defined' was returning undef for size==0 anyway. */
250 New(4,ary,size,SV*);
251 AvALLOC(av) = ary;
252 SvPVX(av) = (char*)ary;
253 AvFILL(av) = size - 1;
254 AvMAX(av) = size - 1;
255 for (i = 0; i < size; i++) {
256 assert (*strp);
257 ary[i] = NEWSV(7,0);
258 sv_setsv(ary[i], *strp);
259 strp++;
260 }
79072805 261 }
463ee0b2 262 return av;
79072805
LW
263}
264
265AV *
8ac85365 266av_fake(register I32 size, register SV **strp)
79072805 267{
463ee0b2 268 register AV *av;
79072805
LW
269 register SV** ary;
270
a0d0e21e
LW
271 av = (AV*)NEWSV(9,0);
272 sv_upgrade((SV *)av, SVt_PVAV);
79072805 273 New(4,ary,size+1,SV*);
463ee0b2 274 AvALLOC(av) = ary;
79072805 275 Copy(strp,ary,size,SV*);
a0d0e21e 276 AvFLAGS(av) = AVf_REIFY;
463ee0b2
LW
277 SvPVX(av) = (char*)ary;
278 AvFILL(av) = size - 1;
279 AvMAX(av) = size - 1;
79072805 280 while (size--) {
a0d0e21e
LW
281 assert (*strp);
282 SvTEMP_off(*strp);
79072805
LW
283 strp++;
284 }
463ee0b2 285 return av;
79072805
LW
286}
287
288void
8ac85365 289av_clear(register AV *av)
79072805
LW
290{
291 register I32 key;
a0d0e21e 292 SV** ary;
79072805 293
7d55f622
PP
294#ifdef DEBUGGING
295 if (SvREFCNT(av) <= 0) {
296 warn("Attempt to clear deleted array");
297 }
298#endif
a0d0e21e 299 if (!av || AvMAX(av) < 0)
79072805
LW
300 return;
301 /*SUPPRESS 560*/
a0d0e21e
LW
302
303 if (AvREAL(av)) {
304 ary = AvARRAY(av);
305 key = AvFILL(av) + 1;
306 while (key) {
307 SvREFCNT_dec(ary[--key]);
308 ary[key] = &sv_undef;
309 }
310 }
463ee0b2
LW
311 if (key = AvARRAY(av) - AvALLOC(av)) {
312 AvMAX(av) += key;
a0d0e21e 313 SvPVX(av) = (char*)AvALLOC(av);
79072805 314 }
463ee0b2 315 AvFILL(av) = -1;
fb73857a
PP
316
317 if (SvRMAGICAL(av))
318 mg_clear((SV*)av);
79072805
LW
319}
320
321void
8ac85365 322av_undef(register AV *av)
79072805
LW
323{
324 register I32 key;
325
463ee0b2 326 if (!av)
79072805
LW
327 return;
328 /*SUPPRESS 560*/
a0d0e21e
LW
329 if (AvREAL(av)) {
330 key = AvFILL(av) + 1;
331 while (key)
332 SvREFCNT_dec(AvARRAY(av)[--key]);
333 }
463ee0b2
LW
334 Safefree(AvALLOC(av));
335 AvALLOC(av) = 0;
336 SvPVX(av) = 0;
337 AvMAX(av) = AvFILL(av) = -1;
748a9306
LW
338 if (AvARYLEN(av)) {
339 SvREFCNT_dec(AvARYLEN(av));
340 AvARYLEN(av) = 0;
341 }
79072805
LW
342}
343
a0d0e21e 344void
8ac85365 345av_push(register AV *av, SV *val)
79072805 346{
a0d0e21e
LW
347 if (!av)
348 return;
349 av_store(av,AvFILL(av)+1,val);
79072805
LW
350}
351
352SV *
8ac85365 353av_pop(register AV *av)
79072805
LW
354{
355 SV *retval;
356
a0d0e21e
LW
357 if (!av || AvFILL(av) < 0)
358 return &sv_undef;
43fcc5d2
CS
359 if (SvREADONLY(av))
360 croak(no_modify);
463ee0b2 361 retval = AvARRAY(av)[AvFILL(av)];
a0d0e21e 362 AvARRAY(av)[AvFILL(av)--] = &sv_undef;
8990e307 363 if (SvSMAGICAL(av))
463ee0b2 364 mg_set((SV*)av);
79072805
LW
365 return retval;
366}
367
368void
8ac85365 369av_unshift(register AV *av, register I32 num)
79072805
LW
370{
371 register I32 i;
372 register SV **sstr,**dstr;
373
a0d0e21e 374 if (!av || num <= 0)
79072805 375 return;
43fcc5d2
CS
376 if (SvREADONLY(av))
377 croak(no_modify);
49beac48
CS
378 if (!AvREAL(av) && AvREIFY(av))
379 av_reify(av);
a0d0e21e
LW
380 i = AvARRAY(av) - AvALLOC(av);
381 if (i) {
382 if (i > num)
383 i = num;
384 num -= i;
385
386 AvMAX(av) += i;
387 AvFILL(av) += i;
388 SvPVX(av) = (char*)(AvARRAY(av) - i);
389 }
390 if (num) {
391 av_extend(av,AvFILL(av)+num);
392 AvFILL(av) += num;
463ee0b2 393 dstr = AvARRAY(av) + AvFILL(av);
79072805
LW
394 sstr = dstr - num;
395#ifdef BUGGY_MSC5
396 # pragma loop_opt(off) /* don't loop-optimize the following code */
397#endif /* BUGGY_MSC5 */
a0d0e21e 398 for (i = AvFILL(av) - num; i >= 0; --i) {
79072805
LW
399 *dstr-- = *sstr--;
400#ifdef BUGGY_MSC5
401 # pragma loop_opt() /* loop-optimization back to command-line setting */
402#endif /* BUGGY_MSC5 */
403 }
a0d0e21e
LW
404 while (num)
405 AvARRAY(av)[--num] = &sv_undef;
79072805
LW
406 }
407}
408
409SV *
8ac85365 410av_shift(register AV *av)
79072805
LW
411{
412 SV *retval;
413
a0d0e21e
LW
414 if (!av || AvFILL(av) < 0)
415 return &sv_undef;
43fcc5d2
CS
416 if (SvREADONLY(av))
417 croak(no_modify);
463ee0b2 418 retval = *AvARRAY(av);
a0d0e21e
LW
419 if (AvREAL(av))
420 *AvARRAY(av) = &sv_undef;
463ee0b2
LW
421 SvPVX(av) = (char*)(AvARRAY(av) + 1);
422 AvMAX(av)--;
423 AvFILL(av)--;
8990e307 424 if (SvSMAGICAL(av))
463ee0b2 425 mg_set((SV*)av);
79072805
LW
426 return retval;
427}
428
429I32
8ac85365 430av_len(register AV *av)
79072805 431{
463ee0b2 432 return AvFILL(av);
79072805
LW
433}
434
435void
8ac85365 436av_fill(register AV *av, I32 fill)
79072805 437{
a0d0e21e
LW
438 if (!av)
439 croak("panic: null array");
79072805
LW
440 if (fill < 0)
441 fill = -1;
463ee0b2 442 if (fill <= AvMAX(av)) {
a0d0e21e
LW
443 I32 key = AvFILL(av);
444 SV** ary = AvARRAY(av);
445
446 if (AvREAL(av)) {
447 while (key > fill) {
448 SvREFCNT_dec(ary[key]);
449 ary[key--] = &sv_undef;
450 }
451 }
452 else {
453 while (key < fill)
454 ary[++key] = &sv_undef;
455 }
456
463ee0b2 457 AvFILL(av) = fill;
8990e307 458 if (SvSMAGICAL(av))
463ee0b2
LW
459 mg_set((SV*)av);
460 }
a0d0e21e
LW
461 else
462 (void)av_store(av,fill,&sv_undef);
79072805 463}
c750a3ec 464
5d5aaa5e
JP
465
466HV*
467avhv_keys(AV *av)
468{
469 SV **keysp;
470 HV *keys = Nullhv;
471
472 keysp = av_fetch(av, 0, FALSE);
473 if (keysp) {
d627ae4e
MB
474 SV *sv = *keysp;
475 if (SvGMAGICAL(sv))
476 mg_get(sv);
477 if (SvROK(sv)) {
478 sv = SvRV(sv);
479 if (SvTYPE(sv) == SVt_PVHV)
480 keys = (HV*)sv;
5d5aaa5e
JP
481 }
482 }
483 if (!keys)
484 croak("Can't coerce array into hash");
485 return keys;
486}
487
c750a3ec 488SV**
8ac85365 489avhv_fetch(AV *av, char *key, U32 klen, I32 lval)
c750a3ec 490{
5d5aaa5e
JP
491 SV **indsvp;
492 HV *keys = avhv_keys(av);
c750a3ec
MB
493 I32 ind;
494
5d5aaa5e 495 indsvp = hv_fetch(keys, key, klen, FALSE);
c750a3ec
MB
496 if (indsvp) {
497 ind = SvIV(*indsvp);
498 if (ind < 1)
499 croak("Bad index while coercing array into hash");
500 } else {
501 if (!lval)
502 return 0;
503
504 ind = AvFILL(av) + 1;
5d5aaa5e 505 hv_store(keys, key, klen, newSViv(ind), 0);
c750a3ec
MB
506 }
507 return av_fetch(av, ind, lval);
508}
509
510SV**
8ac85365 511avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash)
97fcbf96 512{
5d5aaa5e
JP
513 SV **indsvp;
514 HV *keys = avhv_keys(av);
97fcbf96
MB
515 HE *he;
516 I32 ind;
517
5d5aaa5e 518 he = hv_fetch_ent(keys, keysv, FALSE, hash);
97fcbf96
MB
519 if (he) {
520 ind = SvIV(HeVAL(he));
521 if (ind < 1)
522 croak("Bad index while coercing array into hash");
523 } else {
524 if (!lval)
525 return 0;
526
527 ind = AvFILL(av) + 1;
5d5aaa5e 528 hv_store_ent(keys, keysv, newSViv(ind), 0);
97fcbf96
MB
529 }
530 return av_fetch(av, ind, lval);
531}
532
533SV**
8ac85365 534avhv_store(AV *av, char *key, U32 klen, SV *val, U32 hash)
c750a3ec 535{
5d5aaa5e
JP
536 SV **indsvp;
537 HV *keys = avhv_keys(av);
c750a3ec
MB
538 I32 ind;
539
5d5aaa5e 540 indsvp = hv_fetch(keys, key, klen, FALSE);
c750a3ec
MB
541 if (indsvp) {
542 ind = SvIV(*indsvp);
543 if (ind < 1)
544 croak("Bad index while coercing array into hash");
545 } else {
546 ind = AvFILL(av) + 1;
5d5aaa5e 547 hv_store(keys, key, klen, newSViv(ind), hash);
c750a3ec
MB
548 }
549 return av_store(av, ind, val);
550}
551
5bc6513d 552SV**
8ac85365 553avhv_store_ent(AV *av, SV *keysv, SV *val, U32 hash)
5bc6513d 554{
5d5aaa5e 555 HV *keys = avhv_keys(av);
5bc6513d
MB
556 HE *he;
557 I32 ind;
558
5d5aaa5e 559 he = hv_fetch_ent(keys, keysv, FALSE, hash);
5bc6513d
MB
560 if (he) {
561 ind = SvIV(HeVAL(he));
562 if (ind < 1)
563 croak("Bad index while coercing array into hash");
564 } else {
565 ind = AvFILL(av) + 1;
5d5aaa5e 566 hv_store_ent(keys, keysv, newSViv(ind), hash);
5bc6513d
MB
567 }
568 return av_store(av, ind, val);
569}
570
c750a3ec 571bool
8ac85365 572avhv_exists_ent(AV *av, SV *keysv, U32 hash)
97fcbf96 573{
5d5aaa5e
JP
574 HV *keys = avhv_keys(av);
575 return hv_exists_ent(keys, keysv, hash);
97fcbf96
MB
576}
577
578bool
8ac85365 579avhv_exists(AV *av, char *key, U32 klen)
c750a3ec 580{
5d5aaa5e
JP
581 HV *keys = avhv_keys(av);
582 return hv_exists(keys, key, klen);
c750a3ec
MB
583}
584
585/* avhv_delete leaks. Caller can re-index and compress if so desired. */
586SV *
8ac85365 587avhv_delete(AV *av, char *key, U32 klen, I32 flags)
c750a3ec 588{
5d5aaa5e 589 HV *keys = avhv_keys(av);
c750a3ec
MB
590 SV *sv;
591 SV **svp;
592 I32 ind;
593
5d5aaa5e 594 sv = hv_delete(keys, key, klen, 0);
c750a3ec
MB
595 if (!sv)
596 return Nullsv;
597 ind = SvIV(sv);
598 if (ind < 1)
599 croak("Bad index while coercing array into hash");
600 svp = av_fetch(av, ind, FALSE);
601 if (!svp)
602 return Nullsv;
603 if (flags & G_DISCARD) {
604 sv = Nullsv;
605 SvREFCNT_dec(*svp);
606 } else {
607 sv = sv_2mortal(*svp);
608 }
609 *svp = &sv_undef;
610 return sv;
611}
612
97fcbf96
MB
613/* avhv_delete_ent leaks. Caller can re-index and compress if so desired. */
614SV *
8ac85365 615avhv_delete_ent(AV *av, SV *keysv, I32 flags, U32 hash)
97fcbf96 616{
5d5aaa5e 617 HV *keys = avhv_keys(av);
97fcbf96
MB
618 SV *sv;
619 SV **svp;
620 I32 ind;
621
5d5aaa5e 622 sv = hv_delete_ent(keys, keysv, 0, hash);
97fcbf96
MB
623 if (!sv)
624 return Nullsv;
625 ind = SvIV(sv);
626 if (ind < 1)
627 croak("Bad index while coercing array into hash");
628 svp = av_fetch(av, ind, FALSE);
629 if (!svp)
630 return Nullsv;
631 if (flags & G_DISCARD) {
632 sv = Nullsv;
633 SvREFCNT_dec(*svp);
634 } else {
635 sv = sv_2mortal(*svp);
636 }
637 *svp = &sv_undef;
638 return sv;
639}
640
c750a3ec 641I32
8ac85365 642avhv_iterinit(AV *av)
c750a3ec 643{
5d5aaa5e
JP
644 HV *keys = avhv_keys(av);
645 return hv_iterinit(keys);
c750a3ec
MB
646}
647
648HE *
8ac85365 649avhv_iternext(AV *av)
c750a3ec 650{
5d5aaa5e
JP
651 HV *keys = avhv_keys(av);
652 return hv_iternext(keys);
c750a3ec
MB
653}
654
655SV *
8ac85365 656avhv_iterval(AV *av, register HE *entry)
c750a3ec 657{
5d5aaa5e 658 HV *keys = avhv_keys(av);
c750a3ec
MB
659 SV *sv;
660 I32 ind;
661
5d5aaa5e 662 sv = hv_iterval(keys, entry);
c750a3ec
MB
663 ind = SvIV(sv);
664 if (ind < 1)
665 croak("Bad index while coercing array into hash");
666 return *av_fetch(av, ind, TRUE);
667}
668
669SV *
8ac85365 670avhv_iternextsv(AV *av, char **key, I32 *retlen)
c750a3ec 671{
5d5aaa5e 672 HV *keys = avhv_keys(av);
c750a3ec
MB
673 HE *he;
674 SV *sv;
675 I32 ind;
676
5d5aaa5e
JP
677 he = hv_iternext(keys);
678 if (!he)
679 return Nullsv;
c750a3ec 680 *key = hv_iterkey(he, retlen);
5d5aaa5e 681 sv = hv_iterval(keys, he);
c750a3ec
MB
682 ind = SvIV(sv);
683 if (ind < 1)
684 croak("Bad index while coercing array into hash");
685 return *av_fetch(av, ind, TRUE);
686}