stringbuffer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utility for fast string concatenation.
  16. */
  17. goog.provide('goog.string.StringBuffer');
  18. /**
  19. * Utility class to facilitate string concatenation.
  20. *
  21. * @param {*=} opt_a1 Optional first initial item to append.
  22. * @param {...*} var_args Other initial items to
  23. * append, e.g., new goog.string.StringBuffer('foo', 'bar').
  24. * @constructor
  25. */
  26. goog.string.StringBuffer = function(opt_a1, var_args) {
  27. if (opt_a1 != null) {
  28. this.append.apply(this, arguments);
  29. }
  30. };
  31. /**
  32. * Internal buffer for the string to be concatenated.
  33. * @type {string}
  34. * @private
  35. */
  36. goog.string.StringBuffer.prototype.buffer_ = '';
  37. /**
  38. * Sets the contents of the string buffer object, replacing what's currently
  39. * there.
  40. *
  41. * @param {*} s String to set.
  42. */
  43. goog.string.StringBuffer.prototype.set = function(s) {
  44. this.buffer_ = '' + s;
  45. };
  46. /**
  47. * Appends one or more items to the buffer.
  48. *
  49. * Calling this with null, undefined, or empty arguments is an error.
  50. *
  51. * @param {*} a1 Required first string.
  52. * @param {*=} opt_a2 Optional second string.
  53. * @param {...?} var_args Other items to append,
  54. * e.g., sb.append('foo', 'bar', 'baz').
  55. * @return {!goog.string.StringBuffer} This same StringBuffer object.
  56. * @suppress {duplicate}
  57. */
  58. goog.string.StringBuffer.prototype.append = function(a1, opt_a2, var_args) {
  59. // Use a1 directly to avoid arguments instantiation for single-arg case.
  60. this.buffer_ += String(a1);
  61. if (opt_a2 != null) { // second argument is undefined (null == undefined)
  62. for (var i = 1; i < arguments.length; i++) {
  63. this.buffer_ += arguments[i];
  64. }
  65. }
  66. return this;
  67. };
  68. /**
  69. * Clears the internal buffer.
  70. */
  71. goog.string.StringBuffer.prototype.clear = function() {
  72. this.buffer_ = '';
  73. };
  74. /**
  75. * @return {number} the length of the current contents of the buffer.
  76. */
  77. goog.string.StringBuffer.prototype.getLength = function() {
  78. return this.buffer_.length;
  79. };
  80. /**
  81. * @return {string} The concatenated string.
  82. * @override
  83. */
  84. goog.string.StringBuffer.prototype.toString = function() {
  85. return this.buffer_;
  86. };