utilities.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. /*
  2. * Copyright (C) 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2012 Google Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @param {Object} obj
  31. * @return {boolean}
  32. */
  33. Object.isEmpty = function(obj)
  34. {
  35. for (var i in obj)
  36. return false;
  37. return true;
  38. }
  39. /**
  40. * @param {!Object.<string,T>} obj
  41. * @return {!Array.<T>}
  42. * @template T
  43. */
  44. Object.values = function(obj)
  45. {
  46. var result = Object.keys(obj);
  47. var length = result.length;
  48. for (var i = 0; i < length; ++i)
  49. result[i] = obj[result[i]];
  50. return result;
  51. }
  52. /**
  53. * @param {string} string
  54. * @return {!Array.<number>}
  55. */
  56. String.prototype.findAll = function(string)
  57. {
  58. var matches = [];
  59. var i = this.indexOf(string);
  60. while (i !== -1) {
  61. matches.push(i);
  62. i = this.indexOf(string, i + string.length);
  63. }
  64. return matches;
  65. }
  66. /**
  67. * @return {!Array.<number>}
  68. */
  69. String.prototype.lineEndings = function()
  70. {
  71. if (!this._lineEndings) {
  72. this._lineEndings = this.findAll("\n");
  73. this._lineEndings.push(this.length);
  74. }
  75. return this._lineEndings;
  76. }
  77. /**
  78. * @param {string} chars
  79. * @return {string}
  80. */
  81. String.prototype.escapeCharacters = function(chars)
  82. {
  83. var foundChar = false;
  84. for (var i = 0; i < chars.length; ++i) {
  85. if (this.indexOf(chars.charAt(i)) !== -1) {
  86. foundChar = true;
  87. break;
  88. }
  89. }
  90. if (!foundChar)
  91. return String(this);
  92. var result = "";
  93. for (var i = 0; i < this.length; ++i) {
  94. if (chars.indexOf(this.charAt(i)) !== -1)
  95. result += "\\";
  96. result += this.charAt(i);
  97. }
  98. return result;
  99. }
  100. /**
  101. * @return {string}
  102. */
  103. String.regexSpecialCharacters = function()
  104. {
  105. return "^[]{}()\\.$*+?|-,";
  106. }
  107. /**
  108. * @return {string}
  109. */
  110. String.prototype.escapeForRegExp = function()
  111. {
  112. return this.escapeCharacters(String.regexSpecialCharacters());
  113. }
  114. /**
  115. * @return {string}
  116. */
  117. String.prototype.escapeHTML = function()
  118. {
  119. return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"); //" doublequotes just for editor
  120. }
  121. /**
  122. * @return {string}
  123. */
  124. String.prototype.collapseWhitespace = function()
  125. {
  126. return this.replace(/[\s\xA0]+/g, " ");
  127. }
  128. /**
  129. * @param {number} maxLength
  130. * @return {string}
  131. */
  132. String.prototype.trimMiddle = function(maxLength)
  133. {
  134. if (this.length <= maxLength)
  135. return String(this);
  136. var leftHalf = maxLength >> 1;
  137. var rightHalf = maxLength - leftHalf - 1;
  138. return this.substr(0, leftHalf) + "\u2026" + this.substr(this.length - rightHalf, rightHalf);
  139. }
  140. /**
  141. * @param {number} maxLength
  142. * @return {string}
  143. */
  144. String.prototype.trimEnd = function(maxLength)
  145. {
  146. if (this.length <= maxLength)
  147. return String(this);
  148. return this.substr(0, maxLength - 1) + "\u2026";
  149. }
  150. /**
  151. * @param {?string=} baseURLDomain
  152. * @return {string}
  153. */
  154. String.prototype.trimURL = function(baseURLDomain)
  155. {
  156. var result = this.replace(/^(https|http|file):\/\//i, "");
  157. if (baseURLDomain)
  158. result = result.replace(new RegExp("^" + baseURLDomain.escapeForRegExp(), "i"), "");
  159. return result;
  160. }
  161. /**
  162. * @return {string}
  163. */
  164. String.prototype.toTitleCase = function()
  165. {
  166. return this.substring(0, 1).toUpperCase() + this.substring(1);
  167. }
  168. /**
  169. * @param {string} other
  170. * @return {number}
  171. */
  172. String.prototype.compareTo = function(other)
  173. {
  174. if (this > other)
  175. return 1;
  176. if (this < other)
  177. return -1;
  178. return 0;
  179. }
  180. /**
  181. * @param {string} href
  182. * @return {?string}
  183. */
  184. function sanitizeHref(href)
  185. {
  186. return href && href.trim().toLowerCase().startsWith("javascript:") ? null : href;
  187. }
  188. /**
  189. * @return {string}
  190. */
  191. String.prototype.removeURLFragment = function()
  192. {
  193. var fragmentIndex = this.indexOf("#");
  194. if (fragmentIndex == -1)
  195. fragmentIndex = this.length;
  196. return this.substring(0, fragmentIndex);
  197. }
  198. /**
  199. * @return {boolean}
  200. */
  201. String.prototype.startsWith = function(substring)
  202. {
  203. return !this.lastIndexOf(substring, 0);
  204. }
  205. /**
  206. * @return {boolean}
  207. */
  208. String.prototype.endsWith = function(substring)
  209. {
  210. return this.indexOf(substring, this.length - substring.length) !== -1;
  211. }
  212. /**
  213. * @param {string} a
  214. * @param {string} b
  215. * @return {number}
  216. */
  217. String.naturalOrderComparator = function(a, b)
  218. {
  219. var chunk = /^\d+|^\D+/;
  220. var chunka, chunkb, anum, bnum;
  221. while (1) {
  222. if (a) {
  223. if (!b)
  224. return 1;
  225. } else {
  226. if (b)
  227. return -1;
  228. else
  229. return 0;
  230. }
  231. chunka = a.match(chunk)[0];
  232. chunkb = b.match(chunk)[0];
  233. anum = !isNaN(chunka);
  234. bnum = !isNaN(chunkb);
  235. if (anum && !bnum)
  236. return -1;
  237. if (bnum && !anum)
  238. return 1;
  239. if (anum && bnum) {
  240. var diff = chunka - chunkb;
  241. if (diff)
  242. return diff;
  243. if (chunka.length !== chunkb.length) {
  244. if (!+chunka && !+chunkb) // chunks are strings of all 0s (special case)
  245. return chunka.length - chunkb.length;
  246. else
  247. return chunkb.length - chunka.length;
  248. }
  249. } else if (chunka !== chunkb)
  250. return (chunka < chunkb) ? -1 : 1;
  251. a = a.substring(chunka.length);
  252. b = b.substring(chunkb.length);
  253. }
  254. }
  255. /**
  256. * @param {number} num
  257. * @param {number} min
  258. * @param {number} max
  259. * @return {number}
  260. */
  261. Number.constrain = function(num, min, max)
  262. {
  263. if (num < min)
  264. num = min;
  265. else if (num > max)
  266. num = max;
  267. return num;
  268. }
  269. /**
  270. * @param {number} a
  271. * @param {number} b
  272. * @return {number}
  273. */
  274. Number.gcd = function(a, b)
  275. {
  276. if (b === 0)
  277. return a;
  278. else
  279. return Number.gcd(b, a % b);
  280. }
  281. /**
  282. * @param {string} value
  283. * @return {string}
  284. */
  285. Number.toFixedIfFloating = function(value)
  286. {
  287. if (!value || isNaN(value))
  288. return value;
  289. var number = Number(value);
  290. return number % 1 ? number.toFixed(3) : String(number);
  291. }
  292. /**
  293. * @return {string}
  294. */
  295. Date.prototype.toISO8601Compact = function()
  296. {
  297. /**
  298. * @param {number} x
  299. * @return {string}
  300. */
  301. function leadZero(x)
  302. {
  303. return (x > 9 ? "" : "0") + x;
  304. }
  305. return this.getFullYear() +
  306. leadZero(this.getMonth() + 1) +
  307. leadZero(this.getDate()) + "T" +
  308. leadZero(this.getHours()) +
  309. leadZero(this.getMinutes()) +
  310. leadZero(this.getSeconds());
  311. }
  312. Object.defineProperty(Array.prototype, "remove",
  313. {
  314. /**
  315. * @param {T} value
  316. * @param {boolean=} onlyFirst
  317. * @this {Array.<T>}
  318. * @template T
  319. */
  320. value: function(value, onlyFirst)
  321. {
  322. if (onlyFirst) {
  323. var index = this.indexOf(value);
  324. if (index !== -1)
  325. this.splice(index, 1);
  326. return;
  327. }
  328. var length = this.length;
  329. for (var i = 0; i < length; ++i) {
  330. if (this[i] === value)
  331. this.splice(i, 1);
  332. }
  333. }
  334. });
  335. Object.defineProperty(Array.prototype, "keySet",
  336. {
  337. /**
  338. * @return {!Object.<string, boolean>}
  339. * @this {Array.<*>}
  340. */
  341. value: function()
  342. {
  343. var keys = {};
  344. for (var i = 0; i < this.length; ++i)
  345. keys[this[i]] = true;
  346. return keys;
  347. }
  348. });
  349. Object.defineProperty(Array.prototype, "rotate",
  350. {
  351. /**
  352. * @param {number} index
  353. * @return {!Array.<T>}
  354. * @this {Array.<T>}
  355. * @template T
  356. */
  357. value: function(index)
  358. {
  359. var result = [];
  360. for (var i = index; i < index + this.length; ++i)
  361. result.push(this[i % this.length]);
  362. return result;
  363. }
  364. });
  365. Object.defineProperty(Uint32Array.prototype, "sort", {
  366. value: Array.prototype.sort
  367. });
  368. (function() {
  369. var partition = {
  370. /**
  371. * @this {Array.<number>}
  372. * @param {function(number, number): number} comparator
  373. * @param {number} left
  374. * @param {number} right
  375. * @param {number} pivotIndex
  376. */
  377. value: function(comparator, left, right, pivotIndex)
  378. {
  379. function swap(array, i1, i2)
  380. {
  381. var temp = array[i1];
  382. array[i1] = array[i2];
  383. array[i2] = temp;
  384. }
  385. var pivotValue = this[pivotIndex];
  386. swap(this, right, pivotIndex);
  387. var storeIndex = left;
  388. for (var i = left; i < right; ++i) {
  389. if (comparator(this[i], pivotValue) < 0) {
  390. swap(this, storeIndex, i);
  391. ++storeIndex;
  392. }
  393. }
  394. swap(this, right, storeIndex);
  395. return storeIndex;
  396. }
  397. };
  398. Object.defineProperty(Array.prototype, "partition", partition);
  399. Object.defineProperty(Uint32Array.prototype, "partition", partition);
  400. var sortRange = {
  401. /**
  402. * @param {function(number, number): number} comparator
  403. * @param {number} leftBound
  404. * @param {number} rightBound
  405. * @param {number} k
  406. * @return {!Array.<number>}
  407. * @this {Array.<number>}
  408. */
  409. value: function(comparator, leftBound, rightBound, k)
  410. {
  411. function quickSortFirstK(array, comparator, left, right, k)
  412. {
  413. if (right <= left)
  414. return;
  415. var pivotIndex = Math.floor(Math.random() * (right - left)) + left;
  416. var pivotNewIndex = array.partition(comparator, left, right, pivotIndex);
  417. quickSortFirstK(array, comparator, left, pivotNewIndex - 1, k);
  418. if (pivotNewIndex < left + k - 1)
  419. quickSortFirstK(array, comparator, pivotNewIndex + 1, right, left + k - 1 - pivotNewIndex);
  420. }
  421. if (leftBound === 0 && rightBound === (this.length - 1) && k >= this.length)
  422. this.sort(comparator);
  423. else
  424. quickSortFirstK(this, comparator, leftBound, rightBound, k);
  425. return this;
  426. }
  427. }
  428. Object.defineProperty(Array.prototype, "sortRange", sortRange);
  429. Object.defineProperty(Uint32Array.prototype, "sortRange", sortRange);
  430. })();
  431. Object.defineProperty(Array.prototype, "qselect",
  432. {
  433. /**
  434. * @param {number} k
  435. * @param {function(number, number): number=} comparator
  436. * @return {number|undefined}
  437. * @this {Array.<number>}
  438. */
  439. value: function(k, comparator)
  440. {
  441. if (k < 0 || k >= this.length)
  442. return;
  443. if (!comparator)
  444. comparator = function(a, b) { return a - b; }
  445. var low = 0;
  446. var high = this.length - 1;
  447. for (;;) {
  448. var pivotPosition = this.partition(comparator, low, high, Math.floor((high + low) / 2));
  449. if (pivotPosition === k)
  450. return this[k];
  451. else if (pivotPosition > k)
  452. high = pivotPosition - 1;
  453. else
  454. low = pivotPosition + 1;
  455. }
  456. }
  457. });
  458. Object.defineProperty(Array.prototype, "lowerBound",
  459. {
  460. /**
  461. * Return index of the leftmost element that is equal or greater
  462. * than the specimen object. If there's no such element (i.e. all
  463. * elements are smaller than the specimen) returns array.length.
  464. * The function works for sorted array.
  465. *
  466. * @param {T} object
  467. * @param {function(T,S):number=} comparator
  468. * @return {number}
  469. * @this {Array.<S>}
  470. * @template T,S
  471. */
  472. value: function(object, comparator)
  473. {
  474. function defaultComparator(a, b)
  475. {
  476. return a < b ? -1 : (a > b ? 1 : 0);
  477. }
  478. comparator = comparator || defaultComparator;
  479. var l = 0;
  480. var r = this.length;
  481. while (l < r) {
  482. var m = (l + r) >> 1;
  483. if (comparator(object, this[m]) > 0)
  484. l = m + 1;
  485. else
  486. r = m;
  487. }
  488. return r;
  489. }
  490. });
  491. Object.defineProperty(Array.prototype, "upperBound",
  492. {
  493. /**
  494. * Return index of the leftmost element that is greater
  495. * than the specimen object. If there's no such element (i.e. all
  496. * elements are smaller than the specimen) returns array.length.
  497. * The function works for sorted array.
  498. *
  499. * @param {T} object
  500. * @param {function(T,S):number=} comparator
  501. * @return {number}
  502. * @this {Array.<S>}
  503. * @template T,S
  504. */
  505. value: function(object, comparator)
  506. {
  507. function defaultComparator(a, b)
  508. {
  509. return a < b ? -1 : (a > b ? 1 : 0);
  510. }
  511. comparator = comparator || defaultComparator;
  512. var l = 0;
  513. var r = this.length;
  514. while (l < r) {
  515. var m = (l + r) >> 1;
  516. if (comparator(object, this[m]) >= 0)
  517. l = m + 1;
  518. else
  519. r = m;
  520. }
  521. return r;
  522. }
  523. });
  524. Object.defineProperty(Array.prototype, "binaryIndexOf",
  525. {
  526. /**
  527. * @param {T} value
  528. * @param {function(T,S):number} comparator
  529. * @return {number}
  530. * @this {Array.<S>}
  531. * @template T,S
  532. */
  533. value: function(value, comparator)
  534. {
  535. var index = this.lowerBound(value, comparator);
  536. return index < this.length && comparator(value, this[index]) === 0 ? index : -1;
  537. }
  538. });
  539. Object.defineProperty(Array.prototype, "select",
  540. {
  541. /**
  542. * @param {string} field
  543. * @return {!Array.<T>}
  544. * @this {Array.<Object.<string,T>>}
  545. * @template T
  546. */
  547. value: function(field)
  548. {
  549. var result = new Array(this.length);
  550. for (var i = 0; i < this.length; ++i)
  551. result[i] = this[i][field];
  552. return result;
  553. }
  554. });
  555. Object.defineProperty(Array.prototype, "peekLast",
  556. {
  557. /**
  558. * @return {T|undefined}
  559. * @this {Array.<T>}
  560. * @template T
  561. */
  562. value: function()
  563. {
  564. return this[this.length - 1];
  565. }
  566. });
  567. /**
  568. * @param {T} object
  569. * @param {Array.<S>} list
  570. * @param {function(T,S):number=} comparator
  571. * @param {boolean=} insertionIndexAfter
  572. * @return {number}
  573. * @template T,S
  574. */
  575. function insertionIndexForObjectInListSortedByFunction(object, list, comparator, insertionIndexAfter)
  576. {
  577. if (insertionIndexAfter)
  578. return list.upperBound(object, comparator);
  579. else
  580. return list.lowerBound(object, comparator);
  581. }
  582. /**
  583. * @param {string} format
  584. * @param {...*} var_arg
  585. * @return {string}
  586. */
  587. String.sprintf = function(format, var_arg)
  588. {
  589. return String.vsprintf(format, Array.prototype.slice.call(arguments, 1));
  590. }
  591. String.tokenizeFormatString = function(format, formatters)
  592. {
  593. var tokens = [];
  594. var substitutionIndex = 0;
  595. function addStringToken(str)
  596. {
  597. tokens.push({ type: "string", value: str });
  598. }
  599. function addSpecifierToken(specifier, precision, substitutionIndex)
  600. {
  601. tokens.push({ type: "specifier", specifier: specifier, precision: precision, substitutionIndex: substitutionIndex });
  602. }
  603. function isDigit(c)
  604. {
  605. return !!/[0-9]/.exec(c);
  606. }
  607. var index = 0;
  608. for (var precentIndex = format.indexOf("%", index); precentIndex !== -1; precentIndex = format.indexOf("%", index)) {
  609. addStringToken(format.substring(index, precentIndex));
  610. index = precentIndex + 1;
  611. if (isDigit(format[index])) {
  612. // The first character is a number, it might be a substitution index.
  613. var number = parseInt(format.substring(index), 10);
  614. while (isDigit(format[index]))
  615. ++index;
  616. // If the number is greater than zero and ends with a "$",
  617. // then this is a substitution index.
  618. if (number > 0 && format[index] === "$") {
  619. substitutionIndex = (number - 1);
  620. ++index;
  621. }
  622. }
  623. var precision = -1;
  624. if (format[index] === ".") {
  625. // This is a precision specifier. If no digit follows the ".",
  626. // then the precision should be zero.
  627. ++index;
  628. precision = parseInt(format.substring(index), 10);
  629. if (isNaN(precision))
  630. precision = 0;
  631. while (isDigit(format[index]))
  632. ++index;
  633. }
  634. if (!(format[index] in formatters)) {
  635. addStringToken(format.substring(precentIndex, index + 1));
  636. ++index;
  637. continue;
  638. }
  639. addSpecifierToken(format[index], precision, substitutionIndex);
  640. ++substitutionIndex;
  641. ++index;
  642. }
  643. addStringToken(format.substring(index));
  644. return tokens;
  645. }
  646. String.standardFormatters = {
  647. d: function(substitution)
  648. {
  649. return !isNaN(substitution) ? substitution : 0;
  650. },
  651. f: function(substitution, token)
  652. {
  653. if (substitution && token.precision > -1)
  654. substitution = substitution.toFixed(token.precision);
  655. return !isNaN(substitution) ? substitution : (token.precision > -1 ? Number(0).toFixed(token.precision) : 0);
  656. },
  657. s: function(substitution)
  658. {
  659. return substitution;
  660. }
  661. }
  662. /**
  663. * @param {string} format
  664. * @param {Array.<*>} substitutions
  665. * @return {string}
  666. */
  667. String.vsprintf = function(format, substitutions)
  668. {
  669. return String.format(format, substitutions, String.standardFormatters, "", function(a, b) { return a + b; }).formattedResult;
  670. }
  671. String.format = function(format, substitutions, formatters, initialValue, append)
  672. {
  673. if (!format || !substitutions || !substitutions.length)
  674. return { formattedResult: append(initialValue, format), unusedSubstitutions: substitutions };
  675. function prettyFunctionName()
  676. {
  677. return "String.format(\"" + format + "\", \"" + substitutions.join("\", \"") + "\")";
  678. }
  679. function warn(msg)
  680. {
  681. console.warn(prettyFunctionName() + ": " + msg);
  682. }
  683. function error(msg)
  684. {
  685. console.error(prettyFunctionName() + ": " + msg);
  686. }
  687. var result = initialValue;
  688. var tokens = String.tokenizeFormatString(format, formatters);
  689. var usedSubstitutionIndexes = {};
  690. for (var i = 0; i < tokens.length; ++i) {
  691. var token = tokens[i];
  692. if (token.type === "string") {
  693. result = append(result, token.value);
  694. continue;
  695. }
  696. if (token.type !== "specifier") {
  697. error("Unknown token type \"" + token.type + "\" found.");
  698. continue;
  699. }
  700. if (token.substitutionIndex >= substitutions.length) {
  701. // If there are not enough substitutions for the current substitutionIndex
  702. // just output the format specifier literally and move on.
  703. error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (token.substitutionIndex + 1) + ", so substitution was skipped.");
  704. result = append(result, "%" + (token.precision > -1 ? token.precision : "") + token.specifier);
  705. continue;
  706. }
  707. usedSubstitutionIndexes[token.substitutionIndex] = true;
  708. if (!(token.specifier in formatters)) {
  709. // Encountered an unsupported format character, treat as a string.
  710. warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string.");
  711. result = append(result, substitutions[token.substitutionIndex]);
  712. continue;
  713. }
  714. result = append(result, formatters[token.specifier](substitutions[token.substitutionIndex], token));
  715. }
  716. var unusedSubstitutions = [];
  717. for (var i = 0; i < substitutions.length; ++i) {
  718. if (i in usedSubstitutionIndexes)
  719. continue;
  720. unusedSubstitutions.push(substitutions[i]);
  721. }
  722. return { formattedResult: result, unusedSubstitutions: unusedSubstitutions };
  723. }
  724. /**
  725. * @param {string} query
  726. * @param {boolean} caseSensitive
  727. * @param {boolean} isRegex
  728. * @return {RegExp}
  729. */
  730. function createSearchRegex(query, caseSensitive, isRegex)
  731. {
  732. var regexFlags = caseSensitive ? "g" : "gi";
  733. var regexObject;
  734. if (isRegex) {
  735. try {
  736. regexObject = new RegExp(query, regexFlags);
  737. } catch (e) {
  738. // Silent catch.
  739. }
  740. }
  741. if (!regexObject)
  742. regexObject = createPlainTextSearchRegex(query, regexFlags);
  743. return regexObject;
  744. }
  745. /**
  746. * @param {string} query
  747. * @param {string=} flags
  748. * @return {!RegExp}
  749. */
  750. function createPlainTextSearchRegex(query, flags)
  751. {
  752. // This should be kept the same as the one in ContentSearchUtils.cpp.
  753. var regexSpecialCharacters = String.regexSpecialCharacters();
  754. var regex = "";
  755. for (var i = 0; i < query.length; ++i) {
  756. var c = query.charAt(i);
  757. if (regexSpecialCharacters.indexOf(c) != -1)
  758. regex += "\\";
  759. regex += c;
  760. }
  761. return new RegExp(regex, flags || "");
  762. }
  763. /**
  764. * @param {RegExp} regex
  765. * @param {string} content
  766. * @return {number}
  767. */
  768. function countRegexMatches(regex, content)
  769. {
  770. var text = content;
  771. var result = 0;
  772. var match;
  773. while (text && (match = regex.exec(text))) {
  774. if (match[0].length > 0)
  775. ++result;
  776. text = text.substring(match.index + 1);
  777. }
  778. return result;
  779. }
  780. /**
  781. * @param {number} value
  782. * @param {number} symbolsCount
  783. * @return {string}
  784. */
  785. function numberToStringWithSpacesPadding(value, symbolsCount)
  786. {
  787. var numberString = value.toString();
  788. var paddingLength = Math.max(0, symbolsCount - numberString.length);
  789. var paddingString = Array(paddingLength + 1).join("\u00a0");
  790. return paddingString + numberString;
  791. }
  792. /**
  793. * @return {string}
  794. */
  795. var createObjectIdentifier = function()
  796. {
  797. // It has to be string for better performance.
  798. return "_" + ++createObjectIdentifier._last;
  799. }
  800. createObjectIdentifier._last = 0;
  801. /**
  802. * @constructor
  803. * @template T
  804. */
  805. var Set = function()
  806. {
  807. /** @type {!Object.<string, !T>} */
  808. this._set = {};
  809. this._size = 0;
  810. }
  811. Set.prototype = {
  812. /**
  813. * @param {!T} item
  814. */
  815. add: function(item)
  816. {
  817. var objectIdentifier = item.__identifier;
  818. if (!objectIdentifier) {
  819. objectIdentifier = createObjectIdentifier();
  820. item.__identifier = objectIdentifier;
  821. }
  822. if (!this._set[objectIdentifier])
  823. ++this._size;
  824. this._set[objectIdentifier] = item;
  825. },
  826. /**
  827. * @param {!T} item
  828. * @return {boolean}
  829. */
  830. remove: function(item)
  831. {
  832. if (this._set[item.__identifier]) {
  833. --this._size;
  834. delete this._set[item.__identifier];
  835. return true;
  836. }
  837. return false;
  838. },
  839. /**
  840. * @return {!Array.<!T>}
  841. */
  842. items: function()
  843. {
  844. var result = new Array(this._size);
  845. var i = 0;
  846. for (var objectIdentifier in this._set)
  847. result[i++] = this._set[objectIdentifier];
  848. return result;
  849. },
  850. /**
  851. * @param {!T} item
  852. * @return {boolean}
  853. */
  854. hasItem: function(item)
  855. {
  856. return !!this._set[item.__identifier];
  857. },
  858. /**
  859. * @return {number}
  860. */
  861. size: function()
  862. {
  863. return this._size;
  864. },
  865. clear: function()
  866. {
  867. this._set = {};
  868. this._size = 0;
  869. }
  870. }
  871. /**
  872. * @constructor
  873. * @template K,V
  874. */
  875. var Map = function()
  876. {
  877. /** @type {!Object.<string, !Array.<K|V>>} */
  878. this._map = {};
  879. this._size = 0;
  880. }
  881. Map.prototype = {
  882. /**
  883. * @param {!K} key
  884. * @param {V=} value
  885. */
  886. put: function(key, value)
  887. {
  888. var objectIdentifier = key.__identifier;
  889. if (!objectIdentifier) {
  890. objectIdentifier = createObjectIdentifier();
  891. key.__identifier = objectIdentifier;
  892. }
  893. if (!this._map[objectIdentifier])
  894. ++this._size;
  895. this._map[objectIdentifier] = [key, value];
  896. },
  897. /**
  898. * @param {!K} key
  899. */
  900. remove: function(key)
  901. {
  902. var result = this._map[key.__identifier];
  903. if (!result)
  904. return undefined;
  905. --this._size;
  906. delete this._map[key.__identifier];
  907. return result[1];
  908. },
  909. /**
  910. * @return {!Array.<!K>}
  911. */
  912. keys: function()
  913. {
  914. return this._list(0);
  915. },
  916. /**
  917. * @return {!Array.<V>}
  918. */
  919. values: function()
  920. {
  921. return this._list(1);
  922. },
  923. /**
  924. * @param {number} index
  925. * @return {!Array.<K|V>}
  926. */
  927. _list: function(index)
  928. {
  929. var result = new Array(this._size);
  930. var i = 0;
  931. for (var objectIdentifier in this._map)
  932. result[i++] = this._map[objectIdentifier][index];
  933. return result;
  934. },
  935. /**
  936. * @param {!K} key
  937. * @return {V|undefined}
  938. */
  939. get: function(key)
  940. {
  941. var entry = this._map[key.__identifier];
  942. return entry ? entry[1] : undefined;
  943. },
  944. /**
  945. * @param {!K} key
  946. * @return {boolean}
  947. */
  948. contains: function(key)
  949. {
  950. var entry = this._map[key.__identifier];
  951. return !!entry;
  952. },
  953. /**
  954. * @return {number}
  955. */
  956. size: function()
  957. {
  958. return this._size;
  959. },
  960. clear: function()
  961. {
  962. this._map = {};
  963. this._size = 0;
  964. }
  965. }
  966. /**
  967. * @constructor
  968. * @template T
  969. */
  970. var StringMap = function()
  971. {
  972. /** @type {!Object.<string, T>} */
  973. this._map = {};
  974. this._size = 0;
  975. }
  976. StringMap.prototype = {
  977. /**
  978. * @param {string} key
  979. * @param {T} value
  980. */
  981. put: function(key, value)
  982. {
  983. if (key === "__proto__") {
  984. if (!this._hasProtoKey) {
  985. ++this._size;
  986. this._hasProtoKey = true;
  987. }
  988. /** @type {T} */
  989. this._protoValue = value;
  990. return;
  991. }
  992. if (!Object.prototype.hasOwnProperty.call(this._map, key))
  993. ++this._size;
  994. this._map[key] = value;
  995. },
  996. /**
  997. * @param {string} key
  998. */
  999. remove: function(key)
  1000. {
  1001. var result;
  1002. if (key === "__proto__") {
  1003. if (!this._hasProtoKey)
  1004. return undefined;
  1005. --this._size;
  1006. delete this._hasProtoKey;
  1007. result = this._protoValue;
  1008. delete this._protoValue;
  1009. return result;
  1010. }
  1011. if (!Object.prototype.hasOwnProperty.call(this._map, key))
  1012. return undefined;
  1013. --this._size;
  1014. result = this._map[key];
  1015. delete this._map[key];
  1016. return result;
  1017. },
  1018. /**
  1019. * @return {!Array.<string>}
  1020. */
  1021. keys: function()
  1022. {
  1023. var result = Object.keys(this._map) || [];
  1024. if (this._hasProtoKey)
  1025. result.push("__proto__");
  1026. return result;
  1027. },
  1028. /**
  1029. * @return {!Array.<T>}
  1030. */
  1031. values: function()
  1032. {
  1033. var result = Object.values(this._map);
  1034. if (this._hasProtoKey)
  1035. result.push(this._protoValue);
  1036. return result;
  1037. },
  1038. /**
  1039. * @param {string} key
  1040. */
  1041. get: function(key)
  1042. {
  1043. if (key === "__proto__")
  1044. return this._protoValue;
  1045. if (!Object.prototype.hasOwnProperty.call(this._map, key))
  1046. return undefined;
  1047. return this._map[key];
  1048. },
  1049. /**
  1050. * @param {string} key
  1051. * @return {boolean}
  1052. */
  1053. contains: function(key)
  1054. {
  1055. var result;
  1056. if (key === "__proto__")
  1057. return this._hasProtoKey;
  1058. return Object.prototype.hasOwnProperty.call(this._map, key);
  1059. },
  1060. /**
  1061. * @return {number}
  1062. */
  1063. size: function()
  1064. {
  1065. return this._size;
  1066. },
  1067. clear: function()
  1068. {
  1069. this._map = {};
  1070. this._size = 0;
  1071. delete this._hasProtoKey;
  1072. delete this._protoValue;
  1073. }
  1074. }
  1075. /**
  1076. * @param {string} url
  1077. * @param {boolean=} async
  1078. * @param {function(?string)=} callback
  1079. * @return {?string}
  1080. */
  1081. function loadXHR(url, async, callback)
  1082. {
  1083. function onReadyStateChanged()
  1084. {
  1085. if (xhr.readyState !== XMLHttpRequest.DONE)
  1086. return;
  1087. if (xhr.status === 200) {
  1088. callback(xhr.responseText);
  1089. return;
  1090. }
  1091. callback(null);
  1092. }
  1093. var xhr = new XMLHttpRequest();
  1094. xhr.open("GET", url, async);
  1095. if (async)
  1096. xhr.onreadystatechange = onReadyStateChanged;
  1097. xhr.send(null);
  1098. if (!async) {
  1099. if (xhr.status === 200)
  1100. return xhr.responseText;
  1101. return null;
  1102. }
  1103. return null;
  1104. }
  1105. /**
  1106. * @constructor
  1107. */
  1108. function StringPool()
  1109. {
  1110. this.reset();
  1111. }
  1112. StringPool.prototype = {
  1113. /**
  1114. * @param {string} string
  1115. * @return {string}
  1116. */
  1117. intern: function(string)
  1118. {
  1119. // Do not mess with setting __proto__ to anything but null, just handle it explicitly.
  1120. if (string === "__proto__")
  1121. return "__proto__";
  1122. var result = this._strings[string];
  1123. if (result === undefined) {
  1124. this._strings[string] = string;
  1125. result = string;
  1126. }
  1127. return result;
  1128. },
  1129. reset: function()
  1130. {
  1131. this._strings = Object.create(null);
  1132. },
  1133. /**
  1134. * @param {Object} obj
  1135. * @param {number=} depthLimit
  1136. */
  1137. internObjectStrings: function(obj, depthLimit)
  1138. {
  1139. if (typeof depthLimit !== "number")
  1140. depthLimit = 100;
  1141. else if (--depthLimit < 0)
  1142. throw "recursion depth limit reached in StringPool.deepIntern(), perhaps attempting to traverse cyclical references?";
  1143. for (var field in obj) {
  1144. switch (typeof obj[field]) {
  1145. case "string":
  1146. obj[field] = this.intern(obj[field]);
  1147. break;
  1148. case "object":
  1149. this.internObjectStrings(obj[field], depthLimit);
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. var _importedScripts = {};
  1156. /**
  1157. * This function behavior depends on the "debug_devtools" flag value.
  1158. * - In debug mode it loads scripts synchronously via xhr request.
  1159. * - In release mode every occurrence of "importScript" in the js files
  1160. * that have been white listed in the build system gets replaced with
  1161. * the script source code on the compilation phase.
  1162. * The build system will throw an exception if it found importScript call
  1163. * in other files.
  1164. *
  1165. * To load scripts lazily in release mode call "loadScript" function.
  1166. * @param {string} scriptName
  1167. */
  1168. function importScript(scriptName)
  1169. {
  1170. if (_importedScripts[scriptName])
  1171. return;
  1172. var xhr = new XMLHttpRequest();
  1173. _importedScripts[scriptName] = true;
  1174. xhr.open("GET", scriptName, false);
  1175. xhr.send(null);
  1176. if (!xhr.responseText)
  1177. throw "empty response arrived for script '" + scriptName + "'";
  1178. var sourceURL = WebInspector.ParsedURL.completeURL(window.location.href, scriptName);
  1179. window.eval(xhr.responseText + "\n//# sourceURL=" + sourceURL);
  1180. }
  1181. var loadScript = importScript;
  1182. /**
  1183. * @constructor
  1184. */
  1185. function CallbackBarrier()
  1186. {
  1187. this._pendingIncomingCallbacksCount = 0;
  1188. }
  1189. CallbackBarrier.prototype = {
  1190. /**
  1191. * @param {function(T)=} userCallback
  1192. * @return {function(T=)}
  1193. * @template T
  1194. */
  1195. createCallback: function(userCallback)
  1196. {
  1197. console.assert(!this._outgoingCallback, "CallbackBarrier.createCallback() is called after CallbackBarrier.callWhenDone()");
  1198. ++this._pendingIncomingCallbacksCount;
  1199. return this._incomingCallback.bind(this, userCallback);
  1200. },
  1201. /**
  1202. * @param {function()} callback
  1203. */
  1204. callWhenDone: function(callback)
  1205. {
  1206. console.assert(!this._outgoingCallback, "CallbackBarrier.callWhenDone() is called multiple times");
  1207. this._outgoingCallback = callback;
  1208. if (!this._pendingIncomingCallbacksCount)
  1209. this._outgoingCallback();
  1210. },
  1211. /**
  1212. * @param {function(...)=} userCallback
  1213. */
  1214. _incomingCallback: function(userCallback)
  1215. {
  1216. console.assert(this._pendingIncomingCallbacksCount > 0);
  1217. if (userCallback) {
  1218. var args = Array.prototype.slice.call(arguments, 1);
  1219. userCallback.apply(null, args);
  1220. }
  1221. if (!--this._pendingIncomingCallbacksCount && this._outgoingCallback)
  1222. this._outgoingCallback();
  1223. }
  1224. }