view stringjs.rhope @ 189:d0e3a13c1bd9 default tip

Remove old calculator example
author Mike Pavone <pavone@retrodev.com>
date Fri, 07 Oct 2011 00:24:04 -0700
parents fc3815b7462f
children
line wrap: on
line source



Blueprint String
{
	Buffer
}

Foreign Javascript:builtin
{
	print[val]
	parseInt[string,base(Int32,Naked):out(Int32,Naked)]
	_internal_js_eq[left,right:out(Boolean,Naked)]
	slice@[string,start(Int32,Naked),end(Int32,Naked):out]
	concat@[left,right:out]
}

Print@String[string:out]
{	
	print[[string]Buffer >>]
	out <- Yes
}

Int32@String[string:out]
{
	out <- parseInt[[string]Buffer >>, 10]
}

Hex Int32[str:out]
{
	out <- parseInt[[str]Buffer >>, 16]
}

Flatten@String[string:out]
{
	out <- string
}

Slice@String[string,slicepoint:left,right]
{
	If[[slicepoint]>=[[string]Length]]
	{
		left <- string
		right <- ""
	}{
		If[[slicepoint]<=[0]]
		{
			left <- ""
			right <- string
		}{
			[string]Buffer >>
			{
				left <- [Build[String()]]Buffer <<[[~]slice[0,slicepoint]]
				right <- [Build[String()]]Buffer <<[[~]slice[slicepoint,_internal_js_length[~]]]
			}
		}
	}
}
/*
Byte@String[string,index:out,invalid]
{
	out,invalid <- [[string]Buffer >>]Index[index]
}
*/
Length@String[string:out]
{
	out <- _internal_js_length[[string]Buffer >>]
}

Append@String[left,right(String):out]
{
	out <- [Build[String()]]Buffer <<[[[left]Buffer >>]concat[[right]Buffer >>]]
}

=@String[left,right(String):out]
{
	out <- [[left]Buffer >>]_internal_js_eq[[right]Buffer >>]
}

/*
=Delim[string,delims,index:outindex,after,nomatch]
{
	delim <- [delims]Index[index]
	If[[[string]Length]<[[delim]Length]]
	{
		try next <- Yes
	}{
		check,mafter <- [string]Slice[[delim]Length]
		,try next <- If[[check]=[delim]]
		{
			outindex <- index
			after <- Val[mafter]
		}
	}
	Val[try next]
	{
		,nomatch <- [delims]Next[index]
		{
			outindex,after,nomatch <- =Delim[string,delims,~]
		}
	}
}

Pattern@String[string:out]
{
	out <- string
}

Match@String[string,cmp:num,no match,idx]
{
	n <- [string]Length
	,no match <- If[[string]=[[cmp]Slice[n]]]
	{
		num <- Val[n]
		idx <- 0
	}
}

_Partition[string,delims:matched,after,not found]
{
	not found <- If[[string]=[""]] {}
	{
		[delims]Match[string]
		{
			matched,after <- [string]Slice[~]
		}{
			[string]Slice[1] {}
			{ matched,after,not found <- _Partition[~,delims] }
		}
	}
}

Partition[string,delims:before,matched,after,not found]
{
	matched,after,not found <- _Partition[string,Pattern[delims]]
	{ dlen <- Length[~] }
	{ alen <- Length[~] }
	before <- [string]Slice[ [[string]Length]-[[dlen]+[alen]] ]
}


Dict Type ID@String[string:out]
{
	out <- ID[String()]
}

Dict Bits@String[string,index:out,invalid]
{
	,invalid <- [string]Byte[index]
	{ out <- UInt32[~] }
}

_From Dict String[arr,el:out]
{
	out <- [arr]Append[Trunc UInt8[el]]
}

From Dict Key@String[string,data:out]
{
	out <- String[Fold[_From Dict String[?], Array[], data]]
}
*/

String@String[string:out]
{
	out <- string
}

/*
Replace[string,otoreplace,with:out]
{
	toreplace <- Pattern[otoreplace]
	,delim,after <-[string]Partition[toreplace]
	{
		wt <- Blueprint Of[with]
		If[ [[[wt]=[String()]] Or [[wt]=[String Slice()]]] Or [[wt]=[String Cat()]] ]
		{
			replacement <- with
		}{
			,,idx <- [toreplace]Match[delim]
			replacement <- [with]Index[idx]
		}
		out <- [[~]Append[replacement]]Append[Replace[after,toreplace,with]]
	} {} {} {
		out <- string
	}
}

_Join[list,delim,current,index:out]
{
	[list]Next[index]
	{
		out <- _Join[list, delim, [[current]Append[delim]]Append[String[[list]Index[~]]], ~]
	}{
		out <- current
	}
}

Join[list,delim:out]
{
	[list]First
	{
		out <- _Join[list, delim, String[[list]Index[~]], ~]
	}{
		out <- ""
	}
}

*/

Starts With[thing,starts with:out]
{
	out <- [[thing]Slice[[starts with]Length]] = [starts with]
}

Ends With[thing,ends with:out]
{
	,compare <- [thing]Slice[ [[thing]Length] - [[ends with]Length] ]
	out <- [compare] = [ends with]
}

If@String[str:yes,no]
{
	yes,no <- If[[str]Length]
}

/*
_Split[list,string,delim:out]
{
	,,rest <- [string]Partition[delim]
	{
		out <- _Split[[list]Append[~], rest, delim]
	} {} {} {
		out <- [list]Append[string]
	}
}

Split[string,delim:out]
{
	If[string]
	{ out <- _Split[(),string,delim] }
	{ out <- () }
}
*/

In[needle,haystack:out]
{
	If[haystack]
	{
		out <- If[[[haystack]Slice[Length[needle]]]=[needle]] {}
		{
			[haystack]Slice[1] {}
			{ out <- [needle]In[~] }
		}
	}{
		out <- No
	}
}

Left Trim[string,trim:trimmed]
{
	If[ [[string]Length] > [0] ]
	{
		first,rest <- [string]Slice[1]
		If[ [first]In[trim] ]
		{
			trimmed <- Left Trim[rest, trim]
		}{
			trimmed <- string
		}
	}{
		trimmed <- string
	}
}

Right Trim[string,trim:trimmed]
{
	If[ [[string]Length] > [0] ]
	{
		rest,last <- [string]Slice[ [[string]Length] - [1]]
		If[ [last]In[trim] ]
		{
			trimmed <- Right Trim[rest, trim]
		}{
			trimmed <- string
		}
	}{
		trimmed <- string
	}
}

Trim[string,trim:trimmed]
{
	left <- Left Trim[string, trim]
	trimmed <- Right Trim[left, trim]
}

/*
Contains[haystack,needle:out]
{
	[haystack]Partition[needle]
	{
		out <- Yes	
	} {} {} {
		out <- No
	}
}
*/

After[text,prefix:after,not found]
{
	If[[text]Starts With[prefix]]
	{
		,after <- [text]Slice[[prefix]Length]
	}{
		not found <- text
	}
}