changeset 160:f97a7d499182

Optimize _Partition@Pattern using a byte iterator
author Mike Pavone <pavone@retrodev.com>
date Fri, 07 Jan 2011 03:18:33 -0500
parents 0e06f29aa83d
children f5095855c878
files pattern.rhope string.rhope
diffstat 2 files changed, 146 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/pattern.rhope	Thu Dec 23 16:16:31 2010 -0500
+++ b/pattern.rhope	Fri Jan 07 03:18:33 2011 -0500
@@ -87,11 +87,6 @@
 	out <- p
 }
 
-_Match@Empty Pattern[pattern,string,n:num,no match]
-{
-	no match <- Yes
-}
-
 Match@Empty Pattern[pattern,string:num,no match]
 {
 	no match <- Yes
@@ -101,6 +96,33 @@
 {
 	num,no match,idx <- [pattern]_Match[string,0,[string]Byte[0]]
 }
+/*
+_Match@Pattern[pattern,string,n,b:num,no match,idx]
+{
+	myb <- [pattern]Byte >>
+	If[[b]=[myb]]
+	{
+		,check terminal <- [string]Byte[[n]+[1]]
+		{ num,check terminal,idx <- [[pattern]Straight >>]_Match[string, [n]+[1], ~] }
+		Val[check terminal]
+		{
+			,no match <- If[[pattern]Terminal? >>]
+			{
+				num <- [n]+[1]
+				idx <- Index >>[pattern]
+			}
+		}
+	
+	}{
+		If[[b]<[myb]]
+		{
+			num, no match, idx <- [[pattern]Left >>]_Match[string, n,b]
+		}{
+			num, no match, idx <- [[pattern]Right >>]_Match[string, n,b]
+		}
+	}
+}
+*/
 
 _Match@Pattern[pattern,string,n,b:num,no match,idx]
 {
@@ -128,8 +150,51 @@
 	}
 }
 
+_Match Iter@Empty Pattern[pattern,origpat,n,count,b,if end:out,next,no next,seekfunc,seek]
+{
+	out <- Call[if end] {
+	}{
+		If[count]
+		{
+			seek <- [count]-[1]
+			seekfunc <- Val[[origpat]_Match Iter[origpat,[n]+[1],0,?,_No Pattern Match[?]]]
+		}{
+			next <- Val[[origpat]_Match Iter[origpat, [n]+[1], 0, ?, _No Pattern Match[?]]]
+			no next <- Val[_No Pattern Match[?]]
+		}
+	}
+}
+
+_Match Iter@Pattern[pattern,origpat,n,count,b,if end:out,next,no next,seekfunc,seek]
+{
+	myb <- [pattern]Byte >>
+	If[[b]=[myb]]
+	{
+		ncount <- [count]+[1]
+		If[[pattern]Terminal? >>]
+		{ next if end <- Val[Val[[[[()]Append[n]]Append[ncount]]Append[[pattern]Index >>],?]] }
+		{ next if end <- Val[_No Pattern Match[?]] }
+		no next <- Val[next if end]
+		
+		next <- Val[[[pattern]Straight >>]_Match Iter[origpat, n, ncount, ?, next if end]]
+	}{
+		If[[b]<[myb]]
+		{
+			out,next,no next,seekfunc,seek <- [[pattern]Left >>]_Match Iter[origpat,n,count,b,if end]
+		}{
+			out,next,no next,seekfunc,seek <- [[pattern]Right >>]_Match Iter[origpat,n,count,b,if end]
+		}
+	}
+}
+
+
 _Partition@Pattern[delims,string:matched,after,not found]
 {
+	,not found <- [string]Iter Bytes[_Match Iter[delims,delims,0,0,?,_No Pattern Match[?]], 0]
+	{
+		matched, after <- [[string]Substring[[~]Index[0], 0]]Slice[[~]Index[1]]
+	}
+	/*
         not found <- If[[string]=[""]] {}
         {
                 [delims]Match[string]
@@ -139,6 +204,7 @@
                         matched,after,not found <- _Partition[delims, [string]Substring[1, 0]]
                 }
         }
+     */
 }
 
 _Partition@Empty Pattern[delims,string:matched,after,not found]
--- a/string.rhope	Thu Dec 23 16:16:31 2010 -0500
+++ b/string.rhope	Fri Jan 07 03:18:33 2011 -0500
@@ -1142,6 +1142,81 @@
 	out <- string
 }
 
+_No Pattern Match[:out,no match]
+{
+	no match <- Yes
+}
+
+_Iter Bytes Buf[buf,offset,first,last,func,if end:out,end,seekfout,seekout]
+{
+	[buf]Index[offset]
+	{
+		out,nextf,no next,seekf,seek <- [func]Call[~] {}
+		{ 
+			If[[offset]<[last]]
+			{ out,end <- _Iter Bytes Buf[buf, [offset]+[1], first, last, nextf, no next] }
+			{ end <- [[()]Append[nextf]]Append[no next] }
+		}{}{
+			newoff <- [offset]-[seek]
+			If[[newoff]<[first]]
+			{
+				seekfout <- Val[seekf] 
+				seekout <- [first]-[newoff]
+			}{ out,end <- _Iter Bytes Buf[buf, newoff, first, last, seekf, _No Pattern Match[?]] }
+		}
+	}{
+		end <- [[()]Append[func]]Append[if end]
+	}
+}
+
+_Iter Bytes@String[string,func,start,if end:out,end,seekfunc,seek]
+{
+	out,end,seekfunc,seek <- _Iter Bytes Buf[[string]Buffer >>, start, 0, [[string]Byte Length]-[1], func, if end]
+}
+
+_Iter Bytes@String Slice[string,func,start,if end:out,end,seekfunc,seek]
+{
+	out,end,seekfunc,seek <- _Iter Bytes Buf[[[string]Source >>]Buffer >>, [[string]Offset >>]+[start], [string]Offset >>, [[[string]Byte Length]-[1]]+[[string]Offset >>], func, if end]
+}
+
+_Iter Bytes@String Cat[string,func,start,if end:out,end,seekfunc,seek]
+{
+	llen <- [[string]Left >>]Byte Length
+	If[[start]<[llen]]
+	{
+		out,,seekfunc,seek <- [[string]Left >>]_Iter Bytes[func, start, if end] {}
+		{ 
+			right off <- 0
+			right func <- [~]Index[0]
+			right if end <- [~]Index[1]
+		}
+	}{
+		right off <- [start]-[llen]
+		right func <- Val[func]
+		right if end <- Val[if end]
+	}
+	
+	Val[right off]
+	{
+		out,end,,rseek <- [[string]Right >>]_Iter Bytes[right func, ~, right if end] {} {}
+		{
+			out,end,seekfunc,seek <- [string]_Iter Bytes[~, [llen]-[rseek], _No Pattern Match[?]]
+		}
+	}
+}
+
+Iter Bytes[string,func,start:out,end]
+{
+	out <- [string]_Iter Bytes[func, start, _No Pattern Match[?]] {}
+	{
+		out <- Call[[~]Index[1]] {}
+		{
+			end <- Yes
+		}
+	}
+	{ end <- Yes }
+}
+
 Replace[string,otoreplace,with:out]
 {
 	toreplace <- Pattern[otoreplace]