Adds faster quote matching in QuoteScanner.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 5 Jul 2014 13:25:37 +0000 (14:25 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 5 Jul 2014 13:59:58 +0000 (14:59 +0100)
History.md
lib/text/quote-scanner.js

index a9c309a..3ad3914 100644 (file)
@@ -1,6 +1,7 @@
 [2.2.6 / 2014-xx-xx](https://github.com/GoalSmashers/clean-css/compare/v2.2.5...v2.2.6)
 ==================
 
+* Adds faster quote matching in QuoteScanner.
 * Improves QuoteScanner to handle comments correctly.
 * Fixed issue [#308](https://github.com/GoalSmashers/clean-css/issues/308) - parsing comments in quoted urls.
 * Fixed issue [#311](https://github.com/GoalSmashers/clean-css/issues/311) - leading/trailing decimal points.
index 5511753..83dc49f 100644 (file)
@@ -3,31 +3,26 @@
     this.data = data;
   };
 
-  var findCommentEnd = function(data, matched, start) {
+  var findCommentEnd = function(data, matched, cursor) {
     var commentStartMark = '/*';
     var commentEndMark = '*/';
-    var end = start;
-    while (true) {
-      var from = end;
-
-      end = data.indexOf(matched, from);
-      var commentStart = data.indexOf(commentStartMark, from);
-      var commentEnd = data.indexOf(commentEndMark, from);
+    var escapeMark = '\\';
+    var commentStarted = false;
 
-      if (end > -1 && commentEnd > -1 && commentEnd < end && (commentStart == -1 || commentStart > commentEnd)) {
-        end = commentEnd + 1;
+    while (true) {
+      if (data[cursor] === undefined)
         break;
-      }
-
-      if (end > -1 && data[end - 1] == '\\') {
-        end += 1;
-        continue;
-      } else {
+      if (!commentStarted && data[cursor] == commentEndMark[1] && data[cursor - 1] == commentEndMark[0])
         break;
-      }
+      if (data[cursor] == matched && data[cursor - 1] != escapeMark)
+        break;
+      if (data[cursor] == commentStartMark[1] && data[cursor - 1] == commentStartMark[0])
+        commentStarted = true;
+
+      cursor++;
     }
 
-    return end;
+    return cursor;
   };
 
   QuoteScanner.prototype.each = function(callback) {
@@ -67,7 +62,8 @@
 
       var text = data.substring(nextStart, nextEnd + 1);
       tempData.push(data.substring(cursor, nextStart));
-      callback(text, tempData, nextStart);
+      if (text.length > 0)
+        callback(text, tempData, nextStart);
 
       cursor = nextEnd + 1;
     }