This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Nit to S_isa_lookup by Brandon Black
[perl5.git] / universal.c
CommitLineData
d6376244
JH
1/* universal.c
2 *
b5f8cc5c 3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
fdf8c088 4 * 2005, 2006, 2007 by Larry Wall and others
d6376244
JH
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
d31a8517
AT
11/*
12 * "The roots of those mountains must be roots indeed; there must be
13 * great secrets buried there which have not been discovered since the
14 * beginning." --Gandalf, relating Gollum's story
15 */
16
166f8a29
DM
17/* This file contains the code that implements the functions in Perl's
18 * UNIVERSAL package, such as UNIVERSAL->can().
19 */
20
6d4a7be2 21#include "EXTERN.h"
864dbfa3 22#define PERL_IN_UNIVERSAL_C
6d4a7be2 23#include "perl.h"
6d4a7be2 24
39f7a870
JH
25#ifdef USE_PERLIO
26#include "perliol.h" /* For the PERLIO_F_XXX */
27#endif
28
6d4a7be2
PP
29/*
30 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
31 * The main guts of traverse_isa was actually copied from gv_fetchmeth
32 */
33
a9ec700e 34STATIC bool
34692224 35S_isa_lookup(pTHX_ HV *stash, const char *name, const HV* const name_stash,
301daebc 36 int len, int level)
6d4a7be2 37{
97aff369 38 dVAR;
e1a479c5
BB
39 AV* stash_linear_isa;
40 SV** svp;
bfcb3514 41 const char *hvname;
e1a479c5
BB
42 I32 items;
43 PERL_UNUSED_ARG(len);
44 PERL_UNUSED_ARG(level);
6d4a7be2 45
301daebc
MS
46 /* A stash/class can go by many names (ie. User == main::User), so
47 we compare the stash itself just in case */
34692224 48 if (name_stash && ((const HV *)stash == name_stash))
a9ec700e 49 return TRUE;
6d4a7be2 50
bfcb3514
NC
51 hvname = HvNAME_get(stash);
52
53 if (strEQ(hvname, name))
a9ec700e 54 return TRUE;
6d4a7be2 55
a1d407e8 56 if (strEQ(name, "UNIVERSAL"))
a9ec700e 57 return TRUE;
a1d407e8 58
e1a479c5
BB
59 stash_linear_isa = mro_get_linear_isa(stash);
60 svp = AvARRAY(stash_linear_isa) + 1;
61 items = AvFILLp(stash_linear_isa);
62 while (items--) {
63 SV* const basename_sv = *svp++;
64 HV* basestash = gv_stashsv(basename_sv, 0);
e62fbf8b 65 if (!basestash || (HvMROMETA(basestash)->fake && !HvFILL(basestash))) {
b0c482e3 66 if (ckWARN(WARN_SYNTAX))
e1a479c5
BB
67 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
68 "Can't locate package %"SVf" for the parents of %s",
69 SVfARG(basename_sv), hvname);
70 continue;
46e4b22b 71 }
e1a479c5
BB
72 if(name_stash == basestash || strEQ(name, SvPVX(basename_sv)))
73 return TRUE;
6d4a7be2
PP
74 }
75
a9ec700e 76 return FALSE;
6d4a7be2
PP
77}
78
954c1994 79/*
ccfc67b7
JH
80=head1 SV Manipulation Functions
81
954c1994
GS
82=for apidoc sv_derived_from
83
6885da0e 84Returns a boolean indicating whether the SV is derived from the specified class
85I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
86normal Perl method.
954c1994
GS
87
88=cut
89*/
90
55497cff 91bool
864dbfa3 92Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 93{
97aff369 94 dVAR;
0b6f4f5c 95 HV *stash;
46e4b22b 96
5b295bef 97 SvGETMAGIC(sv);
55497cff
PP
98
99 if (SvROK(sv)) {
0b6f4f5c 100 const char *type;
55497cff
PP
101 sv = SvRV(sv);
102 type = sv_reftype(sv,0);
0b6f4f5c
AL
103 if (type && strEQ(type,name))
104 return TRUE;
105 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
55497cff
PP
106 }
107 else {
da51bb9b 108 stash = gv_stashsv(sv, 0);
55497cff 109 }
46e4b22b 110
0b6f4f5c 111 if (stash) {
da51bb9b 112 HV * const name_stash = gv_stashpv(name, 0);
a9ec700e 113 return isa_lookup(stash, name, name_stash, strlen(name), 0);
0b6f4f5c
AL
114 }
115 else
116 return FALSE;
301daebc 117
55497cff
PP
118}
119
cbc021f9 120/*
121=for apidoc sv_does
122
123Returns a boolean indicating whether the SV performs a specific, named role.
124The SV can be a Perl object or the name of a Perl class.
125
126=cut
127*/
128
1b026014
NIS
129#include "XSUB.h"
130
cbc021f9 131bool
132Perl_sv_does(pTHX_ SV *sv, const char *name)
133{
134 const char *classname;
135 bool does_it;
59e7186f 136 SV *methodname;
cbc021f9 137
138 dSP;
139 ENTER;
140 SAVETMPS;
141
142 SvGETMAGIC(sv);
143
144 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
145 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
146 return FALSE;
147
148 if (sv_isobject(sv)) {
149 classname = sv_reftype(SvRV(sv),TRUE);
150 } else {
94707740 151 classname = SvPV_nolen(sv);
cbc021f9 152 }
153
154 if (strEQ(name,classname))
155 return TRUE;
156
157 PUSHMARK(SP);
158 XPUSHs(sv);
159 XPUSHs(sv_2mortal(newSVpv(name, 0)));
160 PUTBACK;
161
59e7186f
RGS
162 methodname = sv_2mortal(newSVpv("isa", 0));
163 /* ugly hack: use the SvSCREAM flag so S_method_common
164 * can figure out we're calling DOES() and not isa(),
165 * and report eventual errors correctly. --rgs */
166 SvSCREAM_on(methodname);
167 call_sv(methodname, G_SCALAR | G_METHOD);
cbc021f9 168 SPAGAIN;
169
170 does_it = SvTRUE( TOPs );
171 FREETMPS;
172 LEAVE;
173
174 return does_it;
175}
176
80305961
YO
177regexp *
178Perl_get_re_arg( pTHX_ SV *sv, U32 flags, MAGIC **mgp) {
179 MAGIC *mg;
180 if (sv) {
181 if (SvMAGICAL(sv))
182 mg_get(sv);
183 if (SvROK(sv) &&
184 (sv = (SV*)SvRV(sv)) && /* assign deliberate */
185 SvTYPE(sv) == SVt_PVMG &&
186 (mg = mg_find(sv, PERL_MAGIC_qr))) /* assign deliberate */
187 {
188 if (mgp) *mgp = mg;
189 return (regexp *)mg->mg_obj;
190 }
191 }
192 if (mgp) *mgp = NULL;
193 return ((flags && PL_curpm) ? PM_GETRE(PL_curpm) : NULL);
194}
195
196
27da23d5
JH
197PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
198PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
cbc021f9 199PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
27da23d5 200PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
439cb1c4
JP
201XS(XS_version_new);
202XS(XS_version_stringify);
203XS(XS_version_numify);
9137345a 204XS(XS_version_normal);
439cb1c4
JP
205XS(XS_version_vcmp);
206XS(XS_version_boolean);
2dfd8427
AL
207#ifdef HASATTRIBUTE_NORETURN
208XS(XS_version_noop) __attribute__noreturn__;
209#else
439cb1c4 210XS(XS_version_noop);
2dfd8427 211#endif
c8d69e4a 212XS(XS_version_is_alpha);
137d6fc0 213XS(XS_version_qv);
8800c35a 214XS(XS_utf8_is_utf8);
1b026014
NIS
215XS(XS_utf8_valid);
216XS(XS_utf8_encode);
217XS(XS_utf8_decode);
218XS(XS_utf8_upgrade);
219XS(XS_utf8_downgrade);
220XS(XS_utf8_unicode_to_native);
221XS(XS_utf8_native_to_unicode);
29569577
JH
222XS(XS_Internals_SvREADONLY);
223XS(XS_Internals_SvREFCNT);
f044d0d1 224XS(XS_Internals_hv_clear_placehold);
39f7a870 225XS(XS_PerlIO_get_layers);
39cff0d9 226XS(XS_Regexp_DESTROY);
9a7034eb 227XS(XS_Internals_hash_seed);
008fb0c0 228XS(XS_Internals_rehash_seed);
05619474 229XS(XS_Internals_HvREHASH);
e1234d8e 230XS(XS_Internals_inc_sub_generation);
80305961
YO
231XS(XS_re_is_regexp);
232XS(XS_re_regname);
233XS(XS_re_regnames);
234XS(XS_re_regnames_iterinit);
235XS(XS_re_regnames_iternext);
236XS(XS_re_regnames_count);
0cb96387
GS
237
238void
239Perl_boot_core_UNIVERSAL(pTHX)
240{
97aff369 241 dVAR;
157e3fc8 242 static const char file[] = __FILE__;
0cb96387
GS
243
244 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
245 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
cbc021f9 246 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
0cb96387 247 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
439cb1c4 248 {
ad63d80f
JP
249 /* register the overloading (type 'A') magic */
250 PL_amagic_generation++;
439cb1c4 251 /* Make it findable via fetchmethod */
be2ebcad 252 newXS("version::()", XS_version_noop, file);
439cb1c4
JP
253 newXS("version::new", XS_version_new, file);
254 newXS("version::(\"\"", XS_version_stringify, file);
255 newXS("version::stringify", XS_version_stringify, file);
256 newXS("version::(0+", XS_version_numify, file);
257 newXS("version::numify", XS_version_numify, file);
9137345a 258 newXS("version::normal", XS_version_normal, file);
439cb1c4
JP
259 newXS("version::(cmp", XS_version_vcmp, file);
260 newXS("version::(<=>", XS_version_vcmp, file);
261 newXS("version::vcmp", XS_version_vcmp, file);
262 newXS("version::(bool", XS_version_boolean, file);
263 newXS("version::boolean", XS_version_boolean, file);
264 newXS("version::(nomethod", XS_version_noop, file);
265 newXS("version::noop", XS_version_noop, file);
c8d69e4a 266 newXS("version::is_alpha", XS_version_is_alpha, file);
137d6fc0 267 newXS("version::qv", XS_version_qv, file);
439cb1c4 268 }
8800c35a 269 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
1b026014
NIS
270 newXS("utf8::valid", XS_utf8_valid, file);
271 newXS("utf8::encode", XS_utf8_encode, file);
272 newXS("utf8::decode", XS_utf8_decode, file);
273 newXS("utf8::upgrade", XS_utf8_upgrade, file);
274 newXS("utf8::downgrade", XS_utf8_downgrade, file);
275 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
276 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
29569577
JH
277 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
278 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
dfd4ef2f 279 newXSproto("Internals::hv_clear_placeholders",
f044d0d1 280 XS_Internals_hv_clear_placehold, file, "\\%");
9d569fce
JH
281 newXSproto("PerlIO::get_layers",
282 XS_PerlIO_get_layers, file, "*;@");
39cff0d9 283 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
9a7034eb 284 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
008fb0c0 285 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
05619474 286 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
e1234d8e
NC
287 newXSproto("Internals::inc_sub_generation",XS_Internals_inc_sub_generation,
288 file, "");
80305961 289 newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
28d8d7f4
YO
290 newXSproto("re::regname", XS_re_regname, file, ";$$");
291 newXSproto("re::regnames", XS_re_regnames, file, ";$");
292 newXSproto("re::regnames_iterinit", XS_re_regnames_iterinit, file, "");
293 newXSproto("re::regnames_iternext", XS_re_regnames_iternext, file, ";$");
294 newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
0cb96387
GS
295}
296
55497cff 297
6d4a7be2
PP
298XS(XS_UNIVERSAL_isa)
299{
97aff369 300 dVAR;
6d4a7be2 301 dXSARGS;
58c0efa5 302 PERL_UNUSED_ARG(cv);
6d4a7be2
PP
303
304 if (items != 2)
cea2e8a9 305 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
c4420975
AL
306 else {
307 SV * const sv = ST(0);
308 const char *name;
6d4a7be2 309
c4420975 310 SvGETMAGIC(sv);
d3f7f2b2 311
c4420975
AL
312 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
313 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
314 XSRETURN_UNDEF;
f8f70380 315
c4420975 316 name = SvPV_nolen_const(ST(1));
6d4a7be2 317
c4420975
AL
318 ST(0) = boolSV(sv_derived_from(sv, name));
319 XSRETURN(1);
320 }
6d4a7be2
PP
321}
322
6d4a7be2
PP
323XS(XS_UNIVERSAL_can)
324{
97aff369 325 dVAR;
6d4a7be2
PP
326 dXSARGS;
327 SV *sv;
6867be6d 328 const char *name;
6d4a7be2 329 SV *rv;
6f08146e 330 HV *pkg = NULL;
58c0efa5 331 PERL_UNUSED_ARG(cv);
6d4a7be2
PP
332
333 if (items != 2)
cea2e8a9 334 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
335
336 sv = ST(0);
f8f70380 337
5b295bef 338 SvGETMAGIC(sv);
d3f7f2b2 339
253ecd6d
RGS
340 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
341 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
f8f70380
GS
342 XSRETURN_UNDEF;
343
0510663f 344 name = SvPV_nolen_const(ST(1));
3280af22 345 rv = &PL_sv_undef;
6d4a7be2 346
46e4b22b 347 if (SvROK(sv)) {
6f08146e 348 sv = (SV*)SvRV(sv);
46e4b22b 349 if (SvOBJECT(sv))
6f08146e
NIS
350 pkg = SvSTASH(sv);
351 }
352 else {
da51bb9b 353 pkg = gv_stashsv(sv, 0);
6f08146e
NIS
354 }
355
356 if (pkg) {
c4420975 357 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
dc848c6f
PP
358 if (gv && isGV(gv))
359 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
360 }
361
362 ST(0) = rv;
363 XSRETURN(1);
364}
365
cbc021f9 366XS(XS_UNIVERSAL_DOES)
367{
368 dVAR;
369 dXSARGS;
58c0efa5 370 PERL_UNUSED_ARG(cv);
cbc021f9 371
372 if (items != 2)
26be3db7 373 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
cbc021f9 374 else {
375 SV * const sv = ST(0);
376 const char *name;
377
378 name = SvPV_nolen_const(ST(1));
379 if (sv_does( sv, name ))
380 XSRETURN_YES;
381
382 XSRETURN_NO;
383 }
384}
385
6d4a7be2
PP
386XS(XS_UNIVERSAL_VERSION)
387{
97aff369 388 dVAR;
6d4a7be2
PP
389 dXSARGS;
390 HV *pkg;
391 GV **gvp;
392 GV *gv;
393 SV *sv;
e1ec3a88 394 const char *undef;
58c0efa5 395 PERL_UNUSED_ARG(cv);
6d4a7be2 396
1571675a 397 if (SvROK(ST(0))) {
6d4a7be2 398 sv = (SV*)SvRV(ST(0));
1571675a 399 if (!SvOBJECT(sv))
cea2e8a9 400 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2
PP
401 pkg = SvSTASH(sv);
402 }
403 else {
da51bb9b 404 pkg = gv_stashsv(ST(0), 0);
6d4a7be2
PP
405 }
406
4608196e 407 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
6d4a7be2 408
0008872a 409 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
c4420975 410 SV * const nsv = sv_newmortal();
6d4a7be2
PP
411 sv_setsv(nsv, sv);
412 sv = nsv;
137d6fc0 413 if ( !sv_derived_from(sv, "version"))
ac0e6a2f 414 upg_version(sv, FALSE);
c445ea15 415 undef = NULL;
6d4a7be2
PP
416 }
417 else {
3280af22 418 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
419 undef = "(undef)";
420 }
421
1571675a 422 if (items > 1) {
1571675a
GS
423 SV *req = ST(1);
424
62658f4d 425 if (undef) {
bfcb3514 426 if (pkg) {
c4420975 427 const char * const name = HvNAME_get(pkg);
a3b680e6 428 Perl_croak(aTHX_
bfcb3514
NC
429 "%s does not define $%s::VERSION--version check failed",
430 name, name);
431 } else {
a3b680e6
AL
432 Perl_croak(aTHX_
433 "%s defines neither package nor VERSION--version check failed",
0510663f 434 SvPVx_nolen_const(ST(0)) );
62658f4d
PM
435 }
436 }
ad63d80f 437
137d6fc0
JP
438 if ( !sv_derived_from(req, "version")) {
439 /* req may very well be R/O, so create a new object */
ac0e6a2f 440 req = sv_2mortal( new_version(req) );
137d6fc0 441 }
1571675a 442
ac0e6a2f
RGS
443 if ( vcmp( req, sv ) > 0 ) {
444 if ( hv_exists((HV*)SvRV(req), "qv", 2 ) ) {
445 Perl_croak(aTHX_ "%s version %"SVf" required--"
446 "this is only version %"SVf"", HvNAME_get(pkg),
be2597df 447 SVfARG(vnormal(req)),
be2597df 448 SVfARG(vnormal(sv)));
ac0e6a2f
RGS
449 } else {
450 Perl_croak(aTHX_ "%s version %"SVf" required--"
451 "this is only version %"SVf"", HvNAME_get(pkg),
452 SVfARG(vnumify(req)),
453 SVfARG(vnumify(sv)));
454 }
455 }
456
2d8e6c8d 457 }
6d4a7be2 458
2b140d5b 459 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
b38a9dc5 460 ST(0) = vnumify(sv);
13f8f398
JP
461 } else {
462 ST(0) = sv;
b38a9dc5 463 }
6d4a7be2
PP
464
465 XSRETURN(1);
466}
467
439cb1c4
JP
468XS(XS_version_new)
469{
97aff369 470 dVAR;
439cb1c4 471 dXSARGS;
58c0efa5 472 PERL_UNUSED_ARG(cv);
129318bd 473 if (items > 3)
439cb1c4
JP
474 Perl_croak(aTHX_ "Usage: version::new(class, version)");
475 SP -= items;
476 {
137d6fc0
JP
477 SV *vs = ST(1);
478 SV *rv;
c4420975
AL
479 const char * const classname =
480 sv_isobject(ST(0)) /* get the class if called as an object method */
481 ? HvNAME(SvSTASH(SvRV(ST(0))))
482 : (char *)SvPV_nolen(ST(0));
9137345a 483
92dcf8ce
JP
484 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
485 /* create empty object */
486 vs = sv_newmortal();
487 sv_setpvn(vs,"",0);
9137345a
JP
488 }
489 else if ( items == 3 ) {
490 vs = sv_newmortal();
cfd0369c 491 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
129318bd 492 }
439cb1c4 493
137d6fc0 494 rv = new_version(vs);
0723351e 495 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
da51bb9b 496 sv_bless(rv, gv_stashpv(classname, GV_ADD));
137d6fc0
JP
497
498 PUSHs(sv_2mortal(rv));
439cb1c4
JP
499 PUTBACK;
500 return;
501 }
502}
503
504XS(XS_version_stringify)
505{
97aff369 506 dVAR;
41be1fbd 507 dXSARGS;
58c0efa5 508 PERL_UNUSED_ARG(cv);
41be1fbd
JH
509 if (items < 1)
510 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
511 SP -= items;
512 {
7452cf6a 513 SV * lobj;
41be1fbd
JH
514
515 if (sv_derived_from(ST(0), "version")) {
9137345a 516 lobj = SvRV(ST(0));
41be1fbd
JH
517 }
518 else
519 Perl_croak(aTHX_ "lobj is not of type version");
520
137d6fc0 521 PUSHs(sv_2mortal(vstringify(lobj)));
41be1fbd
JH
522
523 PUTBACK;
524 return;
525 }
439cb1c4
JP
526}
527
528XS(XS_version_numify)
529{
97aff369 530 dVAR;
41be1fbd 531 dXSARGS;
58c0efa5 532 PERL_UNUSED_ARG(cv);
41be1fbd
JH
533 if (items < 1)
534 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
535 SP -= items;
536 {
7452cf6a 537 SV * lobj;
41be1fbd
JH
538
539 if (sv_derived_from(ST(0), "version")) {
9137345a 540 lobj = SvRV(ST(0));
41be1fbd
JH
541 }
542 else
543 Perl_croak(aTHX_ "lobj is not of type version");
544
137d6fc0 545 PUSHs(sv_2mortal(vnumify(lobj)));
41be1fbd
JH
546
547 PUTBACK;
548 return;
549 }
439cb1c4
JP
550}
551
9137345a
JP
552XS(XS_version_normal)
553{
97aff369 554 dVAR;
9137345a 555 dXSARGS;
58c0efa5 556 PERL_UNUSED_ARG(cv);
9137345a
JP
557 if (items < 1)
558 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
559 SP -= items;
560 {
7452cf6a 561 SV * lobj;
9137345a
JP
562
563 if (sv_derived_from(ST(0), "version")) {
564 lobj = SvRV(ST(0));
565 }
566 else
567 Perl_croak(aTHX_ "lobj is not of type version");
568
569 PUSHs(sv_2mortal(vnormal(lobj)));
570
571 PUTBACK;
572 return;
573 }
574}
575
439cb1c4
JP
576XS(XS_version_vcmp)
577{
97aff369 578 dVAR;
41be1fbd 579 dXSARGS;
58c0efa5 580 PERL_UNUSED_ARG(cv);
41be1fbd
JH
581 if (items < 1)
582 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
583 SP -= items;
584 {
7452cf6a 585 SV * lobj;
41be1fbd
JH
586
587 if (sv_derived_from(ST(0), "version")) {
9137345a 588 lobj = SvRV(ST(0));
41be1fbd
JH
589 }
590 else
591 Perl_croak(aTHX_ "lobj is not of type version");
592
593 {
594 SV *rs;
595 SV *rvs;
596 SV * robj = ST(1);
7452cf6a 597 const IV swap = (IV)SvIV(ST(2));
41be1fbd
JH
598
599 if ( ! sv_derived_from(robj, "version") )
600 {
601 robj = new_version(robj);
602 }
603 rvs = SvRV(robj);
604
605 if ( swap )
606 {
607 rs = newSViv(vcmp(rvs,lobj));
608 }
609 else
610 {
611 rs = newSViv(vcmp(lobj,rvs));
612 }
613
137d6fc0 614 PUSHs(sv_2mortal(rs));
41be1fbd
JH
615 }
616
617 PUTBACK;
618 return;
619 }
439cb1c4
JP
620}
621
622XS(XS_version_boolean)
623{
97aff369
JH
624 dVAR;
625 dXSARGS;
58c0efa5 626 PERL_UNUSED_ARG(cv);
97aff369
JH
627 if (items < 1)
628 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
629 SP -= items;
c4420975
AL
630 if (sv_derived_from(ST(0), "version")) {
631 SV * const lobj = SvRV(ST(0));
396482e1 632 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
c4420975
AL
633 PUSHs(sv_2mortal(rs));
634 PUTBACK;
635 return;
636 }
637 else
638 Perl_croak(aTHX_ "lobj is not of type version");
439cb1c4
JP
639}
640
641XS(XS_version_noop)
642{
97aff369 643 dVAR;
2dfd8427 644 dXSARGS;
58c0efa5 645 PERL_UNUSED_ARG(cv);
2dfd8427
AL
646 if (items < 1)
647 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
648 if (sv_derived_from(ST(0), "version"))
649 Perl_croak(aTHX_ "operation not supported with version object");
650 else
651 Perl_croak(aTHX_ "lobj is not of type version");
652#ifndef HASATTRIBUTE_NORETURN
653 XSRETURN_EMPTY;
654#endif
439cb1c4
JP
655}
656
c8d69e4a
JP
657XS(XS_version_is_alpha)
658{
97aff369 659 dVAR;
c8d69e4a 660 dXSARGS;
58c0efa5 661 PERL_UNUSED_ARG(cv);
c8d69e4a
JP
662 if (items != 1)
663 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
664 SP -= items;
c4420975
AL
665 if (sv_derived_from(ST(0), "version")) {
666 SV * const lobj = ST(0);
667 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
668 XSRETURN_YES;
669 else
670 XSRETURN_NO;
c8d69e4a
JP
671 PUTBACK;
672 return;
673 }
c4420975
AL
674 else
675 Perl_croak(aTHX_ "lobj is not of type version");
c8d69e4a
JP
676}
677
137d6fc0
JP
678XS(XS_version_qv)
679{
97aff369 680 dVAR;
137d6fc0 681 dXSARGS;
58c0efa5 682 PERL_UNUSED_ARG(cv);
137d6fc0
JP
683 if (items != 1)
684 Perl_croak(aTHX_ "Usage: version::qv(ver)");
685 SP -= items;
686 {
687 SV * ver = ST(0);
c4420975 688 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
ac0e6a2f
RGS
689 SV * const rv = sv_newmortal();
690 sv_setsv(rv,ver); /* make a duplicate */
691 upg_version(rv, TRUE);
692 PUSHs(rv);
137d6fc0
JP
693 }
694 else
695 {
696 PUSHs(sv_2mortal(new_version(ver)));
697 }
698
699 PUTBACK;
700 return;
701 }
702}
703
8800c35a
JH
704XS(XS_utf8_is_utf8)
705{
97aff369 706 dVAR;
41be1fbd 707 dXSARGS;
58c0efa5 708 PERL_UNUSED_ARG(cv);
41be1fbd
JH
709 if (items != 1)
710 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
c4420975
AL
711 else {
712 const SV * const sv = ST(0);
713 if (SvUTF8(sv))
714 XSRETURN_YES;
715 else
716 XSRETURN_NO;
41be1fbd
JH
717 }
718 XSRETURN_EMPTY;
8800c35a
JH
719}
720
1b026014
NIS
721XS(XS_utf8_valid)
722{
97aff369 723 dVAR;
41be1fbd 724 dXSARGS;
58c0efa5 725 PERL_UNUSED_ARG(cv);
41be1fbd
JH
726 if (items != 1)
727 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
c4420975
AL
728 else {
729 SV * const sv = ST(0);
730 STRLEN len;
731 const char * const s = SvPV_const(sv,len);
732 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
733 XSRETURN_YES;
734 else
735 XSRETURN_NO;
736 }
41be1fbd 737 XSRETURN_EMPTY;
1b026014
NIS
738}
739
740XS(XS_utf8_encode)
741{
97aff369 742 dVAR;
1b026014 743 dXSARGS;
58c0efa5 744 PERL_UNUSED_ARG(cv);
1b026014
NIS
745 if (items != 1)
746 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
c4420975 747 sv_utf8_encode(ST(0));
1b026014
NIS
748 XSRETURN_EMPTY;
749}
750
751XS(XS_utf8_decode)
752{
97aff369 753 dVAR;
1b026014 754 dXSARGS;
58c0efa5 755 PERL_UNUSED_ARG(cv);
1b026014
NIS
756 if (items != 1)
757 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
c4420975
AL
758 else {
759 SV * const sv = ST(0);
6867be6d 760 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
761 ST(0) = boolSV(RETVAL);
762 sv_2mortal(ST(0));
763 }
764 XSRETURN(1);
765}
766
767XS(XS_utf8_upgrade)
768{
97aff369 769 dVAR;
1b026014 770 dXSARGS;
58c0efa5 771 PERL_UNUSED_ARG(cv);
1b026014
NIS
772 if (items != 1)
773 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
c4420975
AL
774 else {
775 SV * const sv = ST(0);
1b026014
NIS
776 STRLEN RETVAL;
777 dXSTARG;
778
779 RETVAL = sv_utf8_upgrade(sv);
780 XSprePUSH; PUSHi((IV)RETVAL);
781 }
782 XSRETURN(1);
783}
784
785XS(XS_utf8_downgrade)
786{
97aff369 787 dVAR;
1b026014 788 dXSARGS;
58c0efa5 789 PERL_UNUSED_ARG(cv);
1b026014
NIS
790 if (items < 1 || items > 2)
791 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
c4420975
AL
792 else {
793 SV * const sv = ST(0);
6867be6d
AL
794 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
795 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 796
1b026014
NIS
797 ST(0) = boolSV(RETVAL);
798 sv_2mortal(ST(0));
799 }
800 XSRETURN(1);
801}
802
803XS(XS_utf8_native_to_unicode)
804{
97aff369 805 dVAR;
1b026014 806 dXSARGS;
6867be6d 807 const UV uv = SvUV(ST(0));
58c0efa5 808 PERL_UNUSED_ARG(cv);
b7953727
JH
809
810 if (items > 1)
811 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
812
1b026014
NIS
813 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
814 XSRETURN(1);
815}
816
817XS(XS_utf8_unicode_to_native)
818{
97aff369 819 dVAR;
1b026014 820 dXSARGS;
6867be6d 821 const UV uv = SvUV(ST(0));
58c0efa5 822 PERL_UNUSED_ARG(cv);
b7953727
JH
823
824 if (items > 1)
825 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
826
1b026014
NIS
827 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
828 XSRETURN(1);
829}
830
14a976d6 831XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577 832{
97aff369 833 dVAR;
29569577 834 dXSARGS;
c4420975 835 SV * const sv = SvRV(ST(0));
58c0efa5 836 PERL_UNUSED_ARG(cv);
6867be6d 837
29569577
JH
838 if (items == 1) {
839 if (SvREADONLY(sv))
840 XSRETURN_YES;
841 else
842 XSRETURN_NO;
843 }
844 else if (items == 2) {
845 if (SvTRUE(ST(1))) {
846 SvREADONLY_on(sv);
847 XSRETURN_YES;
848 }
849 else {
14a976d6 850 /* I hope you really know what you are doing. */
29569577
JH
851 SvREADONLY_off(sv);
852 XSRETURN_NO;
853 }
854 }
14a976d6 855 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
856}
857
14a976d6 858XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577 859{
97aff369 860 dVAR;
29569577 861 dXSARGS;
c4420975 862 SV * const sv = SvRV(ST(0));
58c0efa5 863 PERL_UNUSED_ARG(cv);
6867be6d 864
29569577 865 if (items == 1)
14a976d6 866 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 867 else if (items == 2) {
14a976d6 868 /* I hope you really know what you are doing. */
29569577
JH
869 SvREFCNT(sv) = SvIV(ST(1));
870 XSRETURN_IV(SvREFCNT(sv));
871 }
14a976d6 872 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
873}
874
f044d0d1 875XS(XS_Internals_hv_clear_placehold)
dfd4ef2f 876{
97aff369 877 dVAR;
dfd4ef2f 878 dXSARGS;
58c0efa5 879 PERL_UNUSED_ARG(cv);
6867be6d 880
3540d4ce
AB
881 if (items != 1)
882 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
c4420975
AL
883 else {
884 HV * const hv = (HV *) SvRV(ST(0));
885 hv_clear_placeholders(hv);
886 XSRETURN(0);
887 }
dfd4ef2f 888}
39f7a870 889
39cff0d9
AE
890XS(XS_Regexp_DESTROY)
891{
96a5add6 892 PERL_UNUSED_CONTEXT;
53c1dcc0 893 PERL_UNUSED_ARG(cv);
39cff0d9
AE
894}
895
39f7a870
JH
896XS(XS_PerlIO_get_layers)
897{
97aff369 898 dVAR;
39f7a870 899 dXSARGS;
58c0efa5 900 PERL_UNUSED_ARG(cv);
39f7a870
JH
901 if (items < 1 || items % 2 == 0)
902 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 903#ifdef USE_PERLIO
39f7a870
JH
904 {
905 SV * sv;
906 GV * gv;
907 IO * io;
908 bool input = TRUE;
909 bool details = FALSE;
910
911 if (items > 1) {
c4420975 912 SV * const *svp;
39f7a870 913 for (svp = MARK + 2; svp <= SP; svp += 2) {
c4420975
AL
914 SV * const * const varp = svp;
915 SV * const * const valp = svp + 1;
39f7a870 916 STRLEN klen;
c4420975 917 const char * const key = SvPV_const(*varp, klen);
39f7a870
JH
918
919 switch (*key) {
920 case 'i':
921 if (klen == 5 && memEQ(key, "input", 5)) {
922 input = SvTRUE(*valp);
923 break;
924 }
925 goto fail;
926 case 'o':
927 if (klen == 6 && memEQ(key, "output", 6)) {
928 input = !SvTRUE(*valp);
929 break;
930 }
931 goto fail;
932 case 'd':
933 if (klen == 7 && memEQ(key, "details", 7)) {
934 details = SvTRUE(*valp);
935 break;
936 }
937 goto fail;
938 default:
939 fail:
940 Perl_croak(aTHX_
941 "get_layers: unknown argument '%s'",
942 key);
943 }
944 }
945
946 SP -= (items - 1);
947 }
948
949 sv = POPs;
950 gv = (GV*)sv;
951
952 if (!isGV(sv)) {
953 if (SvROK(sv) && isGV(SvRV(sv)))
954 gv = (GV*)SvRV(sv);
671d49be 955 else if (SvPOKp(sv))
f776e3cd 956 gv = gv_fetchsv(sv, 0, SVt_PVIO);
39f7a870
JH
957 }
958
959 if (gv && (io = GvIO(gv))) {
960 dTARGET;
c4420975 961 AV* const av = PerlIO_get_layers(aTHX_ input ?
39f7a870
JH
962 IoIFP(io) : IoOFP(io));
963 I32 i;
c4420975 964 const I32 last = av_len(av);
39f7a870
JH
965 I32 nitem = 0;
966
967 for (i = last; i >= 0; i -= 3) {
c4420975
AL
968 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
969 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
970 SV * const * const flgsvp = av_fetch(av, i, FALSE);
39f7a870 971
c4420975
AL
972 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
973 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
974 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
39f7a870
JH
975
976 if (details) {
ec3bab8e
NC
977 XPUSHs(namok
978 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
979 : &PL_sv_undef);
980 XPUSHs(argok
981 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
982 : &PL_sv_undef);
39f7a870
JH
983 if (flgok)
984 XPUSHi(SvIVX(*flgsvp));
985 else
986 XPUSHs(&PL_sv_undef);
987 nitem += 3;
988 }
989 else {
990 if (namok && argok)
991 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
be2597df
MHM
992 SVfARG(*namsvp),
993 SVfARG(*argsvp)));
39f7a870 994 else if (namok)
95b63a38 995 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
be2597df 996 SVfARG(*namsvp)));
39f7a870
JH
997 else
998 XPUSHs(&PL_sv_undef);
999 nitem++;
1000 if (flgok) {
c4420975 1001 const IV flags = SvIVX(*flgsvp);
39f7a870
JH
1002
1003 if (flags & PERLIO_F_UTF8) {
396482e1 1004 XPUSHs(newSVpvs("utf8"));
39f7a870
JH
1005 nitem++;
1006 }
1007 }
1008 }
1009 }
1010
1011 SvREFCNT_dec(av);
1012
1013 XSRETURN(nitem);
1014 }
1015 }
5fef3b4a 1016#endif
39f7a870
JH
1017
1018 XSRETURN(0);
1019}
1020
9a7034eb 1021XS(XS_Internals_hash_seed)
c910b28a 1022{
97aff369 1023 dVAR;
c85d3f85
NC
1024 /* Using dXSARGS would also have dITEM and dSP,
1025 * which define 2 unused local variables. */
557b887a 1026 dAXMARK;
53c1dcc0 1027 PERL_UNUSED_ARG(cv);
ad73156c 1028 PERL_UNUSED_VAR(mark);
81eaca17 1029 XSRETURN_UV(PERL_HASH_SEED);
c910b28a
JH
1030}
1031
008fb0c0 1032XS(XS_Internals_rehash_seed)
8e90d776 1033{
97aff369 1034 dVAR;
8e90d776
NC
1035 /* Using dXSARGS would also have dITEM and dSP,
1036 * which define 2 unused local variables. */
557b887a 1037 dAXMARK;
53c1dcc0 1038 PERL_UNUSED_ARG(cv);
ad73156c 1039 PERL_UNUSED_VAR(mark);
008fb0c0 1040 XSRETURN_UV(PL_rehash_seed);
8e90d776
NC
1041}
1042
05619474
NC
1043XS(XS_Internals_HvREHASH) /* Subject to change */
1044{
97aff369 1045 dVAR;
05619474 1046 dXSARGS;
58c0efa5 1047 PERL_UNUSED_ARG(cv);
05619474 1048 if (SvROK(ST(0))) {
c4420975 1049 const HV * const hv = (HV *) SvRV(ST(0));
05619474
NC
1050 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1051 if (HvREHASH(hv))
1052 XSRETURN_YES;
1053 else
1054 XSRETURN_NO;
1055 }
1056 }
1057 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1058}
241d1a3b 1059
e1234d8e
NC
1060XS(XS_Internals_inc_sub_generation)
1061{
97aff369 1062 dVAR;
e1234d8e
NC
1063 /* Using dXSARGS would also have dITEM and dSP,
1064 * which define 2 unused local variables. */
1065 dAXMARK;
1066 PERL_UNUSED_ARG(cv);
1067 PERL_UNUSED_VAR(mark);
1068 ++PL_sub_generation;
1069 XSRETURN_EMPTY;
1070}
1071
80305961
YO
1072XS(XS_re_is_regexp)
1073{
1074 dVAR;
1075 dXSARGS;
1076 if (items != 1)
1077 Perl_croak(aTHX_ "Usage: %s(%s)", "re::is_regexp", "sv");
1078 PERL_UNUSED_VAR(cv); /* -W */
1079 PERL_UNUSED_VAR(ax); /* -Wall */
1080 SP -= items;
1081 {
1082 SV * sv = ST(0);
1083 if ( Perl_get_re_arg( aTHX_ sv, 0, NULL ) )
1084 {
1085 XSRETURN_YES;
1086 } else {
1087 XSRETURN_NO;
1088 }
1089 /* NOTREACHED */
1090 PUTBACK;
1091 return;
1092 }
1093}
1094
1095XS(XS_re_regname)
1096{
1097
1098 dVAR;
1099 dXSARGS;
28d8d7f4
YO
1100 if (items < 1 || items > 2)
1101 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]");
80305961
YO
1102 PERL_UNUSED_VAR(cv); /* -W */
1103 PERL_UNUSED_VAR(ax); /* -Wall */
1104 SP -= items;
1105 {
1106 SV * sv = ST(0);
80305961 1107 SV * all;
28d8d7f4 1108 regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
80305961
YO
1109 SV *bufs = NULL;
1110
1111 if (items < 2)
80305961
YO
1112 all = NULL;
1113 else {
28d8d7f4 1114 all = ST(1);
80305961
YO
1115 }
1116 {
80305961
YO
1117 if (SvPOK(sv) && re && re->paren_names) {
1118 bufs = CALLREG_NAMEDBUF(re,sv,all && SvTRUE(all));
1119 if (bufs) {
1120 if (all && SvTRUE(all))
1121 XPUSHs(newRV(bufs));
1122 else
1123 XPUSHs(SvREFCNT_inc(bufs));
1124 XSRETURN(1);
1125 }
1126 }
1127 XSRETURN_UNDEF;
1128 }
1129 PUTBACK;
1130 return;
1131 }
1132}
1133
1134XS(XS_re_regnames)
1135{
1136 dVAR;
1137 dXSARGS;
28d8d7f4
YO
1138 if (items < 0 || items > 1)
1139 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]");
80305961
YO
1140 PERL_UNUSED_VAR(cv); /* -W */
1141 PERL_UNUSED_VAR(ax); /* -Wall */
1142 SP -= items;
1143 {
80305961 1144 SV * all;
28d8d7f4 1145 regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
80305961
YO
1146 IV count = 0;
1147
1148 if (items < 1)
80305961
YO
1149 all = NULL;
1150 else {
28d8d7f4 1151 all = ST(0);
80305961
YO
1152 }
1153 {
80305961
YO
1154 if (re && re->paren_names) {
1155 HV *hv= re->paren_names;
1156 (void)hv_iterinit(hv);
1157 while (1) {
1158 HE *temphe = hv_iternext_flags(hv,0);
1159 if (temphe) {
1160 IV i;
1161 IV parno = 0;
1162 SV* sv_dat = HeVAL(temphe);
1163 I32 *nums = (I32*)SvPVX(sv_dat);
1164 for ( i = 0; i < SvIVX(sv_dat); i++ ) {
1165 if ((I32)(re->lastcloseparen) >= nums[i] &&
f0ab9afb
NC
1166 re->offs[nums[i]].start != -1 &&
1167 re->offs[nums[i]].end != -1)
80305961
YO
1168 {
1169 parno = nums[i];
1170 break;
1171 }
1172 }
1173 if (parno || (all && SvTRUE(all))) {
1174 STRLEN len;
1175 char *pv = HePV(temphe, len);
1176 if ( GIMME_V == G_ARRAY )
1177 XPUSHs(newSVpvn(pv,len));
1178 count++;
1179 }
1180 } else {
1181 break;
1182 }
1183 }
1184 }
1185 if ( GIMME_V == G_ARRAY )
1186 XSRETURN(count);
1187 else
1188 XSRETURN_UNDEF;
1189 }
1190 PUTBACK;
1191 return;
1192 }
1193}
1194
1195
1196XS(XS_re_regnames_iterinit)
1197{
1198 dVAR;
1199 dXSARGS;
a2e78f72
RGS
1200 if (items != 0)
1201 Perl_croak(aTHX_ "Usage: re::regnames_iterinit()");
80305961
YO
1202 PERL_UNUSED_VAR(cv); /* -W */
1203 PERL_UNUSED_VAR(ax); /* -Wall */
1204 SP -= items;
1205 {
28d8d7f4
YO
1206 regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1207 if (re && re->paren_names) {
1208 (void)hv_iterinit(re->paren_names);
1209 XPUSHs(newSViv(HvTOTALKEYS(re->paren_names)));
1210 } else {
1211 XSRETURN_UNDEF;
1212 }
80305961
YO
1213 PUTBACK;
1214 return;
1215 }
1216}
1217
1218
1219XS(XS_re_regnames_iternext)
1220{
1221 dVAR;
1222 dXSARGS;
28d8d7f4
YO
1223 if (items < 0 || items > 1)
1224 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_iternext", "[all]");
80305961
YO
1225 PERL_UNUSED_VAR(cv); /* -W */
1226 PERL_UNUSED_VAR(ax); /* -Wall */
1227 SP -= items;
1228 {
80305961 1229 SV * all;
28d8d7f4 1230 regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
80305961
YO
1231
1232 if (items < 1)
80305961
YO
1233 all = NULL;
1234 else {
28d8d7f4 1235 all = ST(0);
80305961 1236 }
28d8d7f4
YO
1237 if (re && re->paren_names) {
1238 HV *hv= re->paren_names;
1239 while (1) {
1240 HE *temphe = hv_iternext_flags(hv,0);
1241 if (temphe) {
1242 IV i;
1243 IV parno = 0;
1244 SV* sv_dat = HeVAL(temphe);
1245 I32 *nums = (I32*)SvPVX(sv_dat);
1246 for ( i = 0; i < SvIVX(sv_dat); i++ ) {
1247 if ((I32)(re->lastcloseparen) >= nums[i] &&
f0ab9afb
NC
1248 re->offs[nums[i]].start != -1 &&
1249 re->offs[nums[i]].end != -1)
28d8d7f4
YO
1250 {
1251 parno = nums[i];
1252 break;
80305961 1253 }
80305961 1254 }
28d8d7f4
YO
1255 if (parno || (all && SvTRUE(all))) {
1256 STRLEN len;
1257 char *pv = HePV(temphe, len);
1258 XPUSHs(newSVpvn(pv,len));
1259 XSRETURN(1);
1260 }
1261 } else {
1262 break;
80305961
YO
1263 }
1264 }
28d8d7f4
YO
1265 }
1266 XSRETURN_UNDEF;
80305961
YO
1267 PUTBACK;
1268 return;
1269 }
1270}
1271
1272
1273XS(XS_re_regnames_count)
1274{
28d8d7f4 1275 regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
80305961
YO
1276 dVAR;
1277 dXSARGS;
1278
28d8d7f4
YO
1279 if (items != 0)
1280 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", "");
80305961
YO
1281 PERL_UNUSED_VAR(cv); /* -W */
1282 PERL_UNUSED_VAR(ax); /* -Wall */
1283 SP -= items;
28d8d7f4 1284
80305961
YO
1285 if (re && re->paren_names) {
1286 XPUSHs(newSViv(HvTOTALKEYS(re->paren_names)));
1287 } else {
1288 XSRETURN_UNDEF;
1289 }
1290 PUTBACK;
1291 return;
1292}
1293
1294
241d1a3b
NC
1295/*
1296 * Local variables:
1297 * c-indentation-style: bsd
1298 * c-basic-offset: 4
1299 * indent-tabs-mode: t
1300 * End:
1301 *
37442d52
RGS
1302 * ex: set ts=8 sts=4 sw=4 noet:
1303 */