Mercurial > repos > rhope
diff string.rhope @ 108:a7add4db4a25
Improved implementation of Pattern
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 06 Oct 2010 23:46:47 +0000 |
parents | 2d2da148d844 |
children | 336da6ce8174 |
line wrap: on
line diff
--- 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]] } {} {} {