Lasso Soft Inc. > Home

[lp_string_printf]

Linklp_string_printf
AuthorBil Corry
CategoryString
Version8.x
LicensePublic Domain
Posted22 Jan 2006
Updated23 Jan 2006
More by this author...

Description

Returns a formatted string.  Be sure to cast the params as the correct type!

Mostly implements this:  http://www.cplusplus.com/ref/cstdio/printf.html

Requires [lp_string_asc] [lp_math_dectobin] [lp_integer_toUnsigned] [lp_string_pad] [lp_string_chr] [lp_decimal_precisionSet] [lp_math_dectohex]

Sample Usage

var:'data' = (: 
	(: '10/20/2005',28,'MegaWidgets',12.18111,1281.921000),
	(: '10/21/2005',21,'GigaWidgets',9.73333,974.883)
	);

'
';
(lp_string_printf: '%-13s%-8s%-25s%7s   %11s\r\n', 'Date','Units','Client','Rate','Total');
(lp_string_printf: '%s\r\n', '-' * 67);
iterate: $data, local:'d';
	(lp_string_printf: '%-13s%-8d%-25s%#6.2f%%   $%#10.2f\r\n', #d);
/iterate;
(lp_string_printf: '%s\r\n', '-' * 67);
'\r\n\r\n';
(lp_string_printf: '|%10d|%10x|%10#x|%*i|%010b|%10.2e|', 127, 127, 127, '05', 127, 127,127); 
'
'; Returns: Date Units Client Rate Total ------------------------------------------------------------------- 10/20/2005 28 MegaWidgets 12.18% $ 1281.92 10/21/2005 21 GigaWidgets 9.73% $ 974.88 ------------------------------------------------------------------- | 127| 7F| 0x7F|00127|0001111111| 1.27e+002|

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:'lp_string_printf',
	-description='Returns a formatted string.  Be sure to cast the params as the correct type!',
	-priority='replace',
	-required='format', -type='string';

	local:'out' = string;
	local:'format_item' = false;
	local:'precision_item' = false;
	local:'flags' = string;
	local:'width' = string;
	local:'width_pad' = string;
	local:'precision' = string;
	local:'type' = string;
	local:'param' = 2;
	local:'params' = params;
	local:'params_size' = params->size;
	local:'temp' = string;

	if: #params_size == 2 && params->(get:2)->type == 'array';
		#params = (array: null);
		#params->(merge: params->(get:2));
		#params_size = #params->size;
	/if;
	
	
	// parse format string
	iterate: #format, local:'char';

		if: #format_item;

			// ---- %% = % (escaped %) ----	
				if: ('%' >> #char) && (#flags->size == 0 )&& (#width->size == 0) && (#precision->size == 0) && (#type->size == 0);
					#out += #char;
					#format_item = false;
		
			// ---- flags ----	
				else: '-+ #{}' >> #char;
					#flags += #char;
					
			// ---- width ----					
				else: !#precision_item && ('0123456789' >> #char) && (#width != '*');
					#width += #char;
				else: ('*' >> #char) && (#width->size == 0);
					#width = #char;
					if: #param <= #params_size;
						#width = #params->(get: #param);
						#param += 1;
					/if;

			// ---- precision ----
				else: ('.' >> #char) && !#precision_item;
					#precision_item = true;
				else: #precision_item && ('0123456789' >> #char);
					#precision += #char;
	
			// ---- type ----
				else: 'bcdiefgsux' >> #char;
					#type = #char;
					#format_item = false;
					
					#width_pad = ' ';
					if: (#width->size) && (#width->(get:1) == '0');
						#width_pad = '0';
					/if;

					if: #param <= #params_size;
						#temp = #params->(get: #param);
						#param += 1;
						
						// format the param given the format item
						select: #type;
							case:'b';
								if: #temp->type == 'string';
									#temp = (lp_string_asc: #temp);
								/if;
								#temp = (lp_math_dectobin:(lp_integer_toUnsigned: #temp));
								if: #precision->size;
									#temp = (lp_string_pad: #temp, #precision, '0', -padleft, -notrim, -nocrop);
								/if;
							case:'c';
								if: #temp->type == 'integer';
									#temp = (lp_string_chr: #temp);
								/if;
								#temp = (string: #temp);
								if: #temp->size;
									#temp = #temp->(get:1);
								/if;
							case:'d';
								if: #temp->type == 'string';
									#temp = (lp_string_asc: #temp);
								/if;
								#temp = (integer: #temp);
								if: (#flags >> '+') && (#temp >= 0);
									#temp = '+' #temp;
								else: (#flags >> ' ') && (#temp >=0);
									#temp = ' ' #temp;
								/if;
								if: #precision->size;
									#temp = (lp_string_pad: #temp, #precision, ' ', -padleft, -notrim, -nocrop);
								else:
									#temp = (lp_string_pad: #temp, 1, ' ', -padleft, -notrim, -nocrop);
								/if;
							case:'i';
								if: #temp->type == 'string';
									#temp = (lp_string_asc: #temp);
								/if;
								#temp = (integer: #temp);
								if: (#flags >> '+') && (#temp >= 0);
									#temp = '+' #temp;
								else: (#flags >> ' ') && (#temp >=0);
									#temp = ' ' #temp;
								/if;
								if: #precision->size;
									#temp = (lp_string_pad: #temp, #precision, ' ', -padleft, -notrim, -nocrop);
								else:
									#temp = (lp_string_pad: #temp, 1, ' ', -padleft, -notrim, -nocrop);
								/if;
							case:'e';
								#temp = (decimal: #temp);
								if: #precision->size;
									#temp->(setformat: -precision=#precision, -scientific=true);
								else;
									#temp->(setformat: -precision=6, -scientific=true);
								/if;

								#temp = (string: #temp);
								
								if: (#flags >> '+') && (#temp >= 0);
									#temp = '+' #temp;
								else: (#flags >> ' ') && (#temp >=0);
									#temp = ' ' #temp;
								/if;
								
								if: (#flags !>> '#');
									#temp->(removetrailing:'0');
									#temp->(removetrailing:'.');
								/if;							
							case:'f';
								#temp = (decimal: #temp);
								if: #precision->size;
									#temp = (lp_decimal_precisionSet: #temp, #precision);
								else;
									#temp = (lp_decimal_precisionSet: #temp, 6);
								/if;
								
								#temp = (string: #temp);
								
								if: (#flags >> '+') && (#temp >= 0);
									#temp = '+' #temp;
								else: (#flags >> ' ') && (#temp >=0);
									#temp = ' ' #temp;
								/if;
								
								if: (#flags !>> '#');
									#temp->(removetrailing:'0');
									#temp->(removetrailing:'.');
								/if;
							case:'s';
								#temp = (string: #temp);
								if: #precision->size;
									#temp = #temp->(substring: 1, (integer:#precision));
								/if;
							case:'u';
								if: #temp->type == 'string';
									#temp = (lp_string_asc: #temp);
								/if;
								#temp = (lp_integer_toUnsigned: #temp);
								if: #precision->size;
									#temp = (lp_string_pad: #temp, #precision, ' ', -padleft, -notrim, -nocrop);
								else:
									#temp = (lp_string_pad: #temp, 1, ' ', -padleft, -notrim, -nocrop);
								/if;							
							case:'x';
								if: #temp->type == 'string';
									#temp = (lp_string_asc: #temp);
								/if;
								#temp = (lp_math_dectohex:(lp_integer_toUnsigned: #temp));
								if: (#flags >> '#');
									#temp = '0x' #temp;
								/if;
								if: #precision->size;
									#temp = (lp_string_pad: #temp, #precision, ' ', -padleft, -notrim, -nocrop);
								else:
									#temp = (lp_string_pad: #temp, 1, ' ', -padleft, -notrim, -nocrop);
								/if;
						/select;
		
						if: (string:#width)->size; // add padding?
							if: #flags >> '-'; // align left
								#out += (lp_string_pad: #temp, #width, #width_pad, -alignleft, -notrim, -nocrop);
							else: #flags >> '{'; // align center left
								#out += (lp_string_pad: #temp, #width, #width_pad, -aligncenterleft, -notrim, -nocrop);
							else: #flags >> '}'; // align center right
								#out += (lp_string_pad: #temp, #width, #width_pad, -aligncenterright, -notrim, -nocrop);
							else; // align right
								#out += (lp_string_pad: #temp, #width, #width_pad, -alignright, -notrim, -nocrop);
							/if;
						else;
							#out += #temp;
						/if;

					else;
						// not enough params for the format string
					/if;
			
			// ---- unknown char ----
				else;
					// throw away #char since we don't understand it
				/if;
		else: #char == '%';
			#format_item = true;
			#flags = string;
			#width = string;
			#precision = string;
			#type = string;
		else;
			#out += #char;
		/if;
		
		// reset precision_item?
		if: #precision_item && ('.0123456789' !>> #char);
			#precision_item = false;
		/if;

	/iterate;

	
	return: #out;

/define_tag;

]

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