summaryrefslogtreecommitdiff
path: root/dist/Devel-PPPort/parts/inc/SvPV
blob: 1d54c0f8b4616eb251d85501a40f2d73e6389957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
################################################################################
##
##  Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
##  Version 2.x, Copyright (C) 2001, Paul Marquess.
##  Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
##
##  This program is free software; you can redistribute it and/or
##  modify it under the same terms as Perl itself.
##
################################################################################

=provides

__UNDEFINED__
SvPVbyte
sv_2pvbyte
sv_2pv_flags
sv_pvn_force_flags

=implementation

/* Hint: sv_2pv_nolen
 * Use the SvPV_nolen() or SvPV_nolen_const() macros instead of sv_2pv_nolen().
 */

__UNDEFINED__  sv_2pv_nolen(sv)   SvPV_nolen(sv)

#ifdef SvPVbyte

/* Hint: SvPVbyte
 * Does not work in perl-5.6.1, ppport.h implements a version
 * borrowed from perl-5.7.3.
 */

#if { VERSION < 5.7.0 }

__UNDEFINED__ sv_2pvbyte(sv, lp) (sv_utf8_downgrade((sv), 0), SvPV((sv), *(lp)))

/* Hint: sv_2pvbyte
 * Use the SvPVbyte() macro instead of sv_2pvbyte().
 */

/* Replace sv_2pvbyte with SvPVbyte */

#undef SvPVbyte

#define SvPVbyte(sv, lp)                                                \
        ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)                \
         ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))

#endif

#else

#  define SvPVbyte          SvPV
#  define sv_2pvbyte        sv_2pv

#endif

__UNDEFINED__  sv_2pvbyte_nolen(sv)  sv_2pv_nolen(sv)

/* Hint: sv_pvn
 * Always use the SvPV() macro instead of sv_pvn().
 */

/* Replace sv_pvn with SvPV */

/* Hint: sv_pvn_force
 * Always use the SvPV_force() macro instead of sv_pvn_force().
 */

/* Replace sv_pvn_force with SvPV_force */

/* If these are undefined, they're not handled by the core anyway */
__UNDEFINED__  SV_IMMEDIATE_UNREF       0
__UNDEFINED__  SV_GMAGIC                0
__UNDEFINED__  SV_COW_DROP_PV           0
__UNDEFINED__  SV_UTF8_NO_ENCODING      0
__UNDEFINED__  SV_CONST_RETURN          0
__UNDEFINED__  SV_MUTABLE_RETURN        0
__UNDEFINED__  SV_SMAGIC                0
__UNDEFINED__  SV_HAS_TRAILING_NUL      0
__UNDEFINED__  SV_COW_SHARED_HASH_KEYS  0

#if defined(PERL_USE_GCC_BRACE_GROUPS)
  __UNDEFINED__ sv_2pv_flags(sv, lp, flags) ({ SV *_sv = (sv); const I32 _flags = (flags); STRLEN *_lp = lp; _lp = _lp ? : &PL_na; (!(_flags & SV_GMAGIC) && SvGMAGICAL(_sv)) ? ({ char *_pv; SvGMAGICAL_off(_sv); _pv = sv_2pv(_sv, _lp); SvGMAGICAL_on(_sv); _pv; }) : sv_2pv(_sv, _lp); })
  __UNDEFINED__ sv_pvn_force_flags(sv, lp, flags) ({ SV *_sv = (sv); const I32 _flags = (flags); STRLEN *_lp = lp; _lp = _lp ? : &PL_na; (!(_flags & SV_GMAGIC) && SvGMAGICAL(_sv)) ? ({ char *_pv; SvGMAGICAL_off(_sv); _pv = sv_pvn_force(_sv, _lp); SvGMAGICAL_on(_sv); _pv; }) : sv_pvn_force(_sv, _lp); })
#else
  __UNDEFINED__ sv_2pv_flags(sv, lp, flags) ((PL_Sv = (sv)), (!((flags) & SV_GMAGIC) && SvGMAGICAL(PL_Sv)) ? (SvGMAGICAL_off(PL_Sv), (PL_Xpv = (XPV *)sv_2pv(PL_Sv, (lp) ? (lp) : &PL_na)), SvGMAGICAL_on(PL_Sv), (char *)PL_Xpv) : sv_2pv(PL_Sv, (lp) ? (lp) : &PL_na))
  __UNDEFINED__ sv_pvn_force_flags(sv, lp, flags) ((PL_Sv = (sv)), (!((flags) & SV_GMAGIC) && SvGMAGICAL(PL_Sv)) ? (SvGMAGICAL_off(PL_Sv), (PL_Xpv = (XPV *)sv_pvn_force(PL_Sv, (lp) ? (lp) : &PL_na)), SvGMAGICAL_on(PL_Sv), (char *)PL_Xpv) : sv_pvn_force(PL_Sv, (lp) ? (lp) : &PL_na))
#endif

#if { VERSION < 5.8.8 } || ( { VERSION >= 5.9.0 } && { VERSION < 5.9.3 } )
# define D_PPP_SVPV_NOLEN_LP_ARG &PL_na
#else
# define D_PPP_SVPV_NOLEN_LP_ARG 0
#endif

__UNDEFINED__  SvPV_const(sv, lp)      SvPV_flags_const(sv, lp, SV_GMAGIC)
__UNDEFINED__  SvPV_mutable(sv, lp)    SvPV_flags_mutable(sv, lp, SV_GMAGIC)

__UNDEFINED__  SvPV_flags(sv, lp, flags) \
                 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                  ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))

__UNDEFINED__  SvPV_flags_const(sv, lp, flags) \
                 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                  ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
                  (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))

__UNDEFINED__  SvPV_flags_const_nolen(sv, flags) \
                 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                  ? SvPVX_const(sv) : \
                  (const char*) sv_2pv_flags(sv, D_PPP_SVPV_NOLEN_LP_ARG, flags|SV_CONST_RETURN))

__UNDEFINED__  SvPV_flags_mutable(sv, lp, flags) \
                 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                  ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
                  sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))

__UNDEFINED__  SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
__UNDEFINED__  SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
__UNDEFINED__  SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
__UNDEFINED__  SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
__UNDEFINED__  SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)

__UNDEFINED__  SvPV_force_flags(sv, lp, flags) \
                 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
                 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))

__UNDEFINED__  SvPV_force_flags_nolen(sv, flags) \
                 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
                 ? SvPVX(sv) : sv_pvn_force_flags(sv, D_PPP_SVPV_NOLEN_LP_ARG, flags))

__UNDEFINED__  SvPV_force_flags_mutable(sv, lp, flags) \
                 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
                 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
                  : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))

__UNDEFINED__  SvPV_nolen(sv) \
                 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                  ? SvPVX(sv) : sv_2pv_flags(sv, D_PPP_SVPV_NOLEN_LP_ARG, SV_GMAGIC))

__UNDEFINED__  SvPV_nolen_const(sv) \
                 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                  ? SvPVX_const(sv) : sv_2pv_flags(sv, D_PPP_SVPV_NOLEN_LP_ARG, SV_GMAGIC|SV_CONST_RETURN))
#  if defined(PERL_USE_GCC_BRACE_GROUPS)
__UNDEFINED__  SvPVx_nolen_const(sv) ({SV *sV_ = (sv); SvPV_nolen_const(sV_); })
#  else
__UNDEFINED__  SvPVx_nolen_const(sv)  (PL_Sv = sv, SvPV_nolen_const(PL_Sv))
#  endif

__UNDEFINED__  SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
__UNDEFINED__  SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
__UNDEFINED__  SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
__UNDEFINED__  SvPV_nomg_nolen(sv) ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
                                    ? SvPVX(sv) : sv_2pv_flags(sv, D_PPP_SVPV_NOLEN_LP_ARG, 0))

__UNDEFINED__  SvPV_renew(sv,n) STMT_START { SvLEN_set(sv, n); \
                 SvPV_set((sv), (char *) saferealloc(          \
                       (Malloc_t)SvPVX(sv), (MEM_SIZE)((n)))); \
               } STMT_END

__UNDEFINED__  SvPVCLEAR(sv) sv_setpvs((sv), "")

=xsubs

IV
SvPVbyte(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPVbyte(sv, len);
                RETVAL = strEQ(str, "mhx") ? (IV) len : (IV) -1;
        OUTPUT:
                RETVAL

IV
SvPV_nolen(sv)
        SV *sv
        PREINIT:
                char *str;
        CODE:
                str = SvPV_nolen(sv);
                RETVAL = strEQ(str, "mhx") ? 42 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_const(sv)
        SV *sv
        PREINIT:
                const char *str;
                STRLEN len;
        CODE:
                str = SvPV_const(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 40 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_mutable(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_mutable(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 41 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_flags(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_flags(sv, len, SV_GMAGIC);
                RETVAL = len + (strEQ(str, "mhx") ? 42 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_flags_const(sv)
        SV *sv
        PREINIT:
                const char *str;
                STRLEN len;
        CODE:
                str = SvPV_flags_const(sv, len, SV_GMAGIC);
                RETVAL = len + (strEQ(str, "mhx") ? 43 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_flags_const_nolen(sv)
        SV *sv
        PREINIT:
                const char *str;
        CODE:
                str = SvPV_flags_const_nolen(sv, SV_GMAGIC);
                RETVAL = strEQ(str, "mhx") ? 47 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_flags_mutable(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_flags_mutable(sv, len, SV_GMAGIC);
                RETVAL = len + (strEQ(str, "mhx") ? 45 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_force(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_force(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 46 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_force_nolen(sv)
        SV *sv
        PREINIT:
                char *str;
        CODE:
                str = SvPV_force_nolen(sv);
                RETVAL = strEQ(str, "mhx") ? 50 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_force_mutable(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_force_mutable(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 48 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_force_nomg(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_force_nomg(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 49 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_force_nomg_nolen(sv)
        SV *sv
        PREINIT:
                char *str;
        CODE:
                str = SvPV_force_nomg_nolen(sv);
                RETVAL = strEQ(str, "mhx") ? 53 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_force_flags(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_force_flags(sv, len, SV_GMAGIC);
                RETVAL = len + (strEQ(str, "mhx") ? 51 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_force_flags_nolen(sv)
        SV *sv
        PREINIT:
                char *str;
        CODE:
                str = SvPV_force_flags_nolen(sv, SV_GMAGIC);
                RETVAL = strEQ(str, "mhx") ? 55 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_force_flags_mutable(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_force_flags_mutable(sv, len, SV_GMAGIC);
                RETVAL = len + (strEQ(str, "mhx") ? 53 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_nolen_const(sv)
        SV *sv
        PREINIT:
                const char *str;
        CODE:
                str = SvPV_nolen_const(sv);
                RETVAL = strEQ(str, "mhx") ? 57 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_nomg(sv)
        SV *sv
        PREINIT:
                char *str;
                STRLEN len;
        CODE:
                str = SvPV_nomg(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 55 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_nomg_const(sv)
        SV *sv
        PREINIT:
                const char *str;
                STRLEN len;
        CODE:
                str = SvPV_nomg_const(sv, len);
                RETVAL = len + (strEQ(str, "mhx") ? 56 : 0);
        OUTPUT:
                RETVAL

IV
SvPV_nomg_const_nolen(sv)
        SV *sv
        PREINIT:
                const char *str;
        CODE:
                str = SvPV_nomg_const_nolen(sv);
                RETVAL = strEQ(str, "mhx") ? 60 : 0;
        OUTPUT:
                RETVAL

IV
SvPV_nomg_nolen(sv)
        SV *sv
        PREINIT:
                char *str;
        CODE:
                str = SvPV_nomg_nolen(sv);
                RETVAL = strEQ(str, "mhx") ? 61 : 0;
        OUTPUT:
                RETVAL

void
SvPV_renew(sv, nlen, insv)
        SV *sv
        STRLEN nlen
        SV *insv
        PREINIT:
                STRLEN slen;
                const char *str;
        PPCODE:
                str = SvPV_const(insv, slen);
                XPUSHs(sv);
                mXPUSHi(SvLEN(sv));
                SvPV_renew(sv, nlen);
                Copy(str, SvPVX(sv), slen + 1, char);
                SvCUR_set(sv, slen);
                mXPUSHi(SvLEN(sv));

void
SvPVCLEAR(sv)
        SV *sv
        CODE:
                SvPVCLEAR(sv);


=tests plan => 50

my $mhx = "mhx";

is(&Devel::PPPort::SvPVbyte($mhx), 3);

my $i = 42;

is(&Devel::PPPort::SvPV_nolen($mhx), $i++);
is(&Devel::PPPort::SvPV_const($mhx), $i++);
is(&Devel::PPPort::SvPV_mutable($mhx), $i++);
is(&Devel::PPPort::SvPV_flags($mhx), $i++);
is(&Devel::PPPort::SvPV_flags_const($mhx), $i++);

is(&Devel::PPPort::SvPV_flags_const_nolen($mhx), $i++);
is(&Devel::PPPort::SvPV_flags_mutable($mhx), $i++);
is(&Devel::PPPort::SvPV_force($mhx), $i++);
is(&Devel::PPPort::SvPV_force_nolen($mhx), $i++);
is(&Devel::PPPort::SvPV_force_mutable($mhx), $i++);

is(&Devel::PPPort::SvPV_force_nomg($mhx), $i++);
is(&Devel::PPPort::SvPV_force_nomg_nolen($mhx), $i++);
is(&Devel::PPPort::SvPV_force_flags($mhx), $i++);
is(&Devel::PPPort::SvPV_force_flags_nolen($mhx), $i++);
is(&Devel::PPPort::SvPV_force_flags_mutable($mhx), $i++);

is(&Devel::PPPort::SvPV_nolen_const($mhx), $i++);
is(&Devel::PPPort::SvPV_nomg($mhx), $i++);
is(&Devel::PPPort::SvPV_nomg_const($mhx), $i++);
is(&Devel::PPPort::SvPV_nomg_const_nolen($mhx), $i++);
is(&Devel::PPPort::SvPV_nomg_nolen($mhx), $i++);

$mhx = 42; is(&Devel::PPPort::SvPV_nolen($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_const($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_mutable($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_flags($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_flags_const($mhx), 2);

$mhx = 42; is(&Devel::PPPort::SvPV_flags_const_nolen($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_flags_mutable($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_force($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_force_nolen($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_force_mutable($mhx), 2);

$mhx = 42; is(&Devel::PPPort::SvPV_force_nomg($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_force_nomg_nolen($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_force_flags($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_force_flags_nolen($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_force_flags_mutable($mhx), 2);

$mhx = 42; is(&Devel::PPPort::SvPV_nolen_const($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_nomg($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_nomg_const($mhx), 2);
$mhx = 42; is(&Devel::PPPort::SvPV_nomg_const_nolen($mhx), 0);
$mhx = 42; is(&Devel::PPPort::SvPV_nomg_nolen($mhx), 0);

&Devel::PPPort::SvPVCLEAR($mhx);
is($mhx, "");

my $str = "";
&Devel::PPPort::SvPV_force($str);
my($s2, $before, $after) = &Devel::PPPort::SvPV_renew($str, 81, "x"x80);
is($str, "x"x80);
is($s2, "x"x80);
ok($before < 81);
is($after, 81);

$str = "x"x400;
&Devel::PPPort::SvPV_force($str);
($s2, $before, $after) = &Devel::PPPort::SvPV_renew($str, 41, "x"x40);
is($str, "x"x40);
is($s2, "x"x40);
ok($before > 41);
is($after, 41);