source: Dev/branches/rest-dojo-ui/client/dojox/string/Builder.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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