root/coldbox/trunk/src/system/plugins/StringBuffer.cfc @ 823

Revision 823, 11.9 kB (checked in by lmajano, 6 years ago)

Almost there.

Line 
1<!---
2********************************************************************************
3Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
4www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com
5********************************************************************************
6
7StringBuffer.cfc
8This CFC greatly increases the speed of string concatenation. CF strings are immutable. When you append a string
9to another string, a whole new string is created. This is fine for a small number of iterations but painfully
10slow and memory intensive for a large number of concatenation operations.
11
12greg.lively@gmail.com
13Use this program however you want.
14
15To use:
16<cfscript>
17        variables.joStringBuffer = createObject('component', 'StringBuffer');
18        variables.joStringBuffer.append(variables.someDataVar);
19        writeOutput(variables.joStringBuffer.getString());
20</cfscript>
21
22from Java 1.5 API:
23StringBuffer
24A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in
25time it contains some particular sequence of characters, but the length and content of the sequence can be changed
26through certain method calls.
27
28String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the
29operations on any particular instance behave as if they occur in some serial order that is consistent with the
30order of the method calls made by each of the individual threads involved.
31
32When a StringBuffer object is created it has a capacity, which is the number of characters that the StringBuffer will contain if full. If the default constructor is called with no parameters this capacity is set to 16, otherwise an int may be passed as a parameter to specify the capacity. Another constructor allows an initial set of characters to be passed as a parameter, in this case the capacity of the StringBuffer will be the number of characters in the initial string plus a further 16. The following example shows the possible ways of building StringBuffer objects.
33
34StringBuffer = strbuf new StringBuffer (); // capacity = 16
35StringBuffer = strbuf2 new StringBuffer (25); // capacity = 25
36StringBuffer = strbuf3 new StringBuffer ("Java"); // capacity = 4 + 16 = 20
37
38************************************************************************************************
39
40Author   :      Luis Majano
41Date     :      September 23, 2005
42Description :
43        Converted this cfc into a ColdBox plugin. You can now also, append to a file, if needed.
44
45Modification History:
4608/01/2006 - Updated the cfc to work for ColdBox.
47
48--->
49<cfcomponent name="StringBuffer"
50                         hint="This CFC greatly increases the speed of string concatenation. CF strings are immutable. When you append a string to another string, a whole new string is created. This is fine for a small number of iterations but painfully slow and memory intensive for a large number of concatenation operations."
51                         extends="coldbox.system.plugin"
52                         output="false"
53                         cache="false">
54
55<!------------------------------------------- CONSTRUCTOR ------------------------------------------->
56
57        <cffunction name="init" access="public" returntype="StringBuffer" output="false">
58                <cfargument name="controller" type="any" required="true">
59                <cfset super.Init(arguments.controller) />
60                <cfset setpluginName("StringBuffer")>
61                <cfset setpluginVersion("1.0")>
62                <cfset setpluginDescription("This is a facade to the java StringBuffer class.")>
63                <cfset instance.joStringBuffer = createObject("java","java.lang.StringBuffer") />
64                <cfreturn this>
65        </cffunction>
66
67        <!--- ************************************************************* --->
68
69        <cffunction name="setup" access="public" returntype="coldbox.system.plugin" output="false" hint="initializes the StringBuffer CF/java object">
70                <!--- ************************************************************* --->
71                <cfargument name="strIn"                        type="string"   required="No" default=""   hint="A string to initialize the buffer with. The bufferLength will be the number of characters + 16. This argument is mutually exclusive to BufferLength" />
72                <cfargument name="BufferLength"         type="numeric"  required="no" default="16" hint="The length to start the buffer at. The default is 16 characters. This argument is mutually exclusive to strIn ">
73                <!--- ************************************************************* --->
74                <cfif arguments.strIn neq "">
75                        <cfset instance.joStringBuffer.init(javaCast("string", arguments.strIn)) />
76                <cfelse>
77                        <cfset instance.joStringBuffer.init(javaCast("int", arguments.BufferLength)) />
78                </cfif>
79                <cfreturn this />
80        </cffunction>
81
82<!------------------------------------------- PUBLIC ------------------------------------------->
83
84        <!--- ************************************************************* --->
85
86        <cffunction name="append" returntype="void" access="public" output="No" hint="Append a string to the buffer.">
87                <!--- ************************************************************* --->
88                <cfargument name="strIn" type="string" required="No" default="" hint="a string to append to the buffer" />
89                <!--- ************************************************************* --->
90                <cfset instance.joStringBuffer.append(javaCast("string", arguments.strIn)) />
91        </cffunction>
92
93        <!--- ************************************************************* --->
94
95        <cffunction name="delete" returntype="void" access="public" output="No" hint="Removes the characters in a substring of this StringBuffer.">
96                <!--- ************************************************************* --->
97                <cfargument name="startPos" type="numeric" required="Yes" hint="The beginning index, inclusive." />
98                <cfargument name="endPos"       type="numeric" required="Yes" hint="The ending index, exclusive." />
99                <!--- ************************************************************* --->
100                <cfset instance.joStringBuffer.delete(javaCast("int", arguments.startPos),javaCast("int", arguments.endPos)) />
101        </cffunction>
102
103        <!--- ************************************************************* --->
104
105        <cffunction name="insertStr" returntype="void" access="public" output="No" hint="Inserts the string into this string buffer at an offset.">
106                <!--- ************************************************************* --->
107                <cfargument name="offSet"       type="numeric" required="No" default="0" hint="the offset" />
108                <cfargument name="inStr"        type="string" required="Yes" hint="a string" />
109                <!--- ************************************************************* --->
110                <cfset instance.joStringBuffer.insert(javaCast("int", arguments.offSet), javaCast("string", arguments.inStr)) />
111        </cffunction>
112
113        <!--- ************************************************************* --->
114
115        <cffunction name="replaceStr" returntype="void" access="public" output="No" hint="Replaces the chracters in a substring of this StringBuffer with characters in the specified inStr">
116                <!--- ************************************************************* --->
117                <cfargument name="startPos" type="numeric"      required="Yes" hint="The beginning index, inclusive." />
118                <cfargument name="endPos"       type="numeric"  required="Yes" hint="The ending index, exclusive." />
119                <cfargument name="inStr"        type="string"   required="Yes" hint="a string" />
120                <!--- ************************************************************* --->
121                <cfset instance.joStringBuffer.replace(javaCast("int", arguments.startPos), javaCast("int", arguments.endPos), javaCast("string", arguments.inStr)) />
122        </cffunction>
123
124        <!--- ************************************************************* --->
125
126        <cffunction name="indexOf" returntype="numeric" access="public" output="No" hint="Returns the index within this string of the first occurrence of the specified substring.">
127                <!--- ************************************************************* --->
128                <cfargument name="inStr"        type="string" required="Yes" hint="the substring for which to search" />
129                <cfargument name="fromPos"      type="numeric" required="No" default="0" hint="the index from which to start the search" />
130                <!--- ************************************************************* --->
131                <cfreturn instance.joStringBuffer.indexOf(javaCast("string", arguments.inStr),javaCast("int", arguments.fromPos)) />
132        </cffunction>
133
134        <!--- ************************************************************* --->
135
136        <cffunction name="lastIndexOf" returntype="numeric" access="public" output="No" hint="Returns the index within this string of the last occurrence of the specified substring.">
137                <!--- ************************************************************* --->
138                <cfargument name="inStr"        type="string" required="Yes" hint="the substring for which to search" />
139                <cfargument name="fromPos"      type="numeric" required="No" default="0" hint="the index from which to start the search" />
140                <!--- ************************************************************* --->
141                <cfreturn instance.joStringBuffer.lastIndexOf(javaCast("string", arguments.inStr),javaCast("int", arguments.fromPos)) />
142        </cffunction>
143
144        <!--- ************************************************************* --->
145
146        <cffunction name="substring" returntype="string" access="public" output="No" hint="Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.The substring begins at the specified index and extends to the end of the StringBuffer.">
147                <!--- ************************************************************* --->
148                <cfargument name="startPos" type="numeric" required="Yes" hint="The beginning index, inclusive." />
149                <cfargument name="endPos"       type="numeric" required="No" default="#(instance.joStringBuffer.length() - 1)#" hint="The ending index, exclusive." />
150                <!--- ************************************************************* --->
151                <cfreturn instance.joStringBuffer.substring(javaCast("int", arguments.startPos),javaCast("int", arguments.endPos)) />
152        </cffunction>
153
154        <!--- ************************************************************* --->
155
156        <cffunction name="reverseStr" returntype="void" access="public" output="No" hint="The character sequence contained in this string buffer is replaced by the reverse of the sequence.">
157                <cfset instance.joStringBuffer.reverse() />
158        </cffunction>
159
160        <!--- ************************************************************* --->
161
162        <cffunction name="setLength" returntype="void" access="public" output="No" hint="Sets the length of this String buffer.">
163                <!--- ************************************************************* --->
164                <cfargument name="newLength" type="numeric" required="true" hint="Length in characters to set.">
165                <!--- ************************************************************* --->
166                <cfset instance.joStringBuffer.setLength(JavaCast("int", arguments.newLength)) />
167        </cffunction>
168
169        <!--- ************************************************************* --->
170
171        <cffunction name="length" returntype="numeric" access="public" output="No" hint="Returns the length (character count) of this string buffer.">
172                <cfreturn instance.joStringBuffer.length() />
173        </cffunction>
174
175        <!--- ************************************************************* --->
176
177        <cffunction name="capacity" returntype="numeric" access="public" output="No" hint="Returns the current capacity of the String buffer.">
178                <cfreturn instance.joStringBuffer.capacity() />
179        </cffunction>
180
181        <!--- ************************************************************* --->
182
183        <cffunction name="getString" returntype="string" access="public" output="No" hint="Converts to a string representing the data in this string buffer.">
184                <cfreturn instance.joStringBuffer.toString() />
185        </cffunction>
186
187        <!--- ************************************************************* --->
188
189        <cffunction name="getStringBuffer" returntype="any" access="public" output="No" hint="Return the StringBuffer Java Object">
190                <cfreturn instance.joStringBuffer />
191        </cffunction>
192
193        <!--- ************************************************************* --->
194
195</cfcomponent>
Note: See TracBrowser for help on using the browser.