first attempt adjusting the C code to handle bang comments properly

This commit is contained in:
ndparker
2015-10-18 16:35:20 +02:00
parent 7cc5616a6d
commit 04fe02df90

View File

@@ -90,15 +90,18 @@ static Py_ssize_t
rjsmin(const rchar *source, rchar *target, Py_ssize_t length, rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
int keep_bang_comments) int keep_bang_comments)
{ {
const rchar *reset, *sentinel = source + length; const rchar *reset, *pcreset = NULL, *pctoken = NULL, *xtarget,
*sentinel = source + length;
rchar *tstart = target; rchar *tstart = target;
int post_regex = 0; int post_regex = 0;
rchar c, quote; rchar c, quote, spaced = U(' ');
while (source < sentinel) { while (source < sentinel) {
c = *source++; c = *source++;
if (RJSMIN_IS_DULL(c)) { if (RJSMIN_IS_DULL(c)) {
if (post_regex) post_regex = 0; if (post_regex) post_regex = 0;
if (pctoken) pctoken = NULL;
*target++ = c; *target++ = c;
continue; continue;
} }
@@ -107,6 +110,8 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
/* String */ /* String */
case U('\''): case U('"'): case U('\''): case U('"'):
if (post_regex) post_regex = 0; if (post_regex) post_regex = 0;
if (pctoken) pctoken = NULL;
reset = source; reset = source;
*target++ = quote = c; *target++ = quote = c;
while (source < sentinel) { while (source < sentinel) {
@@ -139,6 +144,8 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
case U('/'): case U('/'):
if (!(source < sentinel)) { if (!(source < sentinel)) {
if (post_regex) post_regex = 0; if (post_regex) post_regex = 0;
if (pctoken) pctoken = NULL;
*target++ = c; *target++ = c;
} }
else { else {
@@ -149,23 +156,28 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
default: default:
if ( target == tstart if ( target == tstart
|| RJSMIN_IS_PRE_REGEX_1(*(target - 1)) || RJSMIN_IS_PRE_REGEX_1(*((pctoken ? pctoken : target)
- 1))
|| ( || (
(target - tstart >= 6) (!pctoken || spaced == U(' '))
&& *(target - 1) == U('n') && (xtarget = pctoken ? pctoken : target)
&& *(target - 2) == U('r') && (xtarget - tstart >= 6)
&& *(target - 3) == U('u') && *(xtarget - 1) == U('n')
&& *(target - 4) == U('t') && *(xtarget - 2) == U('r')
&& *(target - 5) == U('e') && *(xtarget - 3) == U('u')
&& *(target - 6) == U('r') && *(xtarget - 4) == U('t')
&& *(xtarget - 5) == U('e')
&& *(xtarget - 6) == U('r')
&& ( && (
target - tstart == 6 xtarget - tstart == 6
|| !RJSMIN_IS_ID_LITERAL(*(target - 7)) || !RJSMIN_IS_ID_LITERAL(*(xtarget - 7))
) )
)) { )) {
/* Regex */ /* Regex */
if (post_regex) post_regex = 0; if (post_regex) post_regex = 0;
if (pctoken) pctoken = NULL;
reset = source; reset = source;
*target++ = U('/'); *target++ = U('/');
while (source < sentinel) { while (source < sentinel) {
@@ -216,6 +228,8 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
else { else {
/* Just a slash */ /* Just a slash */
if (post_regex) post_regex = 0; if (post_regex) post_regex = 0;
if (pctoken) pctoken = NULL;
*target++ = c; *target++ = c;
} }
continue; continue;
@@ -244,6 +258,11 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
/* copy bang comment, if requested */ /* copy bang comment, if requested */
if ( keep_bang_comments && source < sentinel if ( keep_bang_comments && source < sentinel
&& *source == U('!')) { && *source == U('!')) {
if (!pctoken) {
pctoken = target;
pcreset = reset;
}
*target++ = U('/'); *target++ = U('/');
*target++ = U('*'); *target++ = U('*');
*target++ = *source++; *target++ = *source++;
@@ -259,8 +278,14 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
} }
if (!reset) if (!reset)
continue; continue;
target -= source - reset; target -= source - reset;
source = reset; source = reset;
if (pcreset == reset) {
pctoken = NULL;
pcreset = NULL;
}
} }
/* strip regular comment */ /* strip regular comment */
else { else {
@@ -305,26 +330,29 @@ rjsmin(const rchar *source, rchar *target, Py_ssize_t length,
break; break;
} }
if ((tstart < target && source < sentinel) if ((tstart < (pctoken ? pctoken : target) && source < sentinel)
&& ((quote == U('\n') && ((quote == U('\n')
&& RJSMIN_IS_ID_LITERAL_CLOSE(*(target - 1)) && ((RJSMIN_IS_ID_LITERAL_CLOSE(*((pctoken ?
pctoken : target) - 1))
&& RJSMIN_IS_ID_LITERAL_OPEN(*source)) && RJSMIN_IS_ID_LITERAL_OPEN(*source))
|| || (post_regex
(quote == U('\n')
&& post_regex
&& RJSMIN_IS_POST_REGEX_OFF(*source) && RJSMIN_IS_POST_REGEX_OFF(*source)
&& !(post_regex = 0)) && !(post_regex = 0))))
|| ||
(quote == U(' ') (quote == U(' ') && !pctoken
&& ((RJSMIN_IS_ID_LITERAL(*(target - 1)) && ((RJSMIN_IS_ID_LITERAL(*(target - 1))
&& RJSMIN_IS_ID_LITERAL(*source)) && RJSMIN_IS_ID_LITERAL(*source))
|| (source < sentinel || (source < sentinel
&& ((*(target - 1) == U('+') && ((*(target - 1) == U('+')
&& *source == U('+')) && *source == U('+'))
|| (*(target - 1) == U('-') || (*(target - 1) == U('-')
&& *source == U('-')))))))) && *source == U('-')))))))) {
*target++ = quote; *target++ = quote;
} }
pcreset = NULL;
spaced = quote;
}
cont: cont:
continue; continue;
} }