CompoundStringUtilities.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var CompoundString =
  6. {
  7. create: function(charCount, pointerCount, cloneCount, strings) {
  8. var ch = "a";
  9. var ptr = "<aaaaaaa>";
  10. var s = "";
  11. while(true) {
  12. s += ""; // nondestructive, clone if necessary
  13. for(var i = 0; i < charCount; ++i)
  14. s += ch; // destructive
  15. for(var i = 0; i < pointerCount; ++i)
  16. s += ptr; // destructive
  17. if(cloneCount === 0)
  18. break;
  19. var s2 = s + ch; // nondestructive, clone for appending
  20. strings.push(s);
  21. s = s2;
  22. --cloneCount;
  23. charCount >>= 2;
  24. pointerCount >>= 2;
  25. }
  26. strings.push(s);
  27. },
  28. createTestStrings: function() {
  29. var strings = [];
  30. for(var pointerCount = 0; pointerCount <= 128; pointerCount = pointerCount === 0 ? 1 : pointerCount << 2) {
  31. for(var charCount = 0; charCount <= 1024; charCount = charCount === 0 ? 1 : charCount << 2) {
  32. for(var cloneCount = 0; cloneCount <= 8; cloneCount = cloneCount === 0 ? 1 : cloneCount << 2) {
  33. this.create(charCount, pointerCount, cloneCount, strings);
  34. if(charCount !== 0)
  35. this.create(charCount + 1, pointerCount, cloneCount, strings);
  36. if(pointerCount !== 0)
  37. this.create(charCount + 1, pointerCount, cloneCount, strings);
  38. }
  39. }
  40. }
  41. var ch = "a";
  42. var ptr = "<aaaaaaa>";
  43. var replaceCh = "_";
  44. var replaceRegex = /_/;
  45. strings.push((ch + replaceCh + ch).replace(replaceRegex, ch));
  46. strings.push((ptr + replaceCh + ptr).replace(replaceRegex, ch));
  47. strings.push((ch + replaceCh + ch).replace(replaceRegex, ptr));
  48. strings.push((ptr + replaceCh + ptr).replace(replaceRegex, ptr));
  49. // Large CompoundString created by concatenating a string to itself multiple times
  50. var bigString = "b"; // not using "a" here so that the concats will be destructive
  51. for(var i = 0; i < 16; ++i)
  52. bigString += bigString; // destructive
  53. bigString += replaceCh;
  54. if(bigString.length !== 0x10001)
  55. throw new Error("Big string length is invalid");
  56. strings.push(bigString.replace(replaceRegex, ch));
  57. // String with a high nesting level of CompoundString objects
  58. var nestedCompoundString = "c";
  59. nestedCompoundString += "c";
  60. nestedCompoundString += "c";
  61. for(var i = 0; i < 100000; ++i) {
  62. var s = "c";
  63. s += "c";
  64. s += "c";
  65. s += nestedCompoundString;
  66. nestedCompoundString = s;
  67. }
  68. strings.push(nestedCompoundString);
  69. // String with a high nesting level of ConcatString objects
  70. var nestedConcatString = "d";
  71. for(var i = 0; i < 100000; ++i)
  72. nestedConcatString = "d" + nestedConcatString;
  73. strings.push(nestedConcatString);
  74. // String with a high nesting level of alternating CompoundString and ConcatString objects
  75. var nestedConcatCompoundString = "";
  76. for(var i = 0; i < 100000; ++i) {
  77. var s = "e";
  78. s += "e";
  79. s += "e";
  80. s += nestedConcatCompoundString;
  81. nestedConcatCompoundString = "e" + s;
  82. }
  83. strings.push(nestedConcatCompoundString);
  84. return strings;
  85. }
  86. };