CFLib.org – Common Function Library Project

unIndent(str)

Last updated March 7, 2009

Version: 0 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Removes unnecessary leading tabs or spaces (whitespace chars) from each line of a string, preserving the formatting while flushing the string left. It is basically a block un-indent (like your IDE probably does with shift-tab).

Return Values:
returns a string

Example:

view plain print about
<pre>
<cfsavecontent variable="str">
    <tag>
        <child>
    </tag>
</cfsavecontent>
<cfoutput>#htmlCodeFormat(unIndent(str))#</cfoutput>
</pre>

Parameters:

Name Description Required
str String to be modified Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Un-indents strings but preserves formatting
 * 
 * @param str      String to be modified (Required)
 * @return returns a string 
 * @author Nathan Strutz (strutz@gmail.com) 
 * @version 0, March 7, 2009 
 */

function unIndent(str) {
    var lines = str.split("\n");
    var i = 0;
    var minSpaceDist = 9999;
    var newStr = "";

    for(i=1; i lte arrayLen(lines); i=i+1) {
        if (len(trim(lines[i]))) {
            minSpaceDist = maxmin(minSpaceDist, reFind("[\S]",lines[i])-1), 0);
        }
    }

    for(i=1; i lte arrayLen(lines); i=i+1) {
        newStr = newStr & removeChars(lines[i], 1, minSpaceDist) & chr(10);
    }
    return newStr;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson