change to quiet cygwin's perlld
[p5sagit/p5-mst-13.2.git] / ext / List / Util / Util.xs
CommitLineData
f4a2945e 1/* Copyright (c) 1997-2000 Graham Barr <gbarr@pobox.com>. All rights reserved.
2 * This program is free software; you can redistribute it and/or
3 * modify it under the same terms as Perl itself.
4 */
5
6#include <EXTERN.h>
7#include <perl.h>
8#include <XSUB.h>
f4a2945e 9
92731555 10#ifndef PERL_VERSION
97605c51 11# include <patchlevel.h>
12# if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL)))
13# include <could_not_find_Perl_patchlevel.h>
14# endif
92731555 15# define PERL_REVISION 5
16# define PERL_VERSION PATCHLEVEL
17# define PERL_SUBVERSION SUBVERSION
18#endif
19
1bfb5477 20#ifndef aTHX
21# define aTHX
9c3c560b 22# define pTHX
23#endif
24
25/* Some platforms have strict exports. And before 5.7.3 cxinc (or Perl_cxinc)
26 was not exported. Therefore platforms like win32, VMS etc have problems
27 so we redefine it here -- GMB
28*/
29#if PERL_VERSION < 7
30/* Not in 5.6.1. */
31# define SvUOK(sv) SvIOK_UV(sv)
32# ifdef cxinc
33# undef cxinc
34# endif
35# define cxinc() my_cxinc(aTHX)
36static I32
37my_cxinc(pTHX)
38{
39 cxstack_max = cxstack_max * 3 / 2;
40 Renew(cxstack, cxstack_max + 1, struct context); /* XXX should fix CXINC macro */
41 return cxstack_ix + 1;
42}
1bfb5477 43#endif
44
45#if PERL_VERSION < 6
46# define NV double
47#endif
48
60f3865b 49#ifdef SVf_IVisUV
b9ae0a2d 50# define slu_sv_value(sv) (SvIOK(sv)) ? (SvIOK_UV(sv)) ? (NV)(SvUVX(sv)) : (NV)(SvIVX(sv)) : (SvNV(sv))
60f3865b 51#else
aaaf1885 52# define slu_sv_value(sv) (SvIOK(sv)) ? (NV)(SvIVX(sv)) : (SvNV(sv))
60f3865b 53#endif
54
1bfb5477 55#ifndef Drand01
56# define Drand01() ((rand() & 0x7FFF) / (double) ((unsigned long)1 << 15))
57#endif
58
92731555 59#if PERL_VERSION < 5
f4a2945e 60# ifndef gv_stashpvn
61# define gv_stashpvn(n,l,c) gv_stashpv(n,c)
62# endif
63# ifndef SvTAINTED
64
65static bool
66sv_tainted(SV *sv)
67{
68 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
69 MAGIC *mg = mg_find(sv, 't');
70 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
71 return TRUE;
72 }
73 return FALSE;
74}
75
76# define SvTAINTED_on(sv) sv_magic((sv), Nullsv, 't', Nullch, 0)
77# define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
78# endif
79# define PL_defgv defgv
80# define PL_op op
81# define PL_curpad curpad
82# define CALLRUNOPS runops
83# define PL_curpm curpm
84# define PL_sv_undef sv_undef
85# define PERL_CONTEXT struct context
86#endif
92731555 87#if (PERL_VERSION < 5) || (PERL_VERSION == 5 && PERL_SUBVERSION <50)
f4a2945e 88# ifndef PL_tainting
89# define PL_tainting tainting
90# endif
91# ifndef PL_stack_base
92# define PL_stack_base stack_base
93# endif
94# ifndef PL_stack_sp
95# define PL_stack_sp stack_sp
96# endif
97# ifndef PL_ppaddr
98# define PL_ppaddr ppaddr
99# endif
100#endif
101
9e7deb6c 102#ifndef PTR2UV
103# define PTR2UV(ptr) (UV)(ptr)
60f3865b 104#endif
105
27da23d5 106#ifdef HASATTRIBUTE
107# if (defined(__GNUC__) && defined(__cplusplus)) || defined(__INTEL_COMPILER)
108# define PERL_UNUSED_DECL
109# else
110# define PERL_UNUSED_DECL __attribute__((unused))
111# endif
112#else
113# define PERL_UNUSED_DECL
114#endif
115
116#ifndef dNOOP
117#define dNOOP extern int Perl___notused PERL_UNUSED_DECL
118#endif
119
120#ifndef dVAR
121#define dVAR dNOOP
122#endif
123
f4a2945e 124MODULE=List::Util PACKAGE=List::Util
125
126void
127min(...)
128PROTOTYPE: @
129ALIAS:
130 min = 0
131 max = 1
132CODE:
133{
134 int index;
135 NV retval;
136 SV *retsv;
137 if(!items) {
138 XSRETURN_UNDEF;
139 }
140 retsv = ST(0);
60f3865b 141 retval = slu_sv_value(retsv);
f4a2945e 142 for(index = 1 ; index < items ; index++) {
143 SV *stacksv = ST(index);
60f3865b 144 NV val = slu_sv_value(stacksv);
f4a2945e 145 if(val < retval ? !ix : ix) {
146 retsv = stacksv;
147 retval = val;
148 }
149 }
150 ST(0) = retsv;
151 XSRETURN(1);
152}
153
154
155
156NV
157sum(...)
158PROTOTYPE: @
159CODE:
160{
60f3865b 161 SV *sv;
f4a2945e 162 int index;
f4a2945e 163 if(!items) {
164 XSRETURN_UNDEF;
165 }
60f3865b 166 sv = ST(0);
167 RETVAL = slu_sv_value(sv);
f4a2945e 168 for(index = 1 ; index < items ; index++) {
60f3865b 169 sv = ST(index);
170 RETVAL += slu_sv_value(sv);
f4a2945e 171 }
172}
173OUTPUT:
174 RETVAL
175
176
177void
178minstr(...)
179PROTOTYPE: @
180ALIAS:
181 minstr = 2
182 maxstr = 0
183CODE:
184{
185 SV *left;
186 int index;
187 if(!items) {
188 XSRETURN_UNDEF;
189 }
190 /*
191 sv_cmp & sv_cmp_locale return 1,0,-1 for gt,eq,lt
192 so we set ix to the value we are looking for
193 xsubpp does not allow -ve values, so we start with 0,2 and subtract 1
194 */
195 ix -= 1;
196 left = ST(0);
197#ifdef OPpLOCALE
198 if(MAXARG & OPpLOCALE) {
199 for(index = 1 ; index < items ; index++) {
200 SV *right = ST(index);
201 if(sv_cmp_locale(left, right) == ix)
202 left = right;
203 }
204 }
205 else {
206#endif
207 for(index = 1 ; index < items ; index++) {
208 SV *right = ST(index);
209 if(sv_cmp(left, right) == ix)
210 left = right;
211 }
212#ifdef OPpLOCALE
213 }
214#endif
215 ST(0) = left;
216 XSRETURN(1);
217}
218
219
220
221void
222reduce(block,...)
223 SV * block
224PROTOTYPE: &@
225CODE:
226{
27da23d5 227 dVAR;
09c2a9b8 228 SV *ret = sv_newmortal();
f4a2945e 229 int index;
f4a2945e 230 GV *agv,*bgv,*gv;
231 HV *stash;
232 CV *cv;
233 OP *reducecop;
1bfb5477 234 PERL_CONTEXT *cx;
235 SV** newsp;
236 I32 gimme = G_SCALAR;
c5661c80 237 U8 hasargs = 0;
1bfb5477 238 bool oldcatch = CATCH_GET;
239
f4a2945e 240 if(items <= 1) {
241 XSRETURN_UNDEF;
242 }
243 agv = gv_fetchpv("a", TRUE, SVt_PV);
244 bgv = gv_fetchpv("b", TRUE, SVt_PV);
245 SAVESPTR(GvSV(agv));
246 SAVESPTR(GvSV(bgv));
09c2a9b8 247 GvSV(agv) = ret;
f4a2945e 248 cv = sv_2cv(block, &stash, &gv, 0);
249 reducecop = CvSTART(cv);
250 SAVESPTR(CvROOT(cv)->op_ppaddr);
251 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
f3548bdc 252#ifdef PAD_SET_CUR
253 PAD_SET_CUR(CvPADLIST(cv),1);
254#else
f4a2945e 255 SAVESPTR(PL_curpad);
256 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
f3548bdc 257#endif
f4a2945e 258 SAVETMPS;
259 SAVESPTR(PL_op);
09c2a9b8 260 SvSetSV(ret, ST(1));
1bfb5477 261 CATCH_SET(TRUE);
60f3865b 262 PUSHBLOCK(cx, CXt_SUB, SP);
263 PUSHSUB(cx);
f4a2945e 264 for(index = 2 ; index < items ; index++) {
f4a2945e 265 GvSV(bgv) = ST(index);
266 PL_op = reducecop;
da53b6b0 267 CALLRUNOPS(aTHX);
09c2a9b8 268 SvSetSV(ret, *PL_stack_sp);
f4a2945e 269 }
09c2a9b8 270 ST(0) = ret;
1bfb5477 271 POPBLOCK(cx,PL_curpm);
49ce4d31 272 LEAVESUB(cv);
1bfb5477 273 CATCH_SET(oldcatch);
f4a2945e 274 XSRETURN(1);
275}
276
277void
278first(block,...)
279 SV * block
280PROTOTYPE: &@
281CODE:
282{
27da23d5 283 dVAR;
f4a2945e 284 int index;
f4a2945e 285 GV *gv;
286 HV *stash;
287 CV *cv;
288 OP *reducecop;
1bfb5477 289 PERL_CONTEXT *cx;
290 SV** newsp;
291 I32 gimme = G_SCALAR;
c5661c80 292 U8 hasargs = 0;
1bfb5477 293 bool oldcatch = CATCH_GET;
294
f4a2945e 295 if(items <= 1) {
296 XSRETURN_UNDEF;
297 }
298 SAVESPTR(GvSV(PL_defgv));
299 cv = sv_2cv(block, &stash, &gv, 0);
300 reducecop = CvSTART(cv);
301 SAVESPTR(CvROOT(cv)->op_ppaddr);
302 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
f3548bdc 303#ifdef PAD_SET_CUR
304 PAD_SET_CUR(CvPADLIST(cv),1);
305#else
f4a2945e 306 SAVESPTR(PL_curpad);
307 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
f3548bdc 308#endif
f4a2945e 309 SAVETMPS;
310 SAVESPTR(PL_op);
1bfb5477 311 CATCH_SET(TRUE);
60f3865b 312 PUSHBLOCK(cx, CXt_SUB, SP);
313 PUSHSUB(cx);
60f3865b 314
f4a2945e 315 for(index = 1 ; index < items ; index++) {
316 GvSV(PL_defgv) = ST(index);
317 PL_op = reducecop;
da53b6b0 318 CALLRUNOPS(aTHX);
f4a2945e 319 if (SvTRUE(*PL_stack_sp)) {
320 ST(0) = ST(index);
1bfb5477 321 POPBLOCK(cx,PL_curpm);
49ce4d31 322 LEAVESUB(cv);
1bfb5477 323 CATCH_SET(oldcatch);
f4a2945e 324 XSRETURN(1);
325 }
326 }
1bfb5477 327 POPBLOCK(cx,PL_curpm);
49ce4d31 328 LEAVESUB(cv);
1bfb5477 329 CATCH_SET(oldcatch);
f4a2945e 330 XSRETURN_UNDEF;
331}
332
1bfb5477 333void
334shuffle(...)
335PROTOTYPE: @
336CODE:
337{
27da23d5 338 dVAR;
1bfb5477 339 int index;
340 struct op dmy_op;
341 struct op *old_op = PL_op;
1bfb5477 342
c29e891d 343 /* We call pp_rand here so that Drand01 get initialized if rand()
344 or srand() has not already been called
345 */
1bfb5477 346 memzero((char*)(&dmy_op), sizeof(struct op));
f3548bdc 347 /* we let pp_rand() borrow the TARG allocated for this XS sub */
348 dmy_op.op_targ = PL_op->op_targ;
1bfb5477 349 PL_op = &dmy_op;
20d72259 350 (void)*(PL_ppaddr[OP_RAND])(aTHX);
1bfb5477 351 PL_op = old_op;
1bfb5477 352 for (index = items ; index > 1 ; ) {
353 int swap = (int)(Drand01() * (double)(index--));
354 SV *tmp = ST(swap);
355 ST(swap) = ST(index);
356 ST(index) = tmp;
357 }
358 XSRETURN(items);
359}
360
361
f4a2945e 362MODULE=List::Util PACKAGE=Scalar::Util
363
364void
365dualvar(num,str)
366 SV * num
367 SV * str
368PROTOTYPE: $$
369CODE:
370{
371 STRLEN len;
372 char *ptr = SvPV(str,len);
373 ST(0) = sv_newmortal();
9c5ffd7c 374 (void)SvUPGRADE(ST(0),SVt_PVNV);
f4a2945e 375 sv_setpvn(ST(0),ptr,len);
1bfb5477 376 if(SvNOK(num) || SvPOK(num) || SvMAGICAL(num)) {
9d6ce603 377 SvNV_set(ST(0), SvNV(num));
f4a2945e 378 SvNOK_on(ST(0));
379 }
1bfb5477 380#ifdef SVf_IVisUV
381 else if (SvUOK(num)) {
607fa7f2 382 SvUV_set(ST(0), SvUV(num));
1bfb5477 383 SvIOK_on(ST(0));
384 SvIsUV_on(ST(0));
385 }
386#endif
f4a2945e 387 else {
45977657 388 SvIV_set(ST(0), SvIV(num));
f4a2945e 389 SvIOK_on(ST(0));
390 }
391 if(PL_tainting && (SvTAINTED(num) || SvTAINTED(str)))
392 SvTAINTED_on(ST(0));
393 XSRETURN(1);
394}
395
396char *
397blessed(sv)
398 SV * sv
399PROTOTYPE: $
400CODE:
401{
402 if (SvMAGICAL(sv))
403 mg_get(sv);
404 if(!sv_isobject(sv)) {
405 XSRETURN_UNDEF;
406 }
407 RETVAL = sv_reftype(SvRV(sv),TRUE);
408}
409OUTPUT:
410 RETVAL
411
412char *
413reftype(sv)
414 SV * sv
415PROTOTYPE: $
416CODE:
417{
418 if (SvMAGICAL(sv))
419 mg_get(sv);
420 if(!SvROK(sv)) {
421 XSRETURN_UNDEF;
422 }
423 RETVAL = sv_reftype(SvRV(sv),FALSE);
424}
425OUTPUT:
426 RETVAL
427
bd1e762a 428UV
60f3865b 429refaddr(sv)
430 SV * sv
431PROTOTYPE: $
432CODE:
433{
4579700c 434 if (SvMAGICAL(sv))
435 mg_get(sv);
60f3865b 436 if(!SvROK(sv)) {
437 XSRETURN_UNDEF;
438 }
bd1e762a 439 RETVAL = PTR2UV(SvRV(sv));
60f3865b 440}
441OUTPUT:
442 RETVAL
443
f4a2945e 444void
445weaken(sv)
446 SV *sv
447PROTOTYPE: $
448CODE:
449#ifdef SvWEAKREF
450 sv_rvweaken(sv);
451#else
452 croak("weak references are not implemented in this release of perl");
453#endif
454
c6c619a9 455void
f4a2945e 456isweak(sv)
457 SV *sv
458PROTOTYPE: $
459CODE:
460#ifdef SvWEAKREF
461 ST(0) = boolSV(SvROK(sv) && SvWEAKREF(sv));
462 XSRETURN(1);
463#else
464 croak("weak references are not implemented in this release of perl");
465#endif
466
467int
468readonly(sv)
469 SV *sv
470PROTOTYPE: $
471CODE:
472 RETVAL = SvREADONLY(sv);
473OUTPUT:
474 RETVAL
475
476int
477tainted(sv)
478 SV *sv
479PROTOTYPE: $
480CODE:
481 RETVAL = SvTAINTED(sv);
482OUTPUT:
483 RETVAL
484
60f3865b 485void
486isvstring(sv)
487 SV *sv
488PROTOTYPE: $
489CODE:
490#ifdef SvVOK
491 ST(0) = boolSV(SvVOK(sv));
492 XSRETURN(1);
493#else
494 croak("vstrings are not implemented in this release of perl");
495#endif
496
9e7deb6c 497int
498looks_like_number(sv)
499 SV *sv
500PROTOTYPE: $
501CODE:
502 RETVAL = looks_like_number(sv);
503OUTPUT:
504 RETVAL
505
c5661c80 506void
97605c51 507set_prototype(subref, proto)
508 SV *subref
509 SV *proto
510PROTOTYPE: &$
511CODE:
512{
513 if (SvROK(subref)) {
514 SV *sv = SvRV(subref);
515 if (SvTYPE(sv) != SVt_PVCV) {
516 /* not a subroutine reference */
517 croak("set_prototype: not a subroutine reference");
518 }
519 if (SvPOK(proto)) {
520 /* set the prototype */
521 STRLEN len;
522 char *ptr = SvPV(proto, len);
523 sv_setpvn(sv, ptr, len);
524 }
525 else {
526 /* delete the prototype */
527 SvPOK_off(sv);
528 }
529 }
530 else {
531 croak("set_prototype: not a reference");
532 }
533 XSRETURN(1);
534}
60f3865b 535
f4a2945e 536BOOT:
537{
60f3865b 538#if !defined(SvWEAKREF) || !defined(SvVOK)
f4a2945e 539 HV *stash = gv_stashpvn("Scalar::Util", 12, TRUE);
540 GV *vargv = *(GV**)hv_fetch(stash, "EXPORT_FAIL", 11, TRUE);
541 AV *varav;
542 if (SvTYPE(vargv) != SVt_PVGV)
543 gv_init(vargv, stash, "Scalar::Util", 12, TRUE);
544 varav = GvAVn(vargv);
60f3865b 545#endif
546#ifndef SvWEAKREF
f4a2945e 547 av_push(varav, newSVpv("weaken",6));
548 av_push(varav, newSVpv("isweak",6));
549#endif
60f3865b 550#ifndef SvVOK
551 av_push(varav, newSVpv("isvstring",9));
552#endif
f4a2945e 553}