This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Provide my_strnlen()
[perl5.git] / dist / Devel-PPPort / parts / inc / uv
CommitLineData
adfe19db
MHM
1################################################################################
2##
b2049988 3## Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
adfe19db
MHM
4## Version 2.x, Copyright (C) 2001, Paul Marquess.
5## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
6##
7## This program is free software; you can redistribute it and/or
8## modify it under the same terms as Perl itself.
9##
10################################################################################
11
12=provides
13
adfe19db 14__UNDEFINED__
e28192fd 15my_strnlen
679ad62d 16SvUOK
adfe19db
MHM
17
18=implementation
19
0d0f8426
MHM
20__UNDEFINED__ sv_setuv(sv, uv) \
21 STMT_START { \
22 UV TeMpUv = uv; \
23 if (TeMpUv <= IV_MAX) \
24 sv_setiv(sv, TeMpUv); \
25 else \
26 sv_setnv(sv, (double)TeMpUv); \
27 } STMT_END
28
29__UNDEFINED__ newSVuv(uv) ((uv) <= IV_MAX ? newSViv((IV)uv) : newSVnv((NV)uv))
adfe19db
MHM
30
31__UNDEFINED__ sv_2uv(sv) ((PL_Sv = (sv)), (UV) (SvNOK(PL_Sv) ? SvNV(PL_Sv) : sv_2nv(PL_Sv)))
32__UNDEFINED__ SvUVX(sv) ((UV)SvIVX(sv))
33__UNDEFINED__ SvUVXx(sv) SvUVX(sv)
34__UNDEFINED__ SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
35__UNDEFINED__ SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
36
37/* Hint: sv_uv
38 * Always use the SvUVx() macro instead of sv_uv().
39 */
40__UNDEFINED__ sv_uv(sv) SvUVx(sv)
41
679ad62d
MHM
42#if !defined(SvUOK) && defined(SvIOK_UV)
43# define SvUOK(sv) SvIOK_UV(sv)
44#endif
45
adfe19db
MHM
46__UNDEFINED__ XST_mUV(i,v) (ST(i) = sv_2mortal(newSVuv(v)) )
47__UNDEFINED__ XSRETURN_UV(v) STMT_START { XST_mUV(0,v); XSRETURN(1); } STMT_END
48
96ad942f
MHM
49__UNDEFINED__ PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
50__UNDEFINED__ XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
51
f2e3e4ce
KW
52#if defined UTF8SKIP
53__UNDEFINED__ UTF8_SAFE_SKIP(s, e) (__ASSERT_((e) >= (s)) \
54 ((e) - (s)) <= 0 \
55 ? 0 \
56 : (((e) - (s)) >= UTF8SKIP(s)) \
57 ? ((e) - (s)) \
58 : UTF8SKIP(s))
59#endif
60
e28192fd
KW
61#if !defined(my_strnlen)
62#if { NEED my_strnlen }
63
64STRLEN
65my_strnlen(const char *str, Size_t maxlen)
66{
67 const char *p = str;
68
69 while(maxlen-- && *p)
70 p++;
71
72 return p - str;
73}
74
75#endif
76#endif
77
78=xsinit
79
80#define NEED_my_strnlen
81
adfe19db
MHM
82=xsubs
83
84SV *
85sv_setuv(uv)
b2049988
MHM
86 UV uv
87 CODE:
88 RETVAL = newSViv(1);
89 sv_setuv(RETVAL, uv);
90 OUTPUT:
91 RETVAL
adfe19db
MHM
92
93SV *
94newSVuv(uv)
b2049988
MHM
95 UV uv
96 CODE:
97 RETVAL = newSVuv(uv);
98 OUTPUT:
99 RETVAL
adfe19db
MHM
100
101UV
102sv_2uv(sv)
b2049988
MHM
103 SV *sv
104 CODE:
105 RETVAL = sv_2uv(sv);
106 OUTPUT:
107 RETVAL
adfe19db
MHM
108
109UV
110SvUVx(sv)
b2049988
MHM
111 SV *sv
112 CODE:
113 sv--;
114 RETVAL = SvUVx(++sv);
115 OUTPUT:
116 RETVAL
adfe19db
MHM
117
118void
119XSRETURN_UV()
b2049988
MHM
120 PPCODE:
121 XSRETURN_UV(42);
adfe19db 122
96ad942f
MHM
123void
124PUSHu()
b2049988
MHM
125 PREINIT:
126 dTARG;
127 PPCODE:
128 TARG = sv_newmortal();
129 EXTEND(SP, 1);
130 PUSHu(42);
131 XSRETURN(1);
96ad942f
MHM
132
133void
134XPUSHu()
b2049988
MHM
135 PREINIT:
136 dTARG;
137 PPCODE:
138 TARG = sv_newmortal();
139 XPUSHu(43);
140 XSRETURN(1);
96ad942f 141
f2e3e4ce
KW
142STRLEN
143UTF8_SAFE_SKIP(s, adjustment)
144 unsigned char * s
145 int adjustment
146 CODE:
147 /* Instead of passing in an 'e' ptr, use the real end, adjusted */
148 RETVAL = UTF8_SAFE_SKIP(s, s + UTF8SKIP(s) + adjustment);
149 OUTPUT:
150 RETVAL
151
e28192fd
KW
152STRLEN
153my_strnlen(s, max)
154 char * s
155 STRLEN max
156 CODE:
157 RETVAL= my_strnlen(s, max);
158 OUTPUT:
159 RETVAL
160
161=tests plan => 13
adfe19db
MHM
162
163ok(&Devel::PPPort::sv_setuv(42), 42);
164ok(&Devel::PPPort::newSVuv(123), 123);
165ok(&Devel::PPPort::sv_2uv("4711"), 4711);
166ok(&Devel::PPPort::sv_2uv("1735928559"), 1735928559);
167ok(&Devel::PPPort::SvUVx("1735928559"), 1735928559);
168ok(&Devel::PPPort::SvUVx(1735928559), 1735928559);
169ok(&Devel::PPPort::SvUVx(0xdeadbeef), 0xdeadbeef);
170ok(&Devel::PPPort::XSRETURN_UV(), 42);
96ad942f
MHM
171ok(&Devel::PPPort::PUSHu(), 42);
172ok(&Devel::PPPort::XPUSHu(), 43);
f2e3e4ce
KW
173ok(&Devel::PPPort::UTF8_SAFE_SKIP("A", 0), 1);
174ok(&Devel::PPPort::UTF8_SAFE_SKIP("A", -1), 0);
e28192fd 175ok(&Devel::PPPort::my_strnlen("abc\0def", 7), 3);