FilePathScoreFunction.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2013 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @param {string} query
  33. */
  34. WebInspector.FilePathScoreFunction = function(query)
  35. {
  36. this._query = query;
  37. this._queryUpperCase = query.toUpperCase();
  38. this._score = null;
  39. this._sequence = null;
  40. this._dataUpperCase = "";
  41. this._fileNameIndex = 0;
  42. }
  43. /**
  44. * @param {string} query
  45. * @return {RegExp}
  46. */
  47. WebInspector.FilePathScoreFunction.filterRegex = function(query)
  48. {
  49. const toEscape = String.regexSpecialCharacters();
  50. var regexString = "";
  51. for (var i = 0; i < query.length; ++i) {
  52. var c = query.charAt(i);
  53. if (toEscape.indexOf(c) !== -1)
  54. c = "\\" + c;
  55. if (i)
  56. regexString += "[^" + c + "]*";
  57. regexString += c;
  58. }
  59. return new RegExp(regexString, "i");
  60. }
  61. WebInspector.FilePathScoreFunction.prototype = {
  62. /**
  63. * @param {string} data
  64. * @param {?Array.<Number>} matchIndexes
  65. * @return {number}
  66. */
  67. score: function(data, matchIndexes)
  68. {
  69. if (!data || !this._query)
  70. return 0;
  71. var n = this._query.length;
  72. var m = data.length;
  73. if (!this._score || this._score.length < n * m) {
  74. this._score = new Int32Array(n * m * 2);
  75. this._sequence = new Int32Array(n * m * 2);
  76. }
  77. var score = this._score;
  78. var sequence = this._sequence;
  79. this._dataUpperCase = data.toUpperCase();
  80. this._fileNameIndex = data.lastIndexOf("/");
  81. for (var i = 0; i < n; ++i) {
  82. for (var j = 0; j < m; ++j) {
  83. var skipCharScore = j === 0 ? 0 : score[i * m + j - 1];
  84. var prevCharScore = i === 0 || j === 0 ? 0 : score[(i - 1) * m + j - 1];
  85. var consecutiveMatch = i === 0 || j === 0 ? 0 : sequence[(i - 1) * m + j - 1];
  86. var pickCharScore = this._match(this._query, data, i, j, consecutiveMatch);
  87. if (pickCharScore && prevCharScore + pickCharScore > skipCharScore) {
  88. sequence[i * m + j] = consecutiveMatch + 1;
  89. score[i * m + j] = (prevCharScore + pickCharScore);
  90. } else {
  91. sequence[i * m + j] = 0;
  92. score[i * m + j] = skipCharScore;
  93. }
  94. }
  95. }
  96. if (matchIndexes)
  97. this._restoreMatchIndexes(sequence, n, m, matchIndexes);
  98. return score[n * m - 1];
  99. },
  100. /**
  101. * @param {string} data
  102. * @param {number} j
  103. * @return {boolean}
  104. */
  105. _testWordStart: function(data, j)
  106. {
  107. var prevChar = data.charAt(j - 1);
  108. return j === 0 || prevChar === "_" || prevChar === "-" || prevChar === "/" ||
  109. (data[j - 1] !== this._dataUpperCase[j - 1] && data[j] === this._dataUpperCase[j]);
  110. },
  111. /**
  112. * @param {Int32Array} sequence
  113. * @param {number} n
  114. * @param {number} m
  115. * @param {Array.<Number>} out
  116. */
  117. _restoreMatchIndexes: function(sequence, n, m, out)
  118. {
  119. var i = n - 1, j = m - 1;
  120. while (i >= 0 && j >= 0) {
  121. switch (sequence[i * m + j]) {
  122. case 0:
  123. --j;
  124. break;
  125. default:
  126. out.push(j);
  127. --i;
  128. --j;
  129. break;
  130. }
  131. }
  132. out.reverse();
  133. },
  134. /**
  135. * @param {string} query
  136. * @param {string} data
  137. * @param {number} i
  138. * @param {number} j
  139. * @return {number}
  140. */
  141. _singleCharScore: function(query, data, i, j)
  142. {
  143. var isWordStart = this._testWordStart(data, j);
  144. var isFileName = j > this._fileNameIndex;
  145. var isPathTokenStart = j === 0 || data[j - 1] === "/";
  146. var isCapsMatch = query[i] === data[j] && query[i] == this._queryUpperCase[i];
  147. var score = 10;
  148. if (isPathTokenStart)
  149. score += 4;
  150. if (isWordStart)
  151. score += 2;
  152. if (isCapsMatch)
  153. score += 6;
  154. if (isFileName)
  155. score += 4;
  156. // promote the case of making the whole match in the filename
  157. if (j === this._fileNameIndex + 1 && i === 0)
  158. score += 5;
  159. if (isFileName && isWordStart)
  160. score += 3;
  161. return score;
  162. },
  163. /**
  164. * @param {string} query
  165. * @param {string} data
  166. * @param {number} i
  167. * @param {number} j
  168. * @param {number} sequenceLength
  169. * @return {number}
  170. */
  171. _sequenceCharScore: function(query, data, i, j, sequenceLength)
  172. {
  173. var isFileName = j > this._fileNameIndex;
  174. var isPathTokenStart = j === 0 || data[j - 1] === "/";
  175. var score = 10;
  176. if (isFileName)
  177. score += 4;
  178. if (isPathTokenStart)
  179. score += 5;
  180. score += sequenceLength * 4;
  181. return score;
  182. },
  183. /**
  184. * @param {string} query
  185. * @param {string} data
  186. * @param {number} i
  187. * @param {number} j
  188. * @param {number} consecutiveMatch
  189. * @return {number}
  190. */
  191. _match: function(query, data, i, j, consecutiveMatch)
  192. {
  193. if (this._queryUpperCase[i] !== this._dataUpperCase[j])
  194. return 0;
  195. if (!consecutiveMatch)
  196. return this._singleCharScore(query, data, i, j);
  197. else
  198. return this._sequenceCharScore(query, data, i, j - consecutiveMatch, consecutiveMatch);
  199. },
  200. }