Builder.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define("dojox/string/Builder", ["dojo/_base/lang"],
  2. function(lang){
  3. lang.getObject("string", true, dojox).Builder =
  4. function(/*String?*/str){
  5. // summary:
  6. // A fast buffer for creating large strings.
  7. //
  8. // length: Number
  9. // The current length of the internal string.
  10. // N.B. the public nature of the internal buffer is no longer
  11. // needed because the IE-specific fork is no longer needed--TRT.
  12. var b = "";
  13. this.length = 0;
  14. this.append = function(/* String... */s){
  15. // summary: Append all arguments to the end of the buffer
  16. if(arguments.length>1){
  17. /*
  18. This is a loop unroll was designed specifically for Firefox;
  19. it would seem that static index access on an Arguments
  20. object is a LOT faster than doing dynamic index access.
  21. Therefore, we create a buffer string and take advantage
  22. of JS's switch fallthrough. The peformance of this method
  23. comes very close to straight up string concatenation (+=).
  24. If the arguments object length is greater than 9, we fall
  25. back to standard dynamic access.
  26. This optimization seems to have no real effect on either
  27. Safari or Opera, so we just use it for all.
  28. It turns out also that this loop unroll can increase performance
  29. significantly with Internet Explorer, particularly when
  30. as many arguments are provided as possible.
  31. Loop unroll per suggestion from Kris Zyp, implemented by
  32. Tom Trenka.
  33. Note: added empty string to force a string cast if needed.
  34. */
  35. var tmp="", l=arguments.length;
  36. switch(l){
  37. case 9: tmp=""+arguments[8]+tmp;
  38. case 8: tmp=""+arguments[7]+tmp;
  39. case 7: tmp=""+arguments[6]+tmp;
  40. case 6: tmp=""+arguments[5]+tmp;
  41. case 5: tmp=""+arguments[4]+tmp;
  42. case 4: tmp=""+arguments[3]+tmp;
  43. case 3: tmp=""+arguments[2]+tmp;
  44. case 2: {
  45. b+=""+arguments[0]+arguments[1]+tmp;
  46. break;
  47. }
  48. default: {
  49. var i=0;
  50. while(i<arguments.length){
  51. tmp += arguments[i++];
  52. }
  53. b += tmp;
  54. }
  55. }
  56. } else {
  57. b += s;
  58. }
  59. this.length = b.length;
  60. return this; // dojox.string.Builder
  61. };
  62. this.concat = function(/*String...*/s){
  63. // summary:
  64. // Alias for append.
  65. return this.append.apply(this, arguments); // dojox.string.Builder
  66. };
  67. this.appendArray = function(/*Array*/strings) {
  68. // summary:
  69. // Append an array of items to the internal buffer.
  70. // Changed from String.prototype.concat.apply because of IE.
  71. return this.append.apply(this, strings); // dojox.string.Builder
  72. };
  73. this.clear = function(){
  74. // summary:
  75. // Remove all characters from the buffer.
  76. b = "";
  77. this.length = 0;
  78. return this; // dojox.string.Builder
  79. };
  80. this.replace = function(/* String */oldStr, /* String */ newStr){
  81. // summary:
  82. // Replace instances of one string with another in the buffer.
  83. b = b.replace(oldStr,newStr);
  84. this.length = b.length;
  85. return this; // dojox.string.Builder
  86. };
  87. this.remove = function(/* Number */start, /* Number? */len){
  88. // summary:
  89. // Remove len characters starting at index start. If len
  90. // is not provided, the end of the string is assumed.
  91. if(len===undefined){ len = b.length; }
  92. if(len == 0){ return this; }
  93. b = b.substr(0, start) + b.substr(start+len);
  94. this.length = b.length;
  95. return this; // dojox.string.Builder
  96. };
  97. this.insert = function(/* Number */index, /* String */str){
  98. // summary:
  99. // Insert string str starting at index.
  100. if(index == 0){
  101. b = str + b;
  102. }else{
  103. b = b.slice(0, index) + str + b.slice(index);
  104. }
  105. this.length = b.length;
  106. return this; // dojox.string.Builder
  107. };
  108. this.toString = function(){
  109. // summary:
  110. // Return the string representation of the internal buffer.
  111. return b; // String
  112. };
  113. // initialize the buffer.
  114. if(str){ this.append(str); }
  115. };
  116. return dojox.string.Builder;
  117. });