diff string.rhope @ 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 64be565a40c6
line wrap: on
line diff
--- 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]