X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=av.c;h=5ac68587cc2ca2c0a5a405ec193e8227961e772a;hb=5d5aaa5e70a8a8ab4803cdb506e2096b6e190e80;hp=933e6559c306e28bf4ef9b90fb4cfd09b53278ca;hpb=d58bf5aa3d3631a46847733b1ff1985b30140228;p=p5sagit%2Fp5-mst-13.2.git diff --git a/av.c b/av.c index 933e655..5ac6858 100644 --- a/av.c +++ b/av.c @@ -16,8 +16,7 @@ #include "perl.h" void -av_reify(av) -AV* av; +av_reify(AV *av) { I32 key; SV* sv; @@ -42,9 +41,7 @@ AV* av; } void -av_extend(av,key) -AV *av; -I32 key; +av_extend(AV *av, I32 key) { dTHR; /* only necessary if we have to extend stack */ if (key > AvMAX(av)) { @@ -123,10 +120,7 @@ I32 key; } SV** -av_fetch(av,key,lval) -register AV *av; -I32 key; -I32 lval; +av_fetch(register AV *av, I32 key, I32 lval) { SV *sv; @@ -175,10 +169,7 @@ I32 lval; } SV** -av_store(av,key,val) -register AV *av; -I32 key; -SV *val; +av_store(register AV *av, I32 key, SV *val) { SV** ary; @@ -232,7 +223,7 @@ SV *val; } AV * -newAV() +newAV(void) { register AV *av; @@ -246,9 +237,7 @@ newAV() } AV * -av_make(size,strp) -register I32 size; -register SV **strp; +av_make(register I32 size, register SV **strp) { register AV *av; register I32 i; @@ -274,9 +263,7 @@ register SV **strp; } AV * -av_fake(size,strp) -register I32 size; -register SV **strp; +av_fake(register I32 size, register SV **strp) { register AV *av; register SV** ary; @@ -299,8 +286,7 @@ register SV **strp; } void -av_clear(av) -register AV *av; +av_clear(register AV *av) { register I32 key; SV** ary; @@ -333,8 +319,7 @@ register AV *av; } void -av_undef(av) -register AV *av; +av_undef(register AV *av) { register I32 key; @@ -357,9 +342,7 @@ register AV *av; } void -av_push(av,val) -register AV *av; -SV *val; +av_push(register AV *av, SV *val) { if (!av) return; @@ -367,8 +350,7 @@ SV *val; } SV * -av_pop(av) -register AV *av; +av_pop(register AV *av) { SV *retval; @@ -384,9 +366,7 @@ register AV *av; } void -av_unshift(av,num) -register AV *av; -register I32 num; +av_unshift(register AV *av, register I32 num) { register I32 i; register SV **sstr,**dstr; @@ -427,8 +407,7 @@ register I32 num; } SV * -av_shift(av) -register AV *av; +av_shift(register AV *av) { SV *retval; @@ -448,16 +427,13 @@ register AV *av; } I32 -av_len(av) -register AV *av; +av_len(register AV *av) { return AvFILL(av); } void -av_fill(av, fill) -register AV *av; -I32 fill; +av_fill(register AV *av, I32 fill) { if (!av) croak("panic: null array"); @@ -486,20 +462,36 @@ I32 fill; (void)av_store(av,fill,&sv_undef); } + +HV* +avhv_keys(AV *av) +{ + SV **keysp; + HV *keys = Nullhv; + + keysp = av_fetch(av, 0, FALSE); + if (keysp) { + if (SvGMAGICAL(*keysp)) + mg_get(*keysp); + if (SvROK(*keysp)) { + SV *hash = SvRV(*keysp); + if (SvTYPE(hash) == SVt_PVHV) + keys = (HV*)hash; + } + } + if (!keys) + croak("Can't coerce array into hash"); + return keys; +} + SV** -avhv_fetch(av, key, klen, lval) -AV *av; -char *key; -U32 klen; -I32 lval; +avhv_fetch(AV *av, char *key, U32 klen, I32 lval) { - SV **keys, **indsvp; + SV **indsvp; + HV *keys = avhv_keys(av); I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - indsvp = hv_fetch((HV*)SvRV(*keys), key, klen, FALSE); + indsvp = hv_fetch(keys, key, klen, FALSE); if (indsvp) { ind = SvIV(*indsvp); if (ind < 1) @@ -509,26 +501,20 @@ I32 lval; return 0; ind = AvFILL(av) + 1; - hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), 0); + hv_store(keys, key, klen, newSViv(ind), 0); } return av_fetch(av, ind, lval); } SV** -avhv_fetch_ent(av, keysv, lval, hash) -AV *av; -SV *keysv; -I32 lval; -U32 hash; +avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash) { - SV **keys, **indsvp; + SV **indsvp; + HV *keys = avhv_keys(av); HE *he; I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - he = hv_fetch_ent((HV*)SvRV(*keys), keysv, FALSE, hash); + he = hv_fetch_ent(keys, keysv, FALSE, hash); if (he) { ind = SvIV(HeVAL(he)); if (ind < 1) @@ -538,108 +524,73 @@ U32 hash; return 0; ind = AvFILL(av) + 1; - hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), 0); + hv_store_ent(keys, keysv, newSViv(ind), 0); } return av_fetch(av, ind, lval); } SV** -avhv_store(av, key, klen, val, hash) -AV *av; -char *key; -U32 klen; -SV *val; -U32 hash; +avhv_store(AV *av, char *key, U32 klen, SV *val, U32 hash) { - SV **keys, **indsvp; + SV **indsvp; + HV *keys = avhv_keys(av); I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - indsvp = hv_fetch((HV*)SvRV(*keys), key, klen, FALSE); + indsvp = hv_fetch(keys, key, klen, FALSE); if (indsvp) { ind = SvIV(*indsvp); if (ind < 1) croak("Bad index while coercing array into hash"); } else { ind = AvFILL(av) + 1; - hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), hash); + hv_store(keys, key, klen, newSViv(ind), hash); } return av_store(av, ind, val); } SV** -avhv_store_ent(av, keysv, val, hash) -AV *av; -SV *keysv; -SV *val; -U32 hash; +avhv_store_ent(AV *av, SV *keysv, SV *val, U32 hash) { - SV **keys; + HV *keys = avhv_keys(av); HE *he; I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - he = hv_fetch_ent((HV*)SvRV(*keys), keysv, FALSE, hash); + he = hv_fetch_ent(keys, keysv, FALSE, hash); if (he) { ind = SvIV(HeVAL(he)); if (ind < 1) croak("Bad index while coercing array into hash"); } else { ind = AvFILL(av) + 1; - hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), hash); + hv_store_ent(keys, keysv, newSViv(ind), hash); } return av_store(av, ind, val); } bool -avhv_exists_ent(av, keysv, hash) -AV *av; -SV *keysv; -U32 hash; +avhv_exists_ent(AV *av, SV *keysv, U32 hash) { - SV **keys; - - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - return hv_exists_ent((HV*)SvRV(*keys), keysv, hash); + HV *keys = avhv_keys(av); + return hv_exists_ent(keys, keysv, hash); } bool -avhv_exists(av, key, klen) -AV *av; -char *key; -U32 klen; +avhv_exists(AV *av, char *key, U32 klen) { - SV **keys; - - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - return hv_exists((HV*)SvRV(*keys), key, klen); + HV *keys = avhv_keys(av); + return hv_exists(keys, key, klen); } /* avhv_delete leaks. Caller can re-index and compress if so desired. */ SV * -avhv_delete(av, key, klen, flags) -AV *av; -char *key; -U32 klen; -I32 flags; +avhv_delete(AV *av, char *key, U32 klen, I32 flags) { - SV **keys; + HV *keys = avhv_keys(av); SV *sv; SV **svp; I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - sv = hv_delete((HV*)SvRV(*keys), key, klen, 0); + sv = hv_delete(keys, key, klen, 0); if (!sv) return Nullsv; ind = SvIV(sv); @@ -660,21 +611,14 @@ I32 flags; /* avhv_delete_ent leaks. Caller can re-index and compress if so desired. */ SV * -avhv_delete_ent(av, keysv, flags, hash) -AV *av; -SV *keysv; -I32 flags; -U32 hash; +avhv_delete_ent(AV *av, SV *keysv, I32 flags, U32 hash) { - SV **keys; + HV *keys = avhv_keys(av); SV *sv; SV **svp; I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - sv = hv_delete_ent((HV*)SvRV(*keys), keysv, 0, hash); + sv = hv_delete_ent(keys, keysv, 0, hash); if (!sv) return Nullsv; ind = SvIV(sv); @@ -694,42 +638,27 @@ U32 hash; } I32 -avhv_iterinit(av) -AV *av; +avhv_iterinit(AV *av) { - SV **keys; - - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - return hv_iterinit((HV*)SvRV(*keys)); + HV *keys = avhv_keys(av); + return hv_iterinit(keys); } HE * -avhv_iternext(av) -AV *av; +avhv_iternext(AV *av) { - SV **keys; - - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - return hv_iternext((HV*)SvRV(*keys)); + HV *keys = avhv_keys(av); + return hv_iternext(keys); } SV * -avhv_iterval(av, entry) -AV *av; -register HE *entry; +avhv_iterval(AV *av, register HE *entry) { - SV **keys; + HV *keys = avhv_keys(av); SV *sv; I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - sv = hv_iterval((HV*)SvRV(*keys), entry); + sv = hv_iterval(keys, entry); ind = SvIV(sv); if (ind < 1) croak("Bad index while coercing array into hash"); @@ -737,23 +666,18 @@ register HE *entry; } SV * -avhv_iternextsv(av, key, retlen) -AV *av; -char **key; -I32 *retlen; +avhv_iternextsv(AV *av, char **key, I32 *retlen) { - SV **keys; + HV *keys = avhv_keys(av); HE *he; SV *sv; I32 ind; - keys = av_fetch(av, 0, FALSE); - if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV) - croak("Can't coerce array into hash"); - if ( (he = hv_iternext((HV*)SvRV(*keys))) == NULL) - return NULL; + he = hv_iternext(keys); + if (!he) + return Nullsv; *key = hv_iterkey(he, retlen); - sv = hv_iterval((HV*)SvRV(*keys), he); + sv = hv_iterval(keys, he); ind = SvIV(sv); if (ind < 1) croak("Bad index while coercing array into hash");