Rename ext/Devel/PPPort to ext/Devel-PPPort
[p5sagit/p5-mst-13.2.git] / ext / Devel-PPPort / parts / inc / SvPV
CommitLineData
adfe19db 1################################################################################
2##
51d6c659 3## $Revision: 22 $
adfe19db 4## $Author: mhx $
51d6c659 5## $Date: 2009/01/18 14:10:54 +0100 $
adfe19db 6##
7################################################################################
8##
51d6c659 9## Version 3.x, Copyright (C) 2004-2009, Marcus Holland-Moritz.
adfe19db 10## Version 2.x, Copyright (C) 2001, Paul Marquess.
11## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
12##
13## This program is free software; you can redistribute it and/or
14## modify it under the same terms as Perl itself.
15##
16################################################################################
17
18=provides
19
679ad62d 20__UNDEFINED__
adfe19db 21SvPVbyte
22sv_2pvbyte
679ad62d 23sv_2pv_flags
24sv_pvn_force_flags
adfe19db 25
679ad62d 26=dontwarn
adfe19db 27
679ad62d 28NEED_sv_2pv_flags
af36fda7 29NEED_sv_2pv_flags_GLOBAL
8d2082b0 30DPPP_SVPV_NOLEN_LP_ARG
adfe19db 31
679ad62d 32=implementation
adfe19db 33
679ad62d 34/* Backwards compatibility stuff... :-( */
35#if !defined(NEED_sv_2pv_flags) && defined(NEED_sv_2pv_nolen)
36# define NEED_sv_2pv_flags
37#endif
38#if !defined(NEED_sv_2pv_flags_GLOBAL) && defined(NEED_sv_2pv_nolen_GLOBAL)
39# define NEED_sv_2pv_flags_GLOBAL
adfe19db 40#endif
41
42/* Hint: sv_2pv_nolen
679ad62d 43 * Use the SvPV_nolen() or SvPV_nolen_const() macros instead of sv_2pv_nolen().
adfe19db 44 */
45
679ad62d 46__UNDEFINED__ sv_2pv_nolen(sv) SvPV_nolen(sv)
adfe19db 47
48#ifdef SvPVbyte
49
50/* Hint: SvPVbyte
51 * Does not work in perl-5.6.1, ppport.h implements a version
52 * borrowed from perl-5.7.3.
53 */
54
55#if { VERSION < 5.7.0 }
56
57#if { NEED sv_2pvbyte }
58
59char *
aab9a3b6 60sv_2pvbyte(pTHX_ SV *sv, STRLEN *lp)
4a582685 61{
adfe19db 62 sv_utf8_downgrade(sv,0);
63 return SvPV(sv,*lp);
64}
65
66#endif
67
68/* Hint: sv_2pvbyte
69 * Use the SvPVbyte() macro instead of sv_2pvbyte().
70 */
71
72#undef SvPVbyte
73
adfe19db 74#define SvPVbyte(sv, lp) \
75 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
76 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
77
78#endif
79
80#else
81
82# define SvPVbyte SvPV
83# define sv_2pvbyte sv_2pv
84
85#endif
86
679ad62d 87__UNDEFINED__ sv_2pvbyte_nolen(sv) sv_2pv_nolen(sv)
adfe19db 88
89/* Hint: sv_pvn
90 * Always use the SvPV() macro instead of sv_pvn().
91 */
adfe19db 92
04fc8b94 93/* Hint: sv_pvn_force
adfe19db 94 * Always use the SvPV_force() macro instead of sv_pvn_force().
95 */
adfe19db 96
679ad62d 97/* If these are undefined, they're not handled by the core anyway */
98__UNDEFINED__ SV_IMMEDIATE_UNREF 0
99__UNDEFINED__ SV_GMAGIC 0
100__UNDEFINED__ SV_COW_DROP_PV 0
101__UNDEFINED__ SV_UTF8_NO_ENCODING 0
102__UNDEFINED__ SV_NOSTEAL 0
103__UNDEFINED__ SV_CONST_RETURN 0
104__UNDEFINED__ SV_MUTABLE_RETURN 0
105__UNDEFINED__ SV_SMAGIC 0
106__UNDEFINED__ SV_HAS_TRAILING_NUL 0
107__UNDEFINED__ SV_COW_SHARED_HASH_KEYS 0
108
109#if { VERSION < 5.7.2 }
110
111#if { NEED sv_2pv_flags }
112
113char *
114sv_2pv_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
115{
116 STRLEN n_a = (STRLEN) flags;
117 return sv_2pv(sv, lp ? lp : &n_a);
118}
119
120#endif
121
122#if { NEED sv_pvn_force_flags }
123
124char *
125sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
126{
127 STRLEN n_a = (STRLEN) flags;
128 return sv_pvn_force(sv, lp ? lp : &n_a);
129}
130
131#endif
132
133#endif
134
8d2082b0 135#if { VERSION < 5.8.8 } || ( { VERSION >= 5.9.0 } && { VERSION < 5.9.3 } )
136# define DPPP_SVPV_NOLEN_LP_ARG &PL_na
137#else
138# define DPPP_SVPV_NOLEN_LP_ARG 0
139#endif
140
679ad62d 141__UNDEFINED__ SvPV_const(sv, lp) SvPV_flags_const(sv, lp, SV_GMAGIC)
142__UNDEFINED__ SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
143
144__UNDEFINED__ SvPV_flags(sv, lp, flags) \
145 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
146 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
147
148__UNDEFINED__ SvPV_flags_const(sv, lp, flags) \
149 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
150 ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
151 (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
152
153__UNDEFINED__ SvPV_flags_const_nolen(sv, flags) \
154 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
155 ? SvPVX_const(sv) : \
8d2082b0 156 (const char*) sv_2pv_flags(sv, DPPP_SVPV_NOLEN_LP_ARG, flags|SV_CONST_RETURN))
679ad62d 157
158__UNDEFINED__ SvPV_flags_mutable(sv, lp, flags) \
159 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
160 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
161 sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
162
163__UNDEFINED__ SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
164__UNDEFINED__ SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
165__UNDEFINED__ SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
166__UNDEFINED__ SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
167__UNDEFINED__ SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
168
169__UNDEFINED__ SvPV_force_flags(sv, lp, flags) \
170 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
171 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
172
173__UNDEFINED__ SvPV_force_flags_nolen(sv, flags) \
174 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
8d2082b0 175 ? SvPVX(sv) : sv_pvn_force_flags(sv, DPPP_SVPV_NOLEN_LP_ARG, flags))
679ad62d 176
177__UNDEFINED__ SvPV_force_flags_mutable(sv, lp, flags) \
178 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
179 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
180 : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
181
182__UNDEFINED__ SvPV_nolen(sv) \
183 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
8d2082b0 184 ? SvPVX(sv) : sv_2pv_flags(sv, DPPP_SVPV_NOLEN_LP_ARG, SV_GMAGIC))
679ad62d 185
186__UNDEFINED__ SvPV_nolen_const(sv) \
187 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
8d2082b0 188 ? SvPVX_const(sv) : sv_2pv_flags(sv, DPPP_SVPV_NOLEN_LP_ARG, SV_GMAGIC|SV_CONST_RETURN))
679ad62d 189
190__UNDEFINED__ SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
191__UNDEFINED__ SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
192__UNDEFINED__ SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
193
c01be2ce 194__UNDEFINED__ SvPV_renew(sv,n) STMT_START { SvLEN_set(sv, n); \
195 SvPV_set((sv), (char *) saferealloc( \
196 (Malloc_t)SvPVX(sv), (MEM_SIZE)((n)))); \
197 } STMT_END
198
adfe19db 199=xsinit
200
679ad62d 201#define NEED_sv_2pv_flags
202#define NEED_sv_pvn_force_flags
adfe19db 203#define NEED_sv_2pvbyte
204
205=xsubs
206
207IV
208SvPVbyte(sv)
209 SV *sv
210 PREINIT:
679ad62d 211 char *str;
adfe19db 212 STRLEN len;
adfe19db 213 CODE:
214 str = SvPVbyte(sv, len);
0d0f8426 215 RETVAL = strEQ(str, "mhx") ? (IV) len : (IV) -1;
adfe19db 216 OUTPUT:
217 RETVAL
218
219IV
220SvPV_nolen(sv)
221 SV *sv
222 PREINIT:
679ad62d 223 char *str;
adfe19db 224 CODE:
225 str = SvPV_nolen(sv);
9132e1a3 226 RETVAL = strEQ(str, "mhx") ? 42 : 0;
adfe19db 227 OUTPUT:
228 RETVAL
229
679ad62d 230IV
231SvPV_const(sv)
232 SV *sv
233 PREINIT:
234 const char *str;
235 STRLEN len;
236 CODE:
237 str = SvPV_const(sv, len);
238 RETVAL = len + (strEQ(str, "mhx") ? 40 : 0);
239 OUTPUT:
240 RETVAL
241
242IV
243SvPV_mutable(sv)
244 SV *sv
245 PREINIT:
246 char *str;
247 STRLEN len;
248 CODE:
249 str = SvPV_mutable(sv, len);
250 RETVAL = len + (strEQ(str, "mhx") ? 41 : 0);
251 OUTPUT:
252 RETVAL
253
254IV
255SvPV_flags(sv)
256 SV *sv
257 PREINIT:
258 char *str;
259 STRLEN len;
260 CODE:
261 str = SvPV_flags(sv, len, SV_GMAGIC);
262 RETVAL = len + (strEQ(str, "mhx") ? 42 : 0);
263 OUTPUT:
264 RETVAL
265
266IV
267SvPV_flags_const(sv)
268 SV *sv
269 PREINIT:
270 const char *str;
271 STRLEN len;
272 CODE:
273 str = SvPV_flags_const(sv, len, SV_GMAGIC);
274 RETVAL = len + (strEQ(str, "mhx") ? 43 : 0);
275 OUTPUT:
276 RETVAL
277
278IV
279SvPV_flags_const_nolen(sv)
280 SV *sv
281 PREINIT:
282 const char *str;
283 CODE:
284 str = SvPV_flags_const_nolen(sv, SV_GMAGIC);
285 RETVAL = strEQ(str, "mhx") ? 47 : 0;
286 OUTPUT:
287 RETVAL
288
289IV
290SvPV_flags_mutable(sv)
291 SV *sv
292 PREINIT:
293 char *str;
294 STRLEN len;
295 CODE:
296 str = SvPV_flags_mutable(sv, len, SV_GMAGIC);
297 RETVAL = len + (strEQ(str, "mhx") ? 45 : 0);
298 OUTPUT:
299 RETVAL
300
301IV
302SvPV_force(sv)
303 SV *sv
304 PREINIT:
305 char *str;
306 STRLEN len;
307 CODE:
308 str = SvPV_force(sv, len);
309 RETVAL = len + (strEQ(str, "mhx") ? 46 : 0);
310 OUTPUT:
311 RETVAL
312
313IV
314SvPV_force_nolen(sv)
315 SV *sv
316 PREINIT:
317 char *str;
318 CODE:
319 str = SvPV_force_nolen(sv);
320 RETVAL = strEQ(str, "mhx") ? 50 : 0;
321 OUTPUT:
322 RETVAL
323
324IV
325SvPV_force_mutable(sv)
326 SV *sv
327 PREINIT:
328 char *str;
329 STRLEN len;
330 CODE:
331 str = SvPV_force_mutable(sv, len);
332 RETVAL = len + (strEQ(str, "mhx") ? 48 : 0);
333 OUTPUT:
334 RETVAL
335
336IV
337SvPV_force_nomg(sv)
338 SV *sv
339 PREINIT:
340 char *str;
341 STRLEN len;
342 CODE:
343 str = SvPV_force_nomg(sv, len);
344 RETVAL = len + (strEQ(str, "mhx") ? 49 : 0);
345 OUTPUT:
346 RETVAL
347
348IV
349SvPV_force_nomg_nolen(sv)
350 SV *sv
351 PREINIT:
352 char *str;
353 CODE:
354 str = SvPV_force_nomg_nolen(sv);
355 RETVAL = strEQ(str, "mhx") ? 53 : 0;
356 OUTPUT:
357 RETVAL
358
359IV
360SvPV_force_flags(sv)
361 SV *sv
362 PREINIT:
363 char *str;
364 STRLEN len;
365 CODE:
366 str = SvPV_force_flags(sv, len, SV_GMAGIC);
367 RETVAL = len + (strEQ(str, "mhx") ? 51 : 0);
368 OUTPUT:
369 RETVAL
370
371IV
372SvPV_force_flags_nolen(sv)
373 SV *sv
374 PREINIT:
375 char *str;
376 CODE:
377 str = SvPV_force_flags_nolen(sv, SV_GMAGIC);
378 RETVAL = strEQ(str, "mhx") ? 55 : 0;
379 OUTPUT:
380 RETVAL
381
382IV
383SvPV_force_flags_mutable(sv)
384 SV *sv
385 PREINIT:
386 char *str;
387 STRLEN len;
388 CODE:
389 str = SvPV_force_flags_mutable(sv, len, SV_GMAGIC);
390 RETVAL = len + (strEQ(str, "mhx") ? 53 : 0);
391 OUTPUT:
392 RETVAL
393
394IV
395SvPV_nolen_const(sv)
396 SV *sv
397 PREINIT:
398 const char *str;
399 CODE:
400 str = SvPV_nolen_const(sv);
401 RETVAL = strEQ(str, "mhx") ? 57 : 0;
402 OUTPUT:
403 RETVAL
404
405IV
406SvPV_nomg(sv)
407 SV *sv
408 PREINIT:
409 char *str;
410 STRLEN len;
411 CODE:
412 str = SvPV_nomg(sv, len);
413 RETVAL = len + (strEQ(str, "mhx") ? 55 : 0);
414 OUTPUT:
415 RETVAL
416
417IV
418SvPV_nomg_const(sv)
419 SV *sv
420 PREINIT:
421 const char *str;
422 STRLEN len;
423 CODE:
424 str = SvPV_nomg_const(sv, len);
425 RETVAL = len + (strEQ(str, "mhx") ? 56 : 0);
426 OUTPUT:
427 RETVAL
428
429IV
430SvPV_nomg_const_nolen(sv)
431 SV *sv
432 PREINIT:
433 const char *str;
434 CODE:
435 str = SvPV_nomg_const_nolen(sv);
436 RETVAL = strEQ(str, "mhx") ? 60 : 0;
437 OUTPUT:
438 RETVAL
439
c01be2ce 440void
441SvPV_renew(sv, nlen, insv)
442 SV *sv
443 IV nlen
444 SV *insv
445 PREINIT:
446 STRLEN slen;
447 const char *str;
448 PPCODE:
449 str = SvPV_const(insv, slen);
450 XPUSHs(sv);
451 mXPUSHi(SvLEN(sv));
452 SvPV_renew(sv, nlen);
453 Copy(str, SvPVX(sv), slen + 1, char);
454 SvCUR_set(sv, slen);
455 mXPUSHi(SvLEN(sv));
456
679ad62d 457
c01be2ce 458=tests plan => 47
679ad62d 459
460my $mhx = "mhx";
461
462ok(&Devel::PPPort::SvPVbyte($mhx), 3);
463
464my $i = 42;
465
466ok(&Devel::PPPort::SvPV_nolen($mhx), $i++);
467ok(&Devel::PPPort::SvPV_const($mhx), $i++);
468ok(&Devel::PPPort::SvPV_mutable($mhx), $i++);
469ok(&Devel::PPPort::SvPV_flags($mhx), $i++);
470ok(&Devel::PPPort::SvPV_flags_const($mhx), $i++);
471
472ok(&Devel::PPPort::SvPV_flags_const_nolen($mhx), $i++);
473ok(&Devel::PPPort::SvPV_flags_mutable($mhx), $i++);
474ok(&Devel::PPPort::SvPV_force($mhx), $i++);
475ok(&Devel::PPPort::SvPV_force_nolen($mhx), $i++);
476ok(&Devel::PPPort::SvPV_force_mutable($mhx), $i++);
477
478ok(&Devel::PPPort::SvPV_force_nomg($mhx), $i++);
479ok(&Devel::PPPort::SvPV_force_nomg_nolen($mhx), $i++);
480ok(&Devel::PPPort::SvPV_force_flags($mhx), $i++);
481ok(&Devel::PPPort::SvPV_force_flags_nolen($mhx), $i++);
482ok(&Devel::PPPort::SvPV_force_flags_mutable($mhx), $i++);
adfe19db 483
679ad62d 484ok(&Devel::PPPort::SvPV_nolen_const($mhx), $i++);
485ok(&Devel::PPPort::SvPV_nomg($mhx), $i++);
486ok(&Devel::PPPort::SvPV_nomg_const($mhx), $i++);
487ok(&Devel::PPPort::SvPV_nomg_const_nolen($mhx), $i++);
adfe19db 488
8d2082b0 489$mhx = 42; ok(&Devel::PPPort::SvPV_nolen($mhx), 0);
490$mhx = 42; ok(&Devel::PPPort::SvPV_const($mhx), 2);
491$mhx = 42; ok(&Devel::PPPort::SvPV_mutable($mhx), 2);
492$mhx = 42; ok(&Devel::PPPort::SvPV_flags($mhx), 2);
493$mhx = 42; ok(&Devel::PPPort::SvPV_flags_const($mhx), 2);
494
495$mhx = 42; ok(&Devel::PPPort::SvPV_flags_const_nolen($mhx), 0);
496$mhx = 42; ok(&Devel::PPPort::SvPV_flags_mutable($mhx), 2);
497$mhx = 42; ok(&Devel::PPPort::SvPV_force($mhx), 2);
498$mhx = 42; ok(&Devel::PPPort::SvPV_force_nolen($mhx), 0);
499$mhx = 42; ok(&Devel::PPPort::SvPV_force_mutable($mhx), 2);
500
501$mhx = 42; ok(&Devel::PPPort::SvPV_force_nomg($mhx), 2);
502$mhx = 42; ok(&Devel::PPPort::SvPV_force_nomg_nolen($mhx), 0);
503$mhx = 42; ok(&Devel::PPPort::SvPV_force_flags($mhx), 2);
504$mhx = 42; ok(&Devel::PPPort::SvPV_force_flags_nolen($mhx), 0);
505$mhx = 42; ok(&Devel::PPPort::SvPV_force_flags_mutable($mhx), 2);
506
507$mhx = 42; ok(&Devel::PPPort::SvPV_nolen_const($mhx), 0);
508$mhx = 42; ok(&Devel::PPPort::SvPV_nomg($mhx), 2);
509$mhx = 42; ok(&Devel::PPPort::SvPV_nomg_const($mhx), 2);
510$mhx = 42; ok(&Devel::PPPort::SvPV_nomg_const_nolen($mhx), 0);
511
c01be2ce 512my $str = "";
513my($s2, $before, $after) = &Devel::PPPort::SvPV_renew($str, 81, "x"x80);
514ok($str, "x"x80);
515ok($s2, "x"x80);
516ok($before < 81);
517ok($after, 81);
518
519$str = "x"x400;
520($s2, $before, $after) = &Devel::PPPort::SvPV_renew($str, 41, "x"x40);
521ok($str, "x"x40);
522ok($s2, "x"x40);
523ok($before > 41);
524ok($after, 41);