bugGH2386.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function toHexCP(c, cp) {
  6. var hex = "0123456789abcdef";
  7. return String.fromCharCode(hex.charCodeAt((c >> (cp * 4)) & 0xf));
  8. }
  9. function toHex(str) {
  10. var result = "";
  11. for(var i = 0; i < str.length; i++) {
  12. var c = str.charCodeAt(i);
  13. for (var cp = 3; cp >= 0; cp--) {
  14. result += toHexCP(c, cp);
  15. }
  16. }
  17. return "0x" + result;
  18. }
  19. var CHECK = function(h)
  20. {
  21. var hex_str = String.fromCharCode(h);
  22. var pattern = eval("/" + hex_str + "/");
  23. if (toHex(hex_str) != toHex(pattern.source)) {
  24. throw new Error("String encoding has failed? "
  25. + toHex(hex_str) + " != " + toHex(pattern.source));
  26. }
  27. }
  28. CHECK("0x0000");
  29. CHECK("0x0080");
  30. CHECK("0x0800");
  31. CHECK("0xFF80");
  32. CHECK("0xFFFD");
  33. CHECK("0xFFFFFF");
  34. CHECK("0xFFFFFF80");
  35. CHECK("0xFFFFFF80FF");
  36. function CHECK_EVAL(s)
  37. {
  38. var eval_s = new RegExp( s ).source;
  39. if (s !== eval_s) throw new Error(
  40. "String Encoding is broken ? ->" + s);
  41. }
  42. var CH1 = String.fromCharCode('0xe4b8ad');
  43. var CH2 = String.fromCharCode('0xe69687');
  44. var CH3 = String.fromCharCode('0xe336b2');
  45. var CH4 = String.fromCharCode('0xe336b2aa');
  46. var CHX = String.fromCharCode("0x80808080");
  47. var BUFF = '';
  48. for(var i = 0; i < 16; i++)
  49. {
  50. var str = CH1;
  51. CHECK_EVAL(str + CHX + BUFF)
  52. CHECK_EVAL(str + BUFF + CHX)
  53. CHECK_EVAL(str + BUFF + CHX + '1')
  54. str += BUFF + CH2 + CHX;
  55. BUFF += '1';
  56. CHECK_EVAL(str + '1' + CH3);
  57. CHECK_EVAL(str + '12' + CH3);
  58. CHECK_EVAL(str + '123' + CH3);
  59. CHECK_EVAL(str + '1' + CH4);
  60. CHECK_EVAL(str + '12' + CH4);
  61. CHECK_EVAL(str + '123' + CH4)
  62. }
  63. console.log("PASS");