ColdFire's SizeSplit function (and the dumb mistake I made)

When working on ColdFire 0.0.3, I added a new UDF that would take a string and split it into an array. This was used to split the header strings up into more manageable size items. The UDF worked fine (as far as I knew), but then Adam reported he had issues using the values on his side. Check out the UDF and see if you can spot the problem. (No fair looking at the code in the released version.) <cffunction name="sizeSplit" output="false" returnType="array" hint="Splits a string into an array of N chars"> <cfargument name="string" type="string" required="true"> <cfargument name="size" type="numeric" required="true"> <cfset var result = arrayNew(1)>

&lt;cfif len(arguments.string) lt arguments.size&gt;
	&lt;cfset result[1] = arguments.string&gt;
	&lt;cfreturn result&gt;
&lt;/cfif&gt;

&lt;cfloop condition="len(arguments.string) gt arguments.size"&gt;
	&lt;cfset arrayAppend(result, left(arguments.string, arguments.size))&gt;
	&lt;cfset arguments.string = right(arguments.string, len(arguments.string)-arguments.size)&gt;
&lt;/cfloop&gt;

&lt;cfreturn result&gt;	 &lt;/cffunction&gt; </code>

The code is rather simple. You pass a string and a size for the sections. The code is smart enough to detect a small string and not bother looping. If it does have to loop, it adds the portion to the array and then cuts down the original string.

Ok, see it yet?

Well what happens if the size of my string isn’t evenly divisible by the size I want? That’s right - I have left overs. Adam was having issues because I was returning incomplete arrays back to him. Luckily it was easy enough to solve:

<cffunction name="sizeSplit" output="false" returnType="array" hint="Splits a string into an array of N chars"> <cfargument name="string" type="string" required="true"> <cfargument name="size" type="numeric" required="true"> <cfset var result = arrayNew(1)>

&lt;cfif len(arguments.string) lt arguments.size&gt;
	&lt;cfset result[1] = arguments.string&gt;
	&lt;cfreturn result&gt;
&lt;/cfif&gt;

&lt;cfloop condition="len(arguments.string) gt arguments.size"&gt;
	&lt;cfset arrayAppend(result, left(arguments.string, arguments.size))&gt;
	&lt;cfset arguments.string = right(arguments.string, len(arguments.string)-arguments.size)&gt;
&lt;/cfloop&gt;
&lt;cfif len(arguments.string)&gt;
	&lt;cfset arrayAppend(result,arguments.string)&gt;
&lt;/cfif&gt;

&lt;cfreturn result&gt;	 &lt;/cffunction&gt; </code>

Notice now I check and see if I have anything left over. If so - I simply add it to the end of the array.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments