changeset 77:a748300a4143

Some untested progress on String
author Mike Pavone <pavone@retrodev.com>
date Thu, 08 Jul 2010 09:57:16 -0400
parents 004f0fc8941f
children 4d5ea487f810
files string.rhope
diffstat 1 files changed, 189 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/string.rhope	Wed Jul 07 00:36:59 2010 -0400
+++ b/string.rhope	Thu Jul 08 09:57:16 2010 -0400
@@ -84,7 +84,7 @@
 	Length(Int32,Naked)
 }
 
-String[in(Array):out(String)]
+String@Array[in:out(String)]
 {
 	out <- [[Build[String()]]Buffer <<[in]]Length <<[Count UTF8[in, 0, 0]]
 }
@@ -146,11 +146,84 @@
 	out <- string
 }
 
+_CPOff to BOff[buff,cur,expected:outcur,outboff]
+{
+	If[expected]
+	{
+		outcur <- cur
+		outboff <- 0i32
+	}{
+		err <- If[[byte]>[192u8]] {}
+		{
+			err <- If[[byte]<[128u8]] {}
+			{
+				outcur <- _CPOff to BOff[buff, [cur]+[1i32], [expected]-[1i32]] {}
+				{ outboff <- [~]+[1i32] }
+			}
+		}
+
+		Val[err]
+		{
+			outcur <- [cur]+[1i32]
+			outboff <- 1i32
+		}
+	}
+}
+
+CPOff to BOff[buff,cur,boff,cpoff:out]
+{
+	If[[cur]=[cpoff]]
+	{
+		out <- boff
+	}{
+		byte <- [buff]Index[cur]
+		If[[byte] < [128u8]]
+		{
+			nboff <- [bof]+[1i32]
+			ncur <- [cur]+[1i32]
+		}{
+			If[[byte]<[192u8]]
+			{
+				//Error: Encoding for 2nd,3rd or 4th byte of sequence
+				//treat as a single character
+				nboff <- [bof]+[1i32]
+				ncur <- [cur]+[1i32]
+			}{
+				If[[byte]<[224u8]]
+				{
+					expect <- 1i32
+				}{
+					If[[byte]<[240u8]]
+					{
+						expect <- 2i32
+					}{
+						If[[byte]<[245u8]]
+						{
+							expect <- 3i32
+						}{
+							//Error
+							nboff <- [bof]+[1i32]
+							ncur <- [cur]+[1i32]
+						}
+					}
+				}
+				Val[expect]
+				{
+					ncur <- _CPOff to BOff[buff, [cur]+[1i32], expect] {}
+					{ nboff <- [1i32]+[~] }
+				}
+			}
+		}
+		out <- CPOff to BOff[buff, ncur, cpoff, nboff]
+	}
+}
+
 Slice@String[string,slicepoint:left,right]
 {
 	//TODO: Handle invalid slicepoints
-	left <- String Slice[string, 0i32, slicepoint]
-	right <- String Slice[string, slicepoint, [[string]Length >>]-[slicepoint]]
+	sliceoffset <- CPOff to BOff[[string]Buffer >>, 0i32, 0i32, slicepoint]
+	left <- String Slice[string, 0i32, slicepoint, sliceoffset]
+	right <- String Slice[string, sliceoffset, [[string]Length >>]-[slicepoint], [[[string]Buffer >>]Length]-[sliceoffset]]
 }
 
 Byte@String[string,index:out,invalid]
@@ -176,7 +249,7 @@
 	}
 }
 
-=@String[left,right:out]
+Eq String[left,right:out]
 {
 	,out <- If[[[left]Length] = [[right]Length]] 
 	{
@@ -184,16 +257,126 @@
 	}
 }
 
+=@String[left,right:out]
+{
+	out <- Eq String[left,right]	
+}
+
+Byte Length@String[string:out]
+{
+	out <- [[string]Buffer >>]Length
+}
+
+Append@String[left,right:out]
+{
+	out <- String Cat[left,right]
+}
+
 Blueprint String Slice
 {
 	Source
 	Offset(Int32,Naked)	
 	Length(Int32,Naked)
+	ByteLen(Int32,Naked)
+}
+
+String Slice[source,offset,length,bytelen:out(String Slice)]
+{
+	out <- [[[[Build[String Slice()]]Source <<[source]]Offset <<[offset]]Length <<[length]]ByteLen <<[bytelen]
+}
+
+Byte@String Slice[string,index:out,invalid]
+{
+	,invalid <- If[[index]<[[string]ByteLen >>]]
+	{
+		out,invalid <- [[string]Source >>]Byte[[index]+[[string]Offset >>]]
+	}
+}
+
+Byte Length@String Slice[string:out]
+{
+	out <- [string]ByteLen >>
+}
+
+=@String Slice[left,right:out]
+{
+	out <- Eq String[left,right]
+}
+
+_Flatten@String[string,dest,offset,count:out]
+{
+	If[count]
+	{
+		out <- [string]_Flatten[[dest]Append[ [[string]Buffer >>]Index[offset] ], [offset]+[1i32], [count]-[1i32]]
+	}{
+		out <- dest
+	}
+}
+
+Flatten@String[string:out]
+{
+	out <- string
+}
+
+_Flatten@String Slice[string,dest,offset,count:out]
+{
+	out <- [[string]Source >>]_Flatten[dest, [[string]Offset >>]+[offset], count]
+}
+
+Flatten@String Slice[string:out]
+{
+	out <- String[ [[string]Source >>]_Flatten[Array[], [string]Offset >>, [string]ByteLen >>] ]
 }
 
-String Slice[source,offset,length:out(String Slice)]
+Append@String Slice[left,right:out]
+{
+	out <- String Cat[left,right]
+}
+
+Blueprint String Cat
 {
-	out <- [[[Build[String Slice()]]Source <<[source]]Offset <<[offset]]Length <<[length]
+	Left
+	Right
+	Length
+	ByteLen
+}
+
+String Cat[left,right:out]
+{
+	out <- [[[[Build[String Cat()]
+		]Left <<[left]
+		]Right <<[right]
+		]Length <<[ [[left]Length]+[[right]Length] ]
+		]ByteLen <<[ [[left]Byte Length]+[[right]Byte Length] ]
 }
 
+Append@String Cat[left,right:out]
+{
+	out <- String Cat[left,right]
+}
 
+Byte@String Cat[string,index:out,invalid]
+{
+	leftlen <- [[string]Left >>]Byte Length
+	If[[index]<[leftlen]]
+	{
+		out,invalid <- [[string]Left >>]Byte[index]
+	}{
+		rindex <- [index]-[leftlen]
+		,invalid <- If[[rindex]<[[[string]Right >>]Byte Length]]
+		{
+			out,invalid <- [[string]Right >>]Byte[rindex]
+		}
+	}
+}
+
+Byte Length@String Cat[string:out]
+{
+	out <- [string]ByteLen >>
+}
+
+_Flatten@String Cat[string,dest,offset,count:out]
+{
+	[string]Left >>
+}
+