match_global.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // BEGIN PRELUDE
  6. function echo(o) {
  7. try {
  8. document.write(o + "<br/>");
  9. echo = function(o) { document.write(o + "<br/>"); };
  10. } catch (ex) {
  11. try {
  12. WScript.Echo("" + o);
  13. echo = function(o) { WScript.Echo("" + o); };
  14. } catch (ex2) {
  15. print("" + o);
  16. echo = function(o) { print("" + o); };
  17. }
  18. }
  19. }
  20. var suppressLastIndex = false;
  21. var suppressRegExp = false;
  22. var suppressIndex = false;
  23. function safeCall(f) {
  24. var args = [];
  25. for (var a = 1; a < arguments.length; ++a)
  26. args.push(arguments[a]);
  27. try {
  28. return f.apply(this, args);
  29. } catch (ex) {
  30. echo("EXCEPTION");
  31. }
  32. }
  33. hex = "0123456789abcdef";
  34. function dump(o) {
  35. var sb = [];
  36. if (o === null)
  37. sb.push("null");
  38. else if (o === undefined)
  39. sb.push("undefined");
  40. else if (o === true)
  41. sb.push("true");
  42. else if (o === false)
  43. sb.push("false");
  44. else if (typeof o === "number")
  45. sb.push(o.toString());
  46. else if (typeof o == "string") {
  47. if (o.length > 8192)
  48. sb.push("<long string>");
  49. else {
  50. sb.push("\"");
  51. var start = -1;
  52. for (var i = 0; i < o.length; i++) {
  53. var c = o.charCodeAt(i);
  54. if (c < 32 || c > 127 || c == '"'.charCodeAt(0) || c == '\\'.charCodeAt(0)) {
  55. if (start >= 0)
  56. sb.push(o.substring(start, i));
  57. start = -1;
  58. sb.push("\\u");
  59. sb.push(String.fromCharCode(hex.charCodeAt((c >> 12) & 0xf)));
  60. sb.push(String.fromCharCode(hex.charCodeAt((c >> 8) & 0xf)));
  61. sb.push(String.fromCharCode(hex.charCodeAt((c >> 4) & 0xf)));
  62. sb.push(String.fromCharCode(hex.charCodeAt((c >> 0) & 0xf)));
  63. }
  64. else {
  65. if (start < 0)
  66. start = i;
  67. }
  68. }
  69. if (start >= 0)
  70. sb.push(o.substring(start, o.length));
  71. sb.push("\"");
  72. }
  73. }
  74. else if (o instanceof RegExp) {
  75. var body = o.source;
  76. sb.push("/");
  77. var start = -1;
  78. for (var i = 0; i < body.length; i++) {
  79. var c = body.charCodeAt(i);
  80. if (c < 32 || c > 127) {
  81. if (start >= 0)
  82. sb.push(body.substring(start, i));
  83. start = -1;
  84. sb.push("\\u");
  85. sb.push(String.fromCharCode(hex.charCodeAt((c >> 12) & 0xf)));
  86. sb.push(String.fromCharCode(hex.charCodeAt((c >> 8) & 0xf)));
  87. sb.push(String.fromCharCode(hex.charCodeAt((c >> 4) & 0xf)));
  88. sb.push(String.fromCharCode(hex.charCodeAt((c >> 0) & 0xf)));
  89. }
  90. else {
  91. if (start < 0)
  92. start = i;
  93. }
  94. }
  95. if (start >= 0)
  96. sb.push(body.substring(start, body.length));
  97. sb.push("/");
  98. if (o.global)
  99. sb.push("g");
  100. if (o.ignoreCase)
  101. sb.push("i");
  102. if (o.multiline)
  103. sb.push("m");
  104. if (!suppressLastIndex && o.lastIndex !== undefined) {
  105. sb.push(" /*lastIndex=");
  106. sb.push(o.lastIndex);
  107. sb.push("*/ ");
  108. }
  109. }
  110. else if (o.length !== undefined) {
  111. sb.push("[");
  112. for (var i = 0; i < o.length; i++) {
  113. if (i > 0)
  114. sb.push(",");
  115. sb.push(dump(o[i]));
  116. }
  117. sb.push("]");
  118. if (!suppressIndex && (o.input !== undefined || o.index !== undefined))
  119. {
  120. sb.push(" /*input=");
  121. sb.push(dump(o.input));
  122. sb.push(", index=");
  123. sb.push(dump(o.index));
  124. // IE only
  125. // sb.push(", lastIndex=");
  126. // sb.push(dump(o.lastIndex));
  127. sb.push("*/ ");
  128. }
  129. }
  130. else if (o.toString !== undefined) {
  131. sb.push("<object with toString>");
  132. }
  133. else
  134. sb.push(o.toString());
  135. return sb.join("");
  136. }
  137. function pre(w, origargs, n) {
  138. var sb = [w];
  139. sb.push("(");
  140. for (var i = 0; i < n; i++) {
  141. if (i > 0) sb.push(", ");
  142. sb.push(dump(origargs[i]));
  143. }
  144. if (origargs.length > n) {
  145. sb.push(", ");
  146. sb.push(dump(origargs[n]));
  147. origargs[0].lastIndex = origargs[n];
  148. }
  149. sb.push(");");
  150. echo(sb.join(""));
  151. }
  152. function post(r) {
  153. if (!suppressLastIndex) {
  154. echo("r.lastIndex=" + dump(r.lastIndex));
  155. }
  156. if (!suppressRegExp) {
  157. // IE only
  158. // echo("RegExp.index=" + dump(RegExp.index));
  159. // echo("RegExp.lastIndex=" + dump(RegExp.lastIndex));
  160. var sb = [];
  161. sb.push("RegExp.${_,1,...,9}=[");
  162. sb.push(dump(RegExp.$_));
  163. for (var i = 1; i <= 9; i++) {
  164. sb.push(",");
  165. sb.push(dump(RegExp["$" + i]));
  166. }
  167. sb.push("]");
  168. echo(sb.join(""));
  169. }
  170. }
  171. function exec(r, s) {
  172. pre("exec", arguments, 2);
  173. echo(dump(r.exec(s)));
  174. post(r);
  175. }
  176. function test(r, s) {
  177. pre("test", arguments, 2);
  178. echo(dump(r.test(s)));
  179. post(r);
  180. }
  181. function replace(r, s, o) {
  182. pre("replace", arguments, 3);
  183. echo(dump(s.replace(r, o)));
  184. post(r);
  185. }
  186. function split(r, s) {
  187. pre("split", arguments, 2);
  188. echo(dump(s.split(r)));
  189. post(r);
  190. }
  191. function match(r, s) {
  192. pre("match", arguments, 2);
  193. echo(dump(s.match(r)));
  194. post(r);
  195. }
  196. function search(r, s) {
  197. pre("search", arguments, 2);
  198. echo(dump(s.search(r)));
  199. post(r);
  200. }
  201. function bogus(r, o) {
  202. echo("bogus(" + dump(r) + ", " + dump(o) + ");");
  203. try { new RegExp(r, o); echo("FAILED"); } catch (e) { echo("PASSED"); }
  204. }
  205. // END PRELUDE
  206. var p = /(\d*)(\D*)/g;
  207. var s = "123!234!567";
  208. exec(p, s);