BolEol.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // WOOB 1147829 - these cause a crash in the compat regex engine, but they don't repro the crash most of the time
  6. match(/^a/m, "b");
  7. match(/$a?/m, "");
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. function measureTime(f) {
  10. var d = new Date();
  11. f();
  12. echo(new Date() - d);
  13. }
  14. function myToString(o, quoteStrings) {
  15. switch(o) {
  16. case null:
  17. case undefined:
  18. return "" + o;
  19. }
  20. switch(typeof o) {
  21. case "boolean":
  22. return "" + o;
  23. case "number":
  24. {
  25. var s = "" + o;
  26. var e = s.indexOf("e");
  27. for(var i = (e == -1 ? s.length : e) - 3; i > 0; i -= 3)
  28. s = s.substring(0, i) + "," + s.substring(i);
  29. return s;
  30. }
  31. case "string":
  32. {
  33. var hex = "0123456789abcdef";
  34. var s = "";
  35. for(var i = 0; i < o.length; ++i) {
  36. var c = o.charCodeAt(i);
  37. switch(c) {
  38. case 0x0:
  39. s += "\\0";
  40. continue;
  41. case 0x8:
  42. s += "\\b";
  43. continue;
  44. case 0xb:
  45. s += "\\v";
  46. continue;
  47. case 0xc:
  48. s += "\\f";
  49. continue;
  50. }
  51. if(quoteStrings) {
  52. switch(c) {
  53. case 0x9:
  54. s += "\\t";
  55. continue;
  56. case 0xa:
  57. s += "\\n";
  58. continue;
  59. case 0xd:
  60. s += "\\r";
  61. continue;
  62. case 0x22:
  63. s += "\\\"";
  64. continue;
  65. case 0x5c:
  66. s += "\\\\";
  67. continue;
  68. }
  69. }
  70. if(c >= 0x20 && c < 0x7f)
  71. s += o.charAt(i);
  72. else if(c <= 0xff)
  73. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  74. else
  75. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  76. }
  77. if(quoteStrings)
  78. s = "\"" + s + "\"";
  79. return s;
  80. }
  81. case "object":
  82. case "function":
  83. break;
  84. default:
  85. return "<unknown type '" + typeof o + "'>";
  86. }
  87. if(o instanceof Array) {
  88. var s = "[";
  89. for(var i = 0; i < o.length; ++i) {
  90. if(i)
  91. s += ", ";
  92. s += myToString(o[i], true);
  93. }
  94. return s + "]";
  95. }
  96. if(o instanceof Error)
  97. return o.name + ": " + o.message;
  98. if(o instanceof RegExp)
  99. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  100. return "" + o;
  101. }
  102. function echo() {
  103. var doEcho;
  104. if(this.WScript)
  105. doEcho = function (s) { this.WScript.Echo(s); };
  106. else if(this.document)
  107. doEcho =
  108. function (s) {
  109. s = s
  110. .replace(/&/g, "&&")
  111. .replace(/</g, "&lt;")
  112. .replace(/>/g, "&gt;");
  113. this.document.write(s + "<br/>");
  114. };
  115. else
  116. doEcho = function (s) { this.print(s); };
  117. echo =
  118. function () {
  119. var s = "";
  120. for(var i = 0; i < arguments.length; ++i)
  121. s += myToString(arguments[i]);
  122. doEcho(s);
  123. };
  124. echo.apply(this, arguments);
  125. }
  126. function safeCall(f) {
  127. var args = [];
  128. for(var a = 1; a < arguments.length; ++a)
  129. args.push(arguments[a]);
  130. try {
  131. return f.apply(this, args);
  132. } catch(ex) {
  133. echo(ex);
  134. }
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  137. function exec(r, s) {
  138. echo("exec(", r, ", ", myToString(s, true), ");");
  139. var result = r.exec(s);
  140. echo(result);
  141. return result;
  142. }
  143. function test(r, s) {
  144. echo("test(", r, ", ", myToString(s, true), ");");
  145. var result = r.test(s);
  146. echo(result);
  147. return result;
  148. }
  149. function match(r, s) {
  150. echo("match(", r, ", ", myToString(s, true), ");");
  151. var result = s.match(r);
  152. echo(result);
  153. return result;
  154. }
  155. function replace(r, s, w) {
  156. echo("replace(", myToString(r, true), ", ", myToString(s, true), ", ", myToString(w, true), ");");
  157. var result = s.replace(r, w);
  158. echo(result);
  159. return result;
  160. }
  161. function split(r, s) {
  162. echo("split(", r, ", ", myToString(s, true), ");");
  163. var result = s.split(r);
  164. echo(result);
  165. return result;
  166. }
  167. function search(r, s) {
  168. echo("search(", r, ", ", myToString(s, true), ");");
  169. var result = s.search(r);
  170. echo(result);
  171. return result;
  172. }