Debian Codegraph @

rjsmin.c

raw symbols SCIP
1 /*
2 * Copyright 2011 - 2025
3 * Andr\xe9 Malo or his licensors, as applicable
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "cext.h"
19 EXT_INIT_FUNC;
20
21 #define RJSMIN_DULL_BIT (1 << 0)
22 #define RJSMIN_PRE_REGEX_BIT (1 << 1)
23 #define RJSMIN_REGEX_DULL_BIT (1 << 2)
24 #define RJSMIN_REGEX_CC_DULL_BIT (1 << 3)
25 #define RJSMIN_ID_LIT_BIT (1 << 4)
26 #define RJSMIN_ID_LIT_O_BIT (1 << 5)
27 #define RJSMIN_ID_LIT_C_BIT (1 << 6)
28 #define RJSMIN_STRING_DULL_BIT (1 << 7)
29 #define RJSMIN_SPACE_BIT (1 << 8)
30 #define RJSMIN_POST_REGEX_OFF_BIT (1 << 9)
31 #define RJSMIN_A_Z_BIT (1 << 10)
32
33 typedef unsigned char rchar;
34 #ifdef U
35 #undef U
36 #endif
37 #define U(c) ((rchar)(c))
38
39 #define RJSMIN_IS_DULL(c) ((U(c) > 127) || \
40 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_DULL_BIT))
41
42 #define RJSMIN_IS_REGEX_DULL(c) ((U(c) > 127) || \
43 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_REGEX_DULL_BIT))
44
45 #define RJSMIN_IS_REGEX_CC_DULL(c) ((U(c) > 127) || \
46 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_REGEX_CC_DULL_BIT))
47
48 #define RJSMIN_IS_STRING_DULL(c) ((U(c) > 127) || \
49 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_STRING_DULL_BIT))
50
51 #define RJSMIN_IS_ID_LITERAL(c) ((U(c) > 127) || \
52 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_ID_LIT_BIT))
53
54 #define RJSMIN_IS_ID_LITERAL_OPEN(c) ((U(c) > 127) || \
55 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_ID_LIT_O_BIT))
56
57 #define RJSMIN_IS_ID_LITERAL_CLOSE(c) ((U(c) > 127) || \
58 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_ID_LIT_C_BIT))
59
60 #define RJSMIN_IS_POST_REGEX_OFF(c) ((U(c) > 127) || \
61 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_POST_REGEX_OFF_BIT))
62
63 #define RJSMIN_IS_SPACE(c) ((U(c) <= 127) && \
64 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_SPACE_BIT))
65
66 #define RJSMIN_IS_PRE_REGEX_1(c) ((U(c) <= 127) && \
67 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_PRE_REGEX_BIT))
68
69 #define RJSMIN_IS_A_Z(c) ((U(c) <= 127) && \
70 (rjsmin_charmask[U(c) & 0x7F] & RJSMIN_A_Z_BIT))
71
72
73 static const unsigned short rjsmin_charmask[128] = {
74 396, 396, 396, 396, 396, 396, 396, 396,
75 396, 396, 2, 396, 396, 2, 396, 396,
76 396, 396, 396, 396, 396, 396, 396, 396,
77 396, 396, 396, 396, 396, 396, 396, 396,
78 396, 687, 588, 653, 765, 653, 143, 588,
79 687, 205, 655, 239, 143, 239, 141, 648,
80 765, 765, 765, 765, 765, 765, 765, 765,
81 765, 765, 143, 143, 653, 143, 653, 143,
82 653, 765, 765, 765, 765, 765, 765, 765,
83 765, 765, 765, 765, 765, 765, 765, 765,
84 765, 765, 765, 765, 765, 765, 765, 765,
85 765, 765, 765, 683, 513, 197, 653, 765,
86 588, 1789, 1789, 1789, 1789, 1789, 1789, 1789,
87 1789, 1789, 1789, 1789, 1789, 1789, 1789, 1789,
88 1789, 1789, 1789, 1789, 1789, 1789, 1789, 1789,
89 1789, 1789, 1789, 687, 143, 207, 653, 765
90 };
91
92
93 static Py_ssize_t
94 rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
95 int keep_bang_comments)
96 {
97 const rchar
98 *sentinel = source + length, /* never hit this pointer (source buf) */
99 *reset, /* reset pointer (source buf) */
100 *pcreset = NULL, /* pre-comment reset pointer (source buf) */
101 *pctoken = NULL, /* pre-comment token pointer (target buf)
102 * Pointing to before the last kept comment, if any */
103 *rsreset = NULL, /* regex-with-method reset pointer (source buf) */
104 *xtarget; /* pre-regex-2 target pointer */
105
106 rchar *tstart = target, /* Target start pointer for reference */
107 *rtreset = NULL; /* regex-with-method reset pointer (target buf) */
108
109 int rsdot, /* seen dot after regex-with-method pattern? */
110 post_regex = 0;
111 rchar c, quote,
112 spaced = U(' '); /* the last seen kind of space (nl taking prio),
113 * init with ' ' */
114
115 /* main loop */
116 while (source < sentinel) {
117 c = *source++;
118
119 if (RJSMIN_IS_DULL(c)) {
120 if (post_regex) post_regex = 0;
121 if (pctoken) pctoken = NULL;
122 if (spaced == U('\n')) spaced = U(' ');
123 if (rsreset) {
124 /* both a-z and . are covered by "dull" */
125 if (!rsdot) {
126 if (c != U('.')) {
127 /* reset regex-with-method to the starting slash */
128 source = rsreset;
129 target = rtreset;
130 rsreset = NULL;
131 continue; /* main loop */
132 }
133 /* Found a dot after possible regex, looking for a-z now */
134 rsdot = 1;
135 }
136 else if (!RJSMIN_IS_A_Z(c)) {
137 /* reset regex-with-method to the starting slash */
138 source = rsreset;
139 target = rtreset;
140 rsreset = NULL;
141 continue; /* main loop */
142 }
143 else {
144 /* Successfull finish the regex-with-method match */
145 rsreset = NULL;
146 }
147 }
148
149 *target++ = c;
150 continue; /* main loop */
151 }
152
153 switch (c) {
154
155 /* String */
156 case U('\''): case U('"'): case U('`'):
157 if (post_regex) post_regex = 0;
158 if (pctoken) pctoken = NULL;
159 if (spaced == U('\n')) spaced = U(' ');
160 if (rsreset) {
161 /* reset regex-with-method to the starting slash */
162 source = rsreset;
163 target = rtreset;
164 rsreset = NULL;
165 continue; /* main loop */
166 }
167
168 reset = source;
169 *target++ = quote = c;
170
171 /* string loop */
172 while (source < sentinel) {
173 c = *source++;
174 *target++ = c;
175 if (RJSMIN_IS_STRING_DULL(c))
176 continue; /* string loop */
177
178 switch (c) {
179 case U('\''): case U('"'): case U('`'):
180 if (c == quote)
181 goto cont; /* main loop */
182 continue; /* string loop */
183 case U('\\'):
184 if (source < sentinel) {
185 c = *source++;
186 *target++ = c;
187 if (c == U('\r') && source < sentinel
188 && *source == U('\n'))
189 *target++ = *source++;
190 }
191 continue; /* string loop */
192 case U('\r'): case U('\n'):
193 if (quote != U('`'))
194 break; /* string reset */
195 continue; /* string loop */
196 }
197 break; /* string reset */
198 }
199 /* string reset */
200 target -= source - reset;
201 source = reset;
202 continue; /* main loop */
203
204 /* Comment or Regex or something else entirely */
205 case U('/'):
206 if (!(source < sentinel)) {
207 if (post_regex) post_regex = 0;
208 if (pctoken) pctoken = NULL;
209 if (spaced == U('\n')) spaced = U(' ');
210 if (rsreset) {
211 /* reset regex-with-method to the starting slash */
212 source = rsreset;
213 target = rtreset;
214 rsreset = NULL;
215 continue; /* main loop */
216 }
217
218 *target++ = c;
219 }
220 else {
221 switch (*source) {
222 /* Comment */
223 case U('*'): case U('/'):
224 goto skip_or_copy_ws;
225
226 /* Regex or slash */
227 default:
228 if (rsreset) {
229 /* reset regex-with-method to the starting slash */
230 if (post_regex) post_regex = 0;
231 if (pctoken) pctoken = NULL;
232 if (spaced == U('\n')) spaced = U(' ');
233 source = rsreset;
234 target = rtreset;
235 rsreset = NULL;
236 continue; /* main loop */
237 }
238
239 xtarget = NULL;
240 if ( target == tstart
241 || RJSMIN_IS_PRE_REGEX_1(*((pctoken ? pctoken : target)
242 - 1))
243 || (
244 (xtarget = pctoken ? pctoken : target)
245 && (xtarget - tstart >= 6)
246 && *(xtarget - 1) == U('n')
247 && *(xtarget - 2) == U('r')
248 && *(xtarget - 3) == U('u')
249 && *(xtarget - 4) == U('t')
250 && *(xtarget - 5) == U('e')
251 && *(xtarget - 6) == U('r')
252 && (
253 xtarget - tstart == 6
254 || !RJSMIN_IS_ID_LITERAL(*(xtarget - 7))
255 )
256 )) {
257 /* nothing to do here, continuing down below
258 * We could unset rsreset here, but we know it already
259 * is. */
260 ;
261 }
262 else if (*((pctoken ? pctoken : target) - 1) == U(')')) {
263 xtarget = NULL;
264 rsreset = source;
265 rtreset = target + 1;
266 rsdot = 0;
267 }
268 else {
269 /* Just a slash */
270 if (post_regex) post_regex = 0;
271 if (pctoken) pctoken = NULL;
272 if (spaced == U('\n')) spaced = U(' ');
273
274 *target++ = c;
275 continue; /* main loop */
276 }
277
278 if (post_regex) post_regex = 0;
279 if (pctoken) pctoken = NULL;
280
281 reset = source;
282 if (spaced == U('\n')) {
283 spaced = U(' ');
284 if (xtarget)
285 *target++ = U('\n');
286 }
287
288 *target++ = U('/');
289
290 /* regex loop */
291 while (source < sentinel) {
292 c = *source++;
293 *target++ = c;
294
295 if (RJSMIN_IS_REGEX_DULL(c))
296 continue; /* regex loop */
297
298 switch (c) {
299 case U('/'):
300 while (source < sentinel
301 && RJSMIN_IS_A_Z(*source))
302 *target++ = *source++;
303 post_regex = !rsreset;
304 /* This check is supposed to make it faster.
305 * It doesn't. It slows it down. I wonder why...
306 */
307 /*
308 * if (!post_regex
309 * && source < sentinel - 1
310 * && *source == U('.')
311 * && RJSMIN_IS_A_Z(*(source + 1)))
312 * rsreset = NULL;
313 */
314
315 goto cont; /* main loop */
316
317 case U('\\'):
318 if (source < sentinel) {
319 c = *source++;
320 *target++ = c;
321 if (c == U('\r') || c == U('\n'))
322 break; /* regex reset */
323 }
324 continue; /* regex loop */
325
326 case U('['):
327 /* regex CC loop */
328 while (source < sentinel) {
329 c = *source++;
330 *target++ = c;
331
332 if (RJSMIN_IS_REGEX_CC_DULL(c))
333 continue; /* regex CC loop */
334
335 switch (c) {
336 case U('\\'):
337 if (source < sentinel) {
338 c = *source++;
339 *target++ = c;
340 if (c == U('\r') || c == U('\n'))
341 break; /* regex reset */
342 }
343 continue; /* regex CC loop */
344
345 case U(']'):
346 goto cont_regex; /* regex loop */
347 }
348 }
349 break; /* regex reset */
350
351 }
352 break; /* regex reset */
353
354 cont_regex:
355 continue; /* regex loop */
356 }
357
358 /* regex reset */
359 target -= source - reset;
360 source = reset;
361 rsreset = NULL;
362 continue; /* main loop */
363 }
364 }
365 continue; /* main loop */ /* LCOV_EXCL_LINE */
366
367 /* Whitespace */
368 default:
369 skip_or_copy_ws:
370 /* remember if we've seen a newline, start with: no */
371 quote = U(' ');
372 --source;
373
374 /* space loop */
375 while (source < sentinel) {
376 c = *source++;
377 if (RJSMIN_IS_SPACE(c))
378 continue; /* space loop */
379
380 switch (c) {
381 case U('\r'): case U('\n'):
382 quote = U('\n');
383 continue; /* space loop */
384
385 /* Can only be a comment at this point
386 * (or ending prematurely) */
387 case U('/'):
388 if (source < sentinel) {
389 switch (*source) {
390
391 /* multiline comment */
392 case U('*'):
393 reset = source++;
394 /* copy bang comment, if requested */
395 if ( keep_bang_comments && source < sentinel
396 && *source == U('!')) {
397 if (!pctoken) {
398 /* Backtracking if ending prematurely */
399 pctoken = target;
400 pcreset = reset;
401 }
402
403 *target++ = U('/');
404 *target++ = U('*');
405 *target++ = *source++;
406
407 /* comment loop */
408 while (source < sentinel) {
409 c = *source++;
410 *target++ = c;
411 if (c == U('*') && source < sentinel
412 && *source == U('/')) {
413 *target++ = *source++;
414 reset = NULL;
415 break; /* continue space loop */
416 }
417 }
418 if (!reset)
419 continue; /* space loop */
420
421 /* comment reset */
422 target -= source - reset;
423 source = reset;
424 if (pcreset == reset) {
425 pctoken = NULL;
426 pcreset = NULL;
427 }
428 }
429
430 /* strip regular comment */
431 else {
432 while (source < sentinel) {
433 c = *source++;
434 if (c == U('*') && source < sentinel
435 && *source == U('/')) {
436 ++source;
437 reset = NULL;
438 break; /* continue space loop */
439 }
440 }
441 if (!reset)
442 continue; /* space loop */
443
444 /* comment reset: fallback to slash */
445 source = reset;
446 *target++ = U('/');
447 }
448 goto cont; /* main loop */
449
450 /* single line comment */
451 case U('/'):
452 ++source;
453
454 /* single line comment loop */
455 while (source < sentinel) {
456 c = *source++;
457 switch (c) {
458 case U('\n'):
459 break; /* continue space loop */
460
461 case U('\r'):
462 if (source < sentinel
463 && *source == U('\n'))
464 ++source;
465 break; /* continue space loop */
466
467 default:
468 continue; /* single line comment loop */
469 }
470 break; /* continue space loop */
471 }
472 quote = U('\n');
473 continue; /* space loop */
474 }
475 }
476 }
477
478 /* No more spacy character found */
479 --source;
480 break; /* end space loop */
481 }
482
483 /* Copy a space if needed */
484 if ((tstart < (pctoken ? pctoken : target) && source < sentinel)
485 && ((quote == U('\n')
486 && ((RJSMIN_IS_ID_LITERAL_CLOSE(*((pctoken ?
487 pctoken : target) - 1))
488 && RJSMIN_IS_ID_LITERAL_OPEN(*source))
489 || (post_regex
490 && RJSMIN_IS_POST_REGEX_OFF(*source)
491 && !(post_regex = 0))))
492 ||
493 (quote == U(' ') && !pctoken
494 && ((RJSMIN_IS_ID_LITERAL(*(target - 1))
495 && RJSMIN_IS_ID_LITERAL(*source))
496 || (source < sentinel
497 && ((*(target - 1) == U('+')
498 && *source == U('+'))
499 || (*(target - 1) == U('-')
500 && *source == U('-')))))))) {
501 *target++ = quote;
502 }
503
504 pcreset = NULL;
505 spaced = quote;
506 }
507
508 cont:
509 continue; /* main loop */
510 }
511 return (Py_ssize_t)(target - tstart);
512 }
513
514
515 PyDoc_STRVAR(rjsmin_jsmin__doc__,
516 "jsmin(script, keep_bang_comments=False)\n\
517 \n\
518 Minify javascript based on `jsmin.c by Douglas Crockford`_\\.\n\
519 \n\
520 Instead of parsing the stream char by char, it uses a regular\n\
521 expression approach which minifies the whole script with one big\n\
522 substitution regex.\n\
523 \n\
524 .. _jsmin.c by Douglas Crockford:\n\
525 http://www.crockford.com/javascript/jsmin.c\n\
526 \n\
527 :Note: This is a hand crafted C implementation built on the regex\n\
528 semantics.\n\
529 \n\
530 Parameters:\n\
531 script (str):\n\
532 Script to minify\n\
533 \n\
534 keep_bang_comments (bool):\n\
535 Keep comments starting with an exclamation mark? (``/*!...*/``)\n\
536 \n\
537 Returns:\n\
538 str: Minified script");
539
540 static PyObject *
541 rjsmin_jsmin(PyObject *self, PyObject *args, PyObject *kwds)
542 {
543 PyObject *script, *keep_bang_comments_ = NULL, *result;
544 static char *kwlist[] = {"script", "keep_bang_comments", NULL};
545 Py_ssize_t slength, length;
546 int keep_bang_comments;
547 #ifdef EXT2
548 int uni;
549 #endif
550 #ifdef EXT3
551 int bytes;
552 rchar *bytescript;
553 #endif
554
555 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
556 &script, &keep_bang_comments_))
557 LCOV_EXCL_LINE_RETURN(NULL);
558
559 if (!keep_bang_comments_)
560 keep_bang_comments = 0;
561 else {
562 keep_bang_comments = PyObject_IsTrue(keep_bang_comments_);
563 if (keep_bang_comments == -1)
564 return NULL;
565 }
566
567 #ifdef EXT2
568 if (PyUnicode_Check(script)) {
569 if (!(script = PyUnicode_AsUTF8String(script)))
570 LCOV_EXCL_LINE_RETURN(NULL);
571 uni = 1;
572 }
573 else if (!PyString_Check(script)) {
574 PyErr_SetString(PyExc_TypeError, "Unexpected type");
575 return NULL;
576 }
577 else {
578 if (!(script = PyObject_Str(script)))
579 LCOV_EXCL_LINE_RETURN(NULL);
580 uni = 0;
581 }
582 slength = PyString_GET_SIZE(script);
583
584 if (!(result = PyString_FromStringAndSize(NULL, slength))) {
585 LCOV_EXCL_START
586
587 Py_DECREF(script);
588 return NULL;
589
590 LCOV_EXCL_STOP
591 }
592 Py_BEGIN_ALLOW_THREADS
593 length = rjsmin((rchar *)PyString_AS_STRING(script),
594 (rchar *)PyString_AS_STRING(result),
595 slength, keep_bang_comments);
596 Py_END_ALLOW_THREADS
597
598 Py_DECREF(script);
599 if (length < 0) {
600 LCOV_EXCL_START
601
602 Py_DECREF(result);
603 return NULL;
604
605 LCOV_EXCL_STOP
606 }
607 if (length != slength && _PyString_Resize(&result, length) == -1)
608 LCOV_EXCL_LINE_RETURN(NULL);
609
610 if (uni) {
611 script = PyUnicode_DecodeUTF8(PyString_AS_STRING(result),
612 PyString_GET_SIZE(result), "strict");
613 Py_DECREF(result);
614 return script;
615 }
616
617 return result;
618
619 #else /* EXT3 */
620
621 if (PyUnicode_Check(script)) {
622 bytes = 0;
623 script = PyUnicode_AsUTF8String(script);
624 bytescript = (rchar *)PyBytes_AS_STRING(script);
625 slength = PyBytes_GET_SIZE(script);
626 }
627 else if (PyBytes_Check(script)) {
628 bytes = 1;
629 Py_INCREF(script);
630 bytescript = (rchar *)PyBytes_AS_STRING(script);
631 slength = PyBytes_GET_SIZE(script);
632 }
633 else if (PyByteArray_Check(script)) {
634 bytes = 2;
635 Py_INCREF(script);
636 bytescript = (rchar *)PyByteArray_AS_STRING(script);
637 slength = PyByteArray_GET_SIZE(script);
638 }
639 else {
640 PyErr_SetString(PyExc_TypeError, "Unexpected type");
641 return NULL;
642 }
643
644 if (!(result = PyBytes_FromStringAndSize(NULL, slength))) {
645 LCOV_EXCL_START
646
647 Py_DECREF(script);
648 return NULL;
649
650 LCOV_EXCL_STOP
651 }
652 Py_BEGIN_ALLOW_THREADS
653 length = rjsmin(bytescript, (rchar *)PyBytes_AS_STRING(result),
654 slength, keep_bang_comments);
655 Py_END_ALLOW_THREADS
656
657 Py_DECREF(script);
658 if (length < 0) {
659 LCOV_EXCL_START
660
661 Py_DECREF(result);
662 return NULL;
663
664 LCOV_EXCL_STOP
665 }
666
667 if (!bytes) {
668 script = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(result), length,
669 "strict");
670 Py_DECREF(result);
671 return script;
672 }
673 if (bytes == 1) {
674 if (length != slength) {
675 _PyBytes_Resize(&result, length);
676 }
677 return result;
678 }
679 /* bytes == 2: bytearray */
680 script = PyByteArray_FromStringAndSize(PyBytes_AS_STRING(result), length);
681 Py_DECREF(result);
682 return script;
683 #endif
684 }
685
686 /* ------------------------ BEGIN MODULE DEFINITION ------------------------ */
687
688 EXT_METHODS = {
689 {"jsmin",
690 EXT_CFUNC(rjsmin_jsmin), METH_VARARGS | METH_KEYWORDS,
691 rjsmin_jsmin__doc__},
692
693 {NULL} /* Sentinel */
694 };
695
696 PyDoc_STRVAR(EXT_DOCS_VAR,
697 "C implementation of rjsmin\n\
698 ==========================\n\
699 \n\
700 C implementation of rjsmin.");
701
702
703 EXT_DEFINE(EXT_MODULE_NAME, EXT_METHODS_VAR, EXT_DOCS_VAR);
704
705 EXT_INIT_FUNC {
706 PyObject *m;
707
708 /* Create the module and populate stuff */
709 if (!(m = EXT_CREATE(&EXT_DEFINE_VAR)))
710 EXT_INIT_ERROR(LCOV_EXCL_LINE(NULL));
711
712 EXT_ADD_UNICODE(m, "__author__", "Andr\xe9 Malo", "latin-1");
713 EXT_ADD_STRING(m, "__version__", STRINGIFY(EXT_VERSION));
714
715 EXT_INIT_RETURN(m);
716 }
717
718 /* ------------------------- END MODULE DEFINITION ------------------------- */