# HG changeset patch # User Mike Pavone # Date 1294388366 18000 # Node ID f5095855c8783a2f5c45e2fb0e19f3880d0d0702 # Parent d1e206ff75f977cd7ea367bf5f110b3793319575# Parent f97a7d4991822462496ca2b1b164a0231f86a009 Merge diff -r d1e206ff75f9 -r f5095855c878 pattern.rhope --- a/pattern.rhope Fri Jan 07 03:17:20 2011 -0500 +++ b/pattern.rhope Fri Jan 07 03:19:26 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] diff -r d1e206ff75f9 -r f5095855c878 string.rhope --- a/string.rhope Fri Jan 07 03:17:20 2011 -0500 +++ b/string.rhope Fri Jan 07 03:19:26 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]