changeset 108:a7add4db4a25

Improved implementation of Pattern
author Mike Pavone <pavone@retrodev.com>
date Wed, 06 Oct 2010 23:46:47 +0000
parents 2d2da148d844
children b7df624895b2 13dfe8214254
files pattern.rhope string.rhope
diffstat 2 files changed, 174 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pattern.rhope	Wed Oct 06 23:46:47 2010 +0000
@@ -0,0 +1,130 @@
+
+Blueprint Empty Pattern
+{
+}
+
+Empty Pattern[:out]
+{
+	out <- Build[Empty Pattern()]
+}
+
+Add String@Empty Pattern[pattern,string,idx:out]
+{
+	out <- [pattern]_Add String[string,0,idx]
+}
+
+_Add String@Empty Pattern[pattern,string,n,idx:out]
+{
+	If[[n]<[[string]Byte Length]]
+	{
+		out <- [[[[[[Build[Pattern()]
+		]Byte <<[[string]Byte[n]]
+		]Left <<[pattern]
+		]Right <<[pattern]
+		]Straight <<[[pattern]_Add String[string, [n]+[1],idx]]
+		]Terminal? <<[[n]=[[[string]Byte Length]-[1]]]
+		]Index <<[idx]
+	}{
+		out <- pattern
+	}
+}
+
+Blueprint Pattern
+{
+	Byte
+	Left
+	Right
+	Straight
+	Terminal?
+	Index
+}
+
+_Add String@Pattern[pattern,string,n,idx:out]
+{
+	If[[n]<[[string]Byte Length]]
+	{
+		b <- [string]Byte[n]
+		myb <- [pattern]Byte >>
+		If[[b]<[myb]]
+		{
+			out <- [pattern]Left <<[[[pattern]Left >>]_Add String[string,n,idx]]
+		}{
+			If[[b]>[myb]]
+			{
+				out <- [pattern]Right <<[[[pattern]Right >>]_Add String[string,n,idx]]
+			}{
+				newpat <- [pattern]Straight <<[[[pattern]Straight >>]_Add String[string,[n]+[1],idx]]
+				If[[n]=[[[string]Byte Length]-[1]]]
+				{
+					out <- [newpat]Terminal? <<[Yes]
+				}{
+					out <- Val[newpat]
+				}
+			}
+		}
+	}{
+		out <- pattern
+	}
+}
+
+Add String@Pattern[pattern,string,idx:out]
+{
+	out <- [pattern]_Add String[string,0,idx]
+}
+
+Pattern@List[list:out]
+{
+	out <- Fold[Add String[?], Empty Pattern[], list]
+}
+
+Pattern@List Leaf[list:out]
+{
+        out <- Fold[Add String[?], Empty Pattern[], list]
+}
+
+Pattern@Pattern[p:out]
+{
+	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
+}
+
+Match@Pattern[pattern,string:num,no match,idx]
+{
+	num,no match,idx <- [pattern]_Match[string,0]
+}
+
+_Match@Pattern[pattern,string,n:num,no match,idx]
+{
+	b <- [string]Byte[n]
+	myb <- [pattern]Byte >>
+	If[[b]=[myb]]
+	{
+		num, ,idx <- [[pattern]Straight >>]_Match[string, [n]+[1]] { }
+		{
+			,no match <- If[[pattern]Terminal? >>]
+			{
+				num <- [n]+[1]
+				idx <- Index >>[pattern]
+			}
+		}
+	
+	}{
+		If[[b]<[myb]]
+		{
+			num, no match, idx <- [[pattern]Left >>]_Match[string, n]
+		}{
+			num, no match, idx <- [[pattern]Right >>]_Match[string, n]
+		}
+	}
+}
+
--- a/string.rhope	Wed Aug 25 03:19:57 2010 +0000
+++ b/string.rhope	Wed Oct 06 23:46:47 2010 +0000
@@ -1,3 +1,5 @@
+
+Import pattern.rhope
 
 UTF8 Expect[num,arr,index,count,consumed:out]
 {
@@ -776,31 +778,59 @@
 	}
 }
 
+Pattern@String[string:out]
+{
+	out <- string
+}
+
+Pattern@String Slice[string:out]
+{
+	out <- string
+}
+
+Pattern@String Cat[string:out]
+{
+	out <- Flatten[string]
+}
+
+Match@String[string,cmp:num,no match,idx]
+{
+	n <- [string]Length
+	,no match <- If[[string]=[[cmp]Slice[n]]]
+	{
+		num <- Val[n]
+		idx <- 0
+	}
+}
+
+Match@String Slice[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]=[""]] {}
 	{
-		,after <- =Delim[string, delims, [delims]First]
+		[delims]Match[string]
 		{
-			matched <- [delims]Index[~]
-		} {} {
+			matched,after <- [string]Slice[~]
+		}{
 			[string]Slice[1] {}
 			{ matched,after,not found <- _Partition[~,delims] }
 		}
 	}
 }
 
-Partition[string,odelims:before,matched,after,not found]
+Partition[string,delims:before,matched,after,not found]
 {
-	dt <- Blueprint Of[odelims]
-	If[ [[[dt]=[String()]] Or [[dt]=[String Slice()]]] Or [[dt]=[String Cat()]] ]
-	{
-		delims <- [List[]]Append[odelims]
-	}{
-		delims <- Val[odelims]
-	}
-	matched,after,not found <- _Partition[string,delims]
+	matched,after,not found <- _Partition[string,Pattern[delims]]
 	{ dlen <- Length[~] }
 	{ alen <- Length[~] }
 	before <- [string]Slice[ [[string]Length]-[[dlen]+[alen]] ]
@@ -874,7 +904,8 @@
 		{
 			replacement <- with
 		}{
-			replacement <- [with]Index[[toreplace]Find[=[delim,?]]]
+			,,idx <- [toreplace]Match[delim]
+			replacement <- [with]Index[idx]
 		}
 		out <- [[~]Append[replacement]]Append[Replace[after,toreplace,with]]
 	} {} {} {