| 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 |
/** |
| 16 |
* @fileoverview Utility for fast string concatenation. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.string.StringBuffer'); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Utility class to facilitate string concatenation. |
| 25 |
* |
| 26 |
* @param {*=} opt_a1 Optional first initial item to append. |
| 27 |
* @param {...*} var_args Other initial items to |
| 28 |
* append, e.g., new goog.string.StringBuffer('foo', 'bar'). |
| 29 |
* @constructor |
| 30 |
*/ |
| 31 |
goog.string.StringBuffer = function(opt_a1, var_args) { |
| 32 |
if (opt_a1 != null) { |
| 33 |
this.append.apply(this, arguments); |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Internal buffer for the string to be concatenated. |
| 40 |
* @type {string} |
| 41 |
* @private |
| 42 |
*/ |
| 43 |
goog.string.StringBuffer.prototype.buffer_ = ''; |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Sets the contents of the string buffer object, replacing what's currently |
| 48 |
* there. |
| 49 |
* |
| 50 |
* @param {*} s String to set. |
| 51 |
*/ |
| 52 |
goog.string.StringBuffer.prototype.set = function(s) { |
| 53 |
this.buffer_ = '' + s; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Appends one or more items to the buffer. |
| 59 |
* |
| 60 |
* Calling this with null, undefined, or empty arguments is an error. |
| 61 |
* |
| 62 |
* @param {*} a1 Required first string. |
| 63 |
* @param {*=} opt_a2 Optional second string. |
| 64 |
* @param {...?} var_args Other items to append, |
| 65 |
* e.g., sb.append('foo', 'bar', 'baz'). |
| 66 |
* @return {!goog.string.StringBuffer} This same StringBuffer object. |
| 67 |
* @suppress {duplicate} |
| 68 |
*/ |
| 69 |
goog.string.StringBuffer.prototype.append = function(a1, opt_a2, var_args) { |
| 70 |
// Use a1 directly to avoid arguments instantiation for single-arg case. |
| 71 |
this.buffer_ += String(a1); |
| 72 |
if (opt_a2 != null) { // second argument is undefined (null == undefined) |
| 73 |
for (var i = 1; i < arguments.length; i++) { |
| 74 |
this.buffer_ += arguments[i]; |
| 75 |
} |
| 76 |
} |
| 77 |
return this; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Clears the internal buffer. |
| 83 |
*/ |
| 84 |
goog.string.StringBuffer.prototype.clear = function() { |
| 85 |
this.buffer_ = ''; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* @return {number} the length of the current contents of the buffer. |
| 91 |
*/ |
| 92 |
goog.string.StringBuffer.prototype.getLength = function() { |
| 93 |
return this.buffer_.length; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* @return {string} The concatenated string. |
| 99 |
* @override |
| 100 |
*/ |
| 101 |
goog.string.StringBuffer.prototype.toString = function() { |
| 102 |
return this.buffer_; |
| 103 |
}; |