Slug.xs

filter: Statementstmt 65.9% 89 / 135 Branchbran 40.2% 33 / 82 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 56.2% 122 / 217 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/slug_utf8.h ↑ Summary include/slug_unicode.h »
1 #include "slug.h"
2  
3 /* ── Helper: allocate output buffer scaled to input ───────────── */
4  
5 /* Transliteration can expand (e.g. ß→ss, Æ→AE) so allow 4x growth */
6 #define SLUG_BUF_SCALE 4
7  
8 /* ══════════════════════════════════════════════════════════════════
9 * Custom ops - bypass XS subroutine dispatch overhead (5.14+)
10 * ══════════════════════════════════════════════════════════════════ */
11  
12 #if PERL_VERSION >= 14
13  
14 /* ── Macro: generate ck_* check function ─────────────────────── */
15  
16 #define SLUG_CK(name) \
17 static OP *slug_ck_##name(pTHX_ OP *o, GV *namegv, SV *protosv) { \
18 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(protosv); \
19 o->op_ppaddr = pp_slug_##name; return o; \
20 }
21  
22 /* ── XOP descriptors ──────────────────────────────────────────── */
23  
24 static XOP slug_xop_slug, slug_xop_slug_ascii, slug_xop_slug_custom;
25  
26 /* ── pp_slug: slug($str) or slug($str, $sep) ─────────────────── */
27  
28 72 static OP *pp_slug_slug(pTHX) {
29 72 dSP;
30 72 I32 markix = POPMARK;
31 72 I32 ax = markix + 1;
32 72 I32 items = (I32)(SP - PL_stack_base) - markix - 1;
33 STRLEN len;
34 const char *str;
35 72 const char *sep = "-";
36 72 int sep_len = 1;
37 char *buf;
38 int out_len;
39 SV *result;
40 int buf_size;
41 72 slug_opts_t opts = SLUG_OPTS_DEFAULT;
42  
43 72 ▶ if (items < 1) croak("slug() requires at least 1 argument");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("slug() requires at least 1 argument"); 0 72 72
44  
45 72 str = SvPVutf8(PL_stack_base[ax], len);
46 72 ▶ if (items >= 2 && SvOK(PL_stack_base[ax + 1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 2 && SvOK(PL_stack_base[ax + 1])) { 10 62 72
if (items >= 2 && SvOK(PL_stack_base[ax + 1])) { 10 0 10
47 STRLEN slen;
48 10 sep = SvPV(PL_stack_base[ax + 1], slen);
49 10 sep_len = (int)slen;
50 }
51  
52 72 opts.separator = sep;
53 72 opts.sep_len = sep_len;
54  
55 72 buf_size = (int)(len * SLUG_BUF_SCALE) + sep_len + 1;
56 72 result = newSV(buf_size);
57 72 buf = SvPVX(result);
58  
59 72 out_len = slug_generate(str, (int)len, buf, buf_size, &opts);
60  
61 72 SvCUR_set(result, out_len);
62 72 SvPOK_on(result);
63  
64 72 SP = PL_stack_base + markix;
65 72 ▶ mXPUSHs(result);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(result); 0 72 72
66 72 PUTBACK;
67 72 return NORMAL;
68 }
69 72 SLUG_CK(slug)
70  
71 /* ── pp_slug_ascii: slug_ascii($str) ──────────────────────────── */
72  
73 10 static OP *pp_slug_slug_ascii(pTHX) {
74 10 dSP;
75 10 I32 markix = POPMARK;
76 10 I32 ax = markix + 1;
77 10 I32 items = (I32)(SP - PL_stack_base) - markix - 1;
78 STRLEN len;
79 const char *str;
80 char *buf;
81 int out_len;
82 SV *result;
83 int buf_size;
84  
85 10 ▶ if (items < 1) croak("slug_ascii() requires 1 argument");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("slug_ascii() requires 1 argument"); 0 10 10
86  
87 10 str = SvPVutf8(PL_stack_base[ax], len);
88  
89 10 buf_size = (int)(len * SLUG_BUF_SCALE) + 1;
90 10 result = newSV(buf_size);
91 10 buf = SvPVX(result);
92  
93 10 out_len = slug_transliterate_str(str, (int)len, buf, buf_size);
94  
95 10 SvCUR_set(result, out_len);
96 10 SvPOK_on(result);
97  
98 10 SP = PL_stack_base + markix;
99 10 ▶ mXPUSHs(result);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(result); 0 10 10
100 10 PUTBACK;
101 10 return NORMAL;
102 }
103 10 SLUG_CK(slug_ascii)
104  
105 /* ── pp_slug_custom: slug_custom($str, \%opts) ────────────────── */
106  
107 20 static OP *pp_slug_slug_custom(pTHX) {
108 20 dSP;
109 20 I32 markix = POPMARK;
110 20 I32 ax = markix + 1;
111 20 I32 items = (I32)(SP - PL_stack_base) - markix - 1;
112 STRLEN len;
113 const char *str;
114 char *buf;
115 int out_len;
116 SV *result;
117 int buf_size;
118 20 slug_opts_t opts = SLUG_OPTS_DEFAULT;
119 SV **svp;
120 HV *hv;
121  
122 20 ▶ if (items < 1) croak("slug_custom() requires at least 1 argument");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("slug_custom() requires at least 1 argument"); 0 20 20
123  
124 20 str = SvPVutf8(PL_stack_base[ax], len);
125  
126 20 ▶ if (items >= 2 && SvOK(PL_stack_base[ax + 1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 2 && SvOK(PL_stack_base[ax + 1])) { 19 1 20
if (items >= 2 && SvOK(PL_stack_base[ax + 1])) { 19 0 19
127 19 ▶ if (!SvROK(PL_stack_base[ax + 1]) ||
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(PL_stack_base[ax + 1]) || 18 1 19
128 18 ▶ SvTYPE(SvRV(PL_stack_base[ax + 1])) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(PL_stack_base[ax + 1])) != SVt_PVHV) 0 18 18
129 1 croak("slug_custom() second argument must be a hash reference");
130  
131 18 hv = (HV *)SvRV(PL_stack_base[ax + 1]);
132  
133 18 svp = hv_fetchs(hv, "separator", 0);
134 18 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 5 13 18
if (svp && SvOK(*svp)) { 5 0 5
135 STRLEN slen;
136 5 opts.separator = SvPV(*svp, slen);
137 5 opts.sep_len = (int)slen;
138 }
139 18 svp = hv_fetchs(hv, "max_length", 0);
140 18 ▶ if (svp && SvOK(*svp)) opts.max_length = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.max_length = SvIV(*svp); 10 8 18
if (svp && SvOK(*svp)) opts.max_length = SvIV(*svp); 10 0 10
141 18 svp = hv_fetchs(hv, "lowercase", 0);
142 18 ▶ if (svp && SvOK(*svp)) opts.lowercase = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.lowercase = SvIV(*svp); 3 15 18
if (svp && SvOK(*svp)) opts.lowercase = SvIV(*svp); 3 0 3
143 18 svp = hv_fetchs(hv, "transliterate", 0);
144 18 ▶ if (svp && SvOK(*svp)) opts.transliterate = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.transliterate = SvIV(*svp); 3 15 18
if (svp && SvOK(*svp)) opts.transliterate = SvIV(*svp); 3 0 3
145 18 svp = hv_fetchs(hv, "trim_separator", 0);
146 18 ▶ if (svp && SvOK(*svp)) opts.trim_sep = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.trim_sep = SvIV(*svp); 2 16 18
if (svp && SvOK(*svp)) opts.trim_sep = SvIV(*svp); 2 0 2
147 }
148  
149 19 buf_size = (int)(len * SLUG_BUF_SCALE) + opts.sep_len + 1;
150 19 result = newSV(buf_size);
151 19 buf = SvPVX(result);
152  
153 19 out_len = slug_generate(str, (int)len, buf, buf_size, &opts);
154  
155 19 SvCUR_set(result, out_len);
156 19 SvPOK_on(result);
157  
158 19 SP = PL_stack_base + markix;
159 19 ▶ mXPUSHs(result);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(result); 0 19 19
160 19 PUTBACK;
161 19 return NORMAL;
162 }
163 20 SLUG_CK(slug_custom)
164  
165 /* ── Registration macros ──────────────────────────────────────── */
166  
167 #define SLUG_REG_XOP(c_name, desc) \
168 XopENTRY_set(&slug_xop_##c_name, xop_name, "slug_" #c_name); \
169 XopENTRY_set(&slug_xop_##c_name, xop_desc, desc); \
170 Perl_custom_op_register(aTHX_ pp_slug_##c_name, &slug_xop_##c_name);
171  
172 #define SLUG_REG_CK(perl_name, c_name) { \
173 CV *cv = get_cv("Slug::" perl_name, 0); \
174 if (cv) cv_set_call_checker(cv, slug_ck_##c_name, (SV *)cv); \
175 }
176  
177 9 static void slug_register_custom_ops(pTHX) {
178 9 SLUG_REG_XOP(slug, "generate URL slug")
179 9 SLUG_REG_XOP(slug_ascii, "transliterate to ASCII")
180 9 SLUG_REG_XOP(slug_custom,"generate slug with options")
181  
182 9 ▶ SLUG_REG_CK("slug", slug)
Branch: 1 branch
BranchTrueFalseTotal
SLUG_REG_CK("slug", slug) 9 0 9
183 9 ▶ SLUG_REG_CK("slug_ascii", slug_ascii)
Branch: 1 branch
BranchTrueFalseTotal
SLUG_REG_CK("slug_ascii", slug_ascii) 9 0 9
184 9 ▶ SLUG_REG_CK("slug_custom", slug_custom)
Branch: 1 branch
BranchTrueFalseTotal
SLUG_REG_CK("slug_custom", slug_custom) 9 0 9
185 9 }
186  
187 #endif /* PERL_VERSION >= 14 */
188  
189 /* ══════════════════════════════════════════════════════════════════
190 * XS module definition - XSUBs serve as fallbacks for Perl < 5.14
191 * ══════════════════════════════════════════════════════════════════ */
192  
193 MODULE = Slug PACKAGE = Slug
194  
195 PROTOTYPES: DISABLE
196  
197 BOOT:
198 {
199 #if PERL_VERSION >= 14
200 9 slug_register_custom_ops(aTHX);
201 #endif
202 }
203  
204 SV *
205 slug(input, ...)
206 SV *input
207 CODE:
208 {
209 STRLEN len;
210 0 const char *str = SvPVutf8(input, len);
211 0 const char *sep = "-";
212 0 int sep_len = 1;
213 int buf_size;
214 char *buf;
215 int out_len;
216 0 slug_opts_t opts = SLUG_OPTS_DEFAULT;
217  
218 0 ▶ if (items >= 2 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 2 && SvOK(ST(1))) { 0 0 0
if (items >= 2 && SvOK(ST(1))) { 0 0 0
219 STRLEN slen;
220 0 sep = SvPV(ST(1), slen);
221 0 sep_len = (int)slen;
222 }
223  
224 0 opts.separator = sep;
225 0 opts.sep_len = sep_len;
226  
227 0 buf_size = (int)(len * SLUG_BUF_SCALE) + sep_len + 1;
228 0 RETVAL = newSV(buf_size);
229 0 buf = SvPVX(RETVAL);
230 0 out_len = slug_generate(str, (int)len, buf, buf_size, &opts);
231 0 SvCUR_set(RETVAL, out_len);
232 0 SvPOK_on(RETVAL);
233 }
234 OUTPUT:
235 RETVAL
236  
237 SV *
238 slug_ascii(input)
239 SV *input
240 CODE:
241 {
242 STRLEN len;
243 0 const char *str = SvPVutf8(input, len);
244 0 int buf_size = (int)(len * SLUG_BUF_SCALE) + 1;
245 char *buf;
246 int out_len;
247  
248 0 RETVAL = newSV(buf_size);
249 0 buf = SvPVX(RETVAL);
250 0 out_len = slug_transliterate_str(str, (int)len, buf, buf_size);
251 0 SvCUR_set(RETVAL, out_len);
252 0 SvPOK_on(RETVAL);
253 }
254 OUTPUT:
255 RETVAL
256  
257 SV *
258 slug_custom(input, ...)
259 SV *input
260 CODE:
261 {
262 STRLEN len;
263 0 const char *str = SvPVutf8(input, len);
264 int buf_size;
265 char *buf;
266 int out_len;
267 0 slug_opts_t opts = SLUG_OPTS_DEFAULT;
268  
269 0 ▶ if (items >= 2 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 2 && SvOK(ST(1))) { 0 0 0
if (items >= 2 && SvOK(ST(1))) { 0 0 0
270 HV *hv;
271 SV **svp;
272 0 ▶ if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVHV) 0 0 0
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVHV) 0 0 0
273 0 croak("slug_custom() second argument must be a hash reference");
274 0 hv = (HV *)SvRV(ST(1));
275  
276 0 svp = hv_fetchs(hv, "separator", 0);
277 0 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 0 0 0
if (svp && SvOK(*svp)) { 0 0 0
278 STRLEN slen;
279 0 opts.separator = SvPV(*svp, slen);
280 0 opts.sep_len = (int)slen;
281 }
282 0 svp = hv_fetchs(hv, "max_length", 0);
283 0 ▶ if (svp && SvOK(*svp)) opts.max_length = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.max_length = SvIV(*svp); 0 0 0
if (svp && SvOK(*svp)) opts.max_length = SvIV(*svp); 0 0 0
284 0 svp = hv_fetchs(hv, "lowercase", 0);
285 0 ▶ if (svp && SvOK(*svp)) opts.lowercase = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.lowercase = SvIV(*svp); 0 0 0
if (svp && SvOK(*svp)) opts.lowercase = SvIV(*svp); 0 0 0
286 0 svp = hv_fetchs(hv, "transliterate", 0);
287 0 ▶ if (svp && SvOK(*svp)) opts.transliterate = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.transliterate = SvIV(*svp); 0 0 0
if (svp && SvOK(*svp)) opts.transliterate = SvIV(*svp); 0 0 0
288 0 svp = hv_fetchs(hv, "trim_separator", 0);
289 0 ▶ if (svp && SvOK(*svp)) opts.trim_sep = SvIV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) opts.trim_sep = SvIV(*svp); 0 0 0
if (svp && SvOK(*svp)) opts.trim_sep = SvIV(*svp); 0 0 0
290 }
291  
292 0 buf_size = (int)(len * SLUG_BUF_SCALE) + opts.sep_len + 1;
293 0 RETVAL = newSV(buf_size);
294 0 buf = SvPVX(RETVAL);
295 0 out_len = slug_generate(str, (int)len, buf, buf_size, &opts);
296 0 SvCUR_set(RETVAL, out_len);
297 0 SvPOK_on(RETVAL);
298 }
299 OUTPUT:
300 RETVAL
« include/slug_utf8.h ↑ Summary include/slug_unicode.h »