Builder.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. if (!dojo._hasResource["dojox.string.Builder"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.string.Builder"] = true;
  6. dojo.provide("dojox.string.Builder");
  7. (function() {
  8. dojox.string.Builder = function(/* String? */str) {
  9. // summary:
  10. // A fast buffer for creating large strings
  11. // str: The initial string to seed the buffer with
  12. this.b = dojo.isIE ? [] : "";
  13. if (str) {
  14. this.append(str);
  15. }
  16. };
  17. var m = {
  18. append : function(/* String */s) {
  19. // summary: Append all arguments to the end of the buffer
  20. return this.appendArray(dojo._toArray(arguments)); // dojox.string.Builder
  21. },
  22. concat : function(/* String */s) {
  23. return this.append(s);
  24. },
  25. appendArray : function(/* Array */strings) {
  26. this.b = String.prototype.concat.apply(this.b, strings);
  27. return this;
  28. },
  29. clear : function() {
  30. // summary: Remove all characters from the buffer
  31. this._clear();
  32. this.length = 0;
  33. return this;
  34. },
  35. replace : function(oldStr, newStr) {
  36. // summary: Replace instances of one string with another in the
  37. // buffer
  38. var s = this.toString();
  39. s = s.replace(oldStr, newStr);
  40. this._reset(s);
  41. this.length = s.length;
  42. return this;
  43. },
  44. remove : function(start, len) {
  45. // summary: Remove len characters starting at index start
  46. if (len == 0) {
  47. return this;
  48. }
  49. var s = this.toString();
  50. this.clear();
  51. if (start > 0) {
  52. this.append(s.substring(0, start));
  53. }
  54. if (start + len < s.length) {
  55. this.append(s.substring(start + len));
  56. }
  57. return this;
  58. },
  59. insert : function(index, str) {
  60. // summary: Insert string str starting at index
  61. var s = this.toString();
  62. this.clear();
  63. if (index == 0) {
  64. this.append(str);
  65. this.append(s);
  66. return this;
  67. } else {
  68. this.append(s.substring(0, index));
  69. this.append(str);
  70. this.append(s.substring(index));
  71. }
  72. return this;
  73. },
  74. toString : function() {
  75. return this.b;
  76. },
  77. _clear : function() {
  78. this.b = "";
  79. },
  80. _reset : function(s) {
  81. this.b = s;
  82. }
  83. }; // will hold methods for Builder
  84. if (dojo.isIE) {
  85. dojo.mixin(m, {
  86. toString : function() {
  87. // Summary: Get the buffer as a string
  88. return this.b.join("");
  89. },
  90. appendArray : function(strings) {
  91. this.b = this.b.concat(strings);
  92. return this;
  93. },
  94. _clear : function() {
  95. this.b = [];
  96. },
  97. _reset : function(s) {
  98. this.b = [s];
  99. }
  100. });
  101. }
  102. dojo.extend(dojox.string.Builder, m);
  103. })();
  104. }