include/slug_transform.h

filter: Statementstmt 98.7% 78 / 79 Branchbran 83.3% 75 / 90 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.5% 153 / 169 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/slug_unicode.h ↑ Summary
1 #ifndef SLUG_TRANSFORM_H
2 #define SLUG_TRANSFORM_H
3  
4 /*
5 * slug_transform.h — Core slug generation algorithm
6 *
7 * Pure C, no Perl dependencies.
8 * Single-pass streaming: decode UTF-8 → transliterate → lowercase →
9 * emit alphanumeric or collapse to separator.
10 */
11  
12 #include <string.h>
13 #include "slug_utf8.h"
14 #include "slug_unicode.h"
15  
16 /* ── Options ──────────────────────────────────────────────────── */
17  
18 typedef struct {
19 const char *separator; /* separator string (default "-") */
20 int sep_len; /* cached strlen(separator) */
21 int max_length; /* 0 = unlimited */
22 int lowercase; /* 1 = force lowercase */
23 int transliterate; /* 1 = unicode->ascii */
24 int trim_sep; /* 1 = trim leading/trailing sep */
25 } slug_opts_t;
26  
27 #define SLUG_OPTS_DEFAULT { "-", 1, 0, 1, 1, 1 }
28  
29 /* ── ASCII helpers ────────────────────────────────────────────── */
30  
31 6708 static inline int slug_is_alnum(unsigned char c) {
32 6708 ▶ return (c >= 'a' && c <= 'z') ||
Branch: 2 branches
BranchTrueFalseTotal
return (c >= 'a' && c <= 'z') || 0 5475 5475
return (c >= 'a' && c <= 'z') || 109 1124 1233
33 13442 ▶ (c >= 'A' && c <= 'Z') ||
Branch: 3 branches
BranchTrueFalseTotal
(c >= 'A' && c <= 'Z') || 5475 1233 6708
(c >= 'A' && c <= 'Z') || 2 107 109
(c >= 'A' && c <= 'Z') || 26 1100 1126
34 26 ▶ (c >= '0' && c <= '9');
Branch: 1 branch
BranchTrueFalseTotal
(c >= '0' && c <= '9'); 20 6 26
35 }
36  
37 5578 static inline unsigned char slug_tolower(unsigned char c) {
38 5578 ▶ return (c >= 'A' && c <= 'Z') ? (c + 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
return (c >= 'A' && c <= 'Z') ? (c + 32) : c; 5558 20 5578
return (c >= 'A' && c <= 'Z') ? (c + 32) : c; 102 5456 5558
39 }
40  
41 /* ── Emit helpers ─────────────────────────────────────────────── */
42  
43 /* Emit a character, respecting max_length. Returns 1 if max exceeded. */
44 5602 static inline int slug_emit_char(char *out, int *pos, int max, unsigned char c) {
45 5602 ▶ if (max > 0 && *pos >= max) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (max > 0 && *pos >= max) return 1; 5602 0 5602
if (max > 0 && *pos >= max) return 1; 5 5597 5602
46 5597 out[(*pos)++] = c;
47 5597 return 0;
48 }
49  
50 /* Emit a separator, collapsing consecutive. Returns 1 if max exceeded. */
51 1109 static inline int slug_emit_sep(char *out, int *pos, int max,
52 const slug_opts_t *opts, int *prev_sep) {
53 int i;
54 1109 ▶ if (*prev_sep) return 0; /* already have trailing sep */
Branch: 1 branch
BranchTrueFalseTotal
if (*prev_sep) return 0; /* already have trailing sep */ 43 1066 1109
55 1066 ▶ if (max > 0 && *pos + opts->sep_len > max) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (max > 0 && *pos + opts->sep_len > max) return 1; 1066 0 1066
if (max > 0 && *pos + opts->sep_len > max) return 1; 1 1065 1066
56 2133 ▶ for (i = 0; i < opts->sep_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < opts->sep_len; i++) { 1068 1065 2133
57 1068 out[(*pos)++] = opts->separator[i];
58 }
59 1065 *prev_sep = 1;
60 1065 return 0;
61 }
62  
63 /* ── Emit a transliterated/ASCII string into the slug ─────────── */
64  
65 75 static inline int slug_emit_ascii_str(char *out, int *pos, int max,
66 const char *str, int str_len,
67 const slug_opts_t *opts,
68 int *prev_sep) {
69 int i;
70 163 ▶ for (i = 0; i < str_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < str_len; i++) { 88 75 163
71 88 unsigned char c = (unsigned char)str[i];
72 88 ▶ if (slug_is_alnum(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slug_is_alnum(c)) { 87 1 88
73 87 ▶ if (opts->lowercase)
Branch: 1 branch
BranchTrueFalseTotal
if (opts->lowercase) 86 1 87
74 86 c = slug_tolower(c);
75 87 *prev_sep = 0;
76 87 ▶ if (slug_emit_char(out, pos, max, c)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_char(out, pos, max, c)) return 1; 0 87 87
77 } else {
78 /* Non-alnum in replacement → separator */
79 1 ▶ if (slug_emit_sep(out, pos, max, opts, prev_sep)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_sep(out, pos, max, opts, prev_sep)) return 1; 0 1 1
80 }
81 }
82 75 return 0;
83 }
84  
85 /* ── Core slug generator ──────────────────────────────────────── */
86  
87 /*
88 * Generate a URL slug from UTF-8 input.
89 *
90 * Returns: length of output slug (not including NUL terminator).
91 * Output is always NUL-terminated if output_size > 0.
92 */
93 91 static inline int slug_generate(const char *input, int input_len,
94 char *output, int output_size,
95 const slug_opts_t *opts) {
96 91 const unsigned char *p = (const unsigned char *)input;
97 91 const unsigned char *end = p + input_len;
98 91 int pos = 0;
99 91 int prev_sep = opts->trim_sep ? 1 : 0; /* start as sep=1 → trims leading */
100 91 int max = opts->max_length;
101  
102 91 ▶ if (max <= 0 || max >= output_size)
Branch: 2 branches
BranchTrueFalseTotal
if (max <= 0 || max >= output_size) 9 82 91
if (max <= 0 || max >= output_size) 1 8 9
103 83 max = output_size - 1;
104  
105 6783 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 6698 85 6783
106 6698 unsigned char byte = *p;
107  
108 /* Fast path: ASCII */
109 6698 ▶ if (byte < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte < 0x80) { 6620 78 6698
110 6620 ▶ if (slug_is_alnum(byte)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slug_is_alnum(byte)) { 5515 1105 6620
111 5515 ▶ unsigned char c = opts->lowercase ? slug_tolower(byte) : byte;
Branch: 1 branch
BranchTrueFalseTotal
unsigned char c = opts->lowercase ? slug_tolower(byte) : byte; 5492 23 5515
112 5515 prev_sep = 0;
113 5515 ▶ if (slug_emit_char(output, &pos, max, c)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_char(output, &pos, max, c)) break; 5 5510 5515
114 } else {
115 /* Non-alnum ASCII → separator */
116 1105 ▶ if (slug_emit_sep(output, &pos, max, opts, &prev_sep)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_sep(output, &pos, max, opts, &prev_sep)) break; 1 1104 1105
117 }
118 6614 p++;
119 } else {
120 /* Multi-byte UTF-8 */
121 78 uint32_t cp = slug_utf8_decode(&p, end);
122  
123 78 ▶ if (opts->transliterate) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->transliterate) { 76 2 78
124 76 const char *repl = slug_transliterate(cp);
125 76 ▶ if (repl != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (repl != NULL) { 75 1 76
126 75 int rlen = (int)strlen(repl);
127 75 ▶ if (rlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rlen > 0) { 75 0 75
128 75 ▶ if (slug_emit_ascii_str(output, &pos, max, repl, rlen,
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_ascii_str(output, &pos, max, repl, rlen, 0 75 75
129 opts, &prev_sep))
130 0 break;
131 }
132 /* rlen == 0 → drop (soft sign, hard sign, etc.) */
133 } else {
134 /* No mapping → emit separator (drop the char) */
135 1 ▶ if (slug_emit_sep(output, &pos, max, opts, &prev_sep)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_sep(output, &pos, max, opts, &prev_sep)) break; 0 1 1
136 }
137 } else {
138 /* No transliteration → emit separator for non-ASCII */
139 2 ▶ if (slug_emit_sep(output, &pos, max, opts, &prev_sep)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (slug_emit_sep(output, &pos, max, opts, &prev_sep)) break; 0 2 2
140 }
141 }
142 }
143  
144 /* Trim trailing separator */
145 91 ▶ if (opts->trim_sep && pos > 0 && prev_sep) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts->trim_sep && pos > 0 && prev_sep) { 90 1 91
if (opts->trim_sep && pos > 0 && prev_sep) { 84 6 90
if (opts->trim_sep && pos > 0 && prev_sep) { 8 76 84
146 8 pos -= opts->sep_len;
147 8 ▶ if (pos < 0) pos = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos < 0) pos = 0; 0 8 8
148 }
149  
150 91 ▶ if (output_size > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (output_size > 0) 91 0 91
151 91 output[pos] = '\0';
152  
153 91 return pos;
154 }
155  
156 /* ── Transliterate-only mode (no slugification) ───────────────── */
157  
158 /*
159 * Transliterate Unicode to ASCII without slugifying.
160 * Preserves spaces, punctuation, and case.
161 *
162 * Returns: length of output (not including NUL terminator).
163 */
164 10 static inline int slug_transliterate_str(const char *input, int input_len,
165 char *output, int output_size) {
166 10 const unsigned char *p = (const unsigned char *)input;
167 10 const unsigned char *end = p + input_len;
168 10 int pos = 0;
169 10 int max = output_size - 1;
170  
171 99 ▶ while (p < end && pos < max) {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && pos < max) { 89 10 99
while (p < end && pos < max) { 89 0 89
172 89 unsigned char byte = *p;
173  
174 /* ASCII passthrough */
175 89 ▶ if (byte < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte < 0x80) { 68 21 89
176 68 output[pos++] = (char)byte;
177 68 p++;
178 } else {
179 21 uint32_t cp = slug_utf8_decode(&p, end);
180 21 const char *repl = slug_transliterate(cp);
181 21 ▶ if (repl != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (repl != NULL) { 21 0 21
182 21 int rlen = (int)strlen(repl);
183 int i;
184 45 ▶ for (i = 0; i < rlen && pos < max; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < rlen && pos < max; i++) { 24 21 45
for (i = 0; i < rlen && pos < max; i++) { 24 0 24
185 24 output[pos++] = repl[i];
186 }
187 }
188 /* No mapping → drop */
189 }
190 }
191  
192 10 ▶ if (output_size > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (output_size > 0) 10 0 10
193 10 output[pos] = '\0';
194  
195 10 return pos;
196 }
197  
198 #endif /* SLUG_TRANSFORM_H */
« include/slug_unicode.h ↑ Summary