Açıklama Yok

quote-scanner.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (function() {
  2. var QuoteScanner = function QuoteScanner(data) {
  3. this.data = data;
  4. };
  5. var findQuoteEnd = function(data, matched, cursor, oldCursor) {
  6. var commentStartMark = '/*';
  7. var commentEndMark = '*/';
  8. var escapeMark = '\\';
  9. var blockEndMark = '}';
  10. var dataPrefix = data.substring(oldCursor, cursor);
  11. var commentEndedAt = dataPrefix.lastIndexOf(commentEndMark, cursor);
  12. var commentStartedAt = dataPrefix.lastIndexOf(commentStartMark, cursor);
  13. var commentStarted = false;
  14. if (commentEndedAt >= cursor && commentStartedAt > -1)
  15. commentStarted = true;
  16. if (commentStartedAt < cursor && commentStartedAt > commentEndedAt)
  17. commentStarted = true;
  18. if (commentStarted) {
  19. var commentEndsAt = data.indexOf(commentEndMark, cursor);
  20. if (commentEndsAt > -1)
  21. return commentEndsAt;
  22. commentEndsAt = data.indexOf(blockEndMark, cursor);
  23. return commentEndsAt > -1 ? commentEndsAt - 1 : data.length;
  24. }
  25. while (true) {
  26. if (data[cursor] === undefined)
  27. break;
  28. if (data[cursor] == matched && (data[cursor - 1] != escapeMark || data[cursor - 2] == escapeMark))
  29. break;
  30. cursor++;
  31. }
  32. return cursor;
  33. };
  34. QuoteScanner.prototype.each = function(callback) {
  35. var data = this.data;
  36. var tempData = [];
  37. var nextStart = 0;
  38. var nextEnd = 0;
  39. var cursor = 0;
  40. var matchedMark = null;
  41. var singleMark = '\'';
  42. var doubleMark = '"';
  43. var dataLength = data.length;
  44. for (; nextEnd < data.length;) {
  45. var nextStartSingle = data.indexOf(singleMark, nextEnd + 1);
  46. var nextStartDouble = data.indexOf(doubleMark, nextEnd + 1);
  47. if (nextStartSingle == -1)
  48. nextStartSingle = dataLength;
  49. if (nextStartDouble == -1)
  50. nextStartDouble = dataLength;
  51. if (nextStartSingle < nextStartDouble) {
  52. nextStart = nextStartSingle;
  53. matchedMark = singleMark;
  54. } else {
  55. nextStart = nextStartDouble;
  56. matchedMark = doubleMark;
  57. }
  58. if (nextStart == -1)
  59. break;
  60. nextEnd = findQuoteEnd(data, matchedMark, nextStart + 1, cursor);
  61. if (nextEnd == -1)
  62. break;
  63. var text = data.substring(nextStart, nextEnd + 1);
  64. tempData.push(data.substring(cursor, nextStart));
  65. if (text.length > 0)
  66. callback(text, tempData, nextStart);
  67. cursor = nextEnd + 1;
  68. }
  69. return tempData.length > 0 ?
  70. tempData.join('') + data.substring(cursor, data.length) :
  71. data;
  72. };
  73. module.exports = QuoteScanner;
  74. })();