Lasso Soft Inc. > Home

[string_wrap]

Linkstring_wrap
AuthorJason Huck
CategoryString
Version8.x
LicensePublic Domain
Posted29 Nov 2005
Updated29 Mar 2007
More by this author...

Description

Wraps a string to the specified length. An optional -trim parameter determines whether or not to trim whitespace from each line. Defaults to a length of 80 characters with trimming on, using unix style line breaks.

Sample Usage

string_wrap(
    $longstring,
    -length=60,
    -linebreak='\r\n',
    -trim=false
);

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'wrap',
	-namespace='string_',
	-req='text', -copy,
	-opt='length', -copy, -type='integer',
	-opt='linebreak', -type='string',
	-opt='trim', -type='boolean',
	-priority='replace',
	-description='Wraps the given string to the specified length.'
);
	local(
		'in' = string(#text),
		'out' = string
	);
	
	!local_defined('length') ? local('length' = 80);
	#length -= 1;
	
	!local_defined('linebreak') ? local('linebreak' = '\n');
	
	!local_defined('trim') ? local('trim' = true);
	
	#in->trim;
	#in->replace('\r\n','\n')&replace('\r','\n');
	
	iterate(#in->split('\n'), local('i'));
		local('line' = #i);
	
		#trim ? #line->trim;
	
		if(#line->size < #length);
			#out += #line + #linebreak;
		else;
			local(
				'lineIn' = #line,
				'lineOut' = string
			);
			
			while(#lineIn->size > #length);
				local('offset' = #length);
				
				while(#lineIn->size > #offset && #offset > 0 && !#lineIn->isspace(#offset));
					#offset -= 1;
				/while;
				
				#offset == 0 ? #offset = #length;
			
				local('chunk') = #lineIn->substring(1, #offset);
			
				#lineOut += #chunk + #linebreak;
				#lineIn->removeleading(#chunk);
			/while;
			
			#lineIn->size ? #lineOut += #lineIn + #linebreak;
			
			#out += #lineOut;
		/if;
	/iterate;
	
	return(#out);
/define_tag;

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft