From 29a861e74521a5f903bccb023c86950d733fa0b7 Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Mon, 29 Jan 2007 18:28:16 +0000 Subject: Add av_create_and_push() and av_create_and_unshift_one() to refactor out two repeated idioms. p4raw-id: //depot/perl@30064 --- av.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'av.c') diff --git a/av.c b/av.c index 22eb6716f2..c6677a9ed8 100644 --- a/av.c +++ b/av.c @@ -486,6 +486,24 @@ Perl_av_undef(pTHX_ register AV *av) AvMAX(av) = AvFILLp(av) = -1; } +/* + +=for apidoc av_create_and_push + +Push an SV onto the end of the array, creating the array if necessary. +A small internal helper function to remove a commonly duplicated idiom. + +=cut +*/ + +void +Perl_av_create_and_push(pTHX_ AV **const avp, SV *const val) +{ + if (!*avp) + *avp = newAV(); + av_push(*avp, val); +} + /* =for apidoc av_push @@ -567,6 +585,26 @@ Perl_av_pop(pTHX_ register AV *av) return retval; } +/* + +=for apidoc av_create_and_unshift_one + +Unshifts an SV onto the beginning of the array, creating the array if +necessary. +A small internal helper function to remove a commonly duplicated idiom. + +=cut +*/ + +SV ** +Perl_av_create_and_unshift_one(pTHX_ AV **const avp, SV *const val) +{ + if (!*avp) + *avp = newAV(); + av_unshift(*avp, 1); + return av_store(*avp, 0, val); +} + /* =for apidoc av_unshift -- cgit v1.2.1