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