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