Builder.js 4.0 KB

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