Mercurial > repos > rhope
comparison 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 |
comparison
equal
deleted
inserted
replaced
104:2d2da148d844 | 108:a7add4db4a25 |
---|---|
1 | |
2 Import pattern.rhope | |
1 | 3 |
2 UTF8 Expect[num,arr,index,count,consumed:out] | 4 UTF8 Expect[num,arr,index,count,consumed:out] |
3 { | 5 { |
4 byte <- [arr]Index[index] | 6 byte <- [arr]Index[index] |
5 { | 7 { |
774 outindex,after,nomatch <- =Delim[string,delims,~] | 776 outindex,after,nomatch <- =Delim[string,delims,~] |
775 } | 777 } |
776 } | 778 } |
777 } | 779 } |
778 | 780 |
781 Pattern@String[string:out] | |
782 { | |
783 out <- string | |
784 } | |
785 | |
786 Pattern@String Slice[string:out] | |
787 { | |
788 out <- string | |
789 } | |
790 | |
791 Pattern@String Cat[string:out] | |
792 { | |
793 out <- Flatten[string] | |
794 } | |
795 | |
796 Match@String[string,cmp:num,no match,idx] | |
797 { | |
798 n <- [string]Length | |
799 ,no match <- If[[string]=[[cmp]Slice[n]]] | |
800 { | |
801 num <- Val[n] | |
802 idx <- 0 | |
803 } | |
804 } | |
805 | |
806 Match@String Slice[string,cmp:num,no match,idx] | |
807 { | |
808 n <- [string]Length | |
809 ,no match <- If[[string]=[[cmp]Slice[n]]] | |
810 { | |
811 num <- Val[n] | |
812 idx <- 0 | |
813 } | |
814 } | |
815 | |
779 | 816 |
780 _Partition[string,delims:matched,after,not found] | 817 _Partition[string,delims:matched,after,not found] |
781 { | 818 { |
782 not found <- If[[string]=[""]] {} | 819 not found <- If[[string]=[""]] {} |
783 { | 820 { |
784 ,after <- =Delim[string, delims, [delims]First] | 821 [delims]Match[string] |
785 { | 822 { |
786 matched <- [delims]Index[~] | 823 matched,after <- [string]Slice[~] |
787 } {} { | 824 }{ |
788 [string]Slice[1] {} | 825 [string]Slice[1] {} |
789 { matched,after,not found <- _Partition[~,delims] } | 826 { matched,after,not found <- _Partition[~,delims] } |
790 } | 827 } |
791 } | 828 } |
792 } | 829 } |
793 | 830 |
794 Partition[string,odelims:before,matched,after,not found] | 831 Partition[string,delims:before,matched,after,not found] |
795 { | 832 { |
796 dt <- Blueprint Of[odelims] | 833 matched,after,not found <- _Partition[string,Pattern[delims]] |
797 If[ [[[dt]=[String()]] Or [[dt]=[String Slice()]]] Or [[dt]=[String Cat()]] ] | |
798 { | |
799 delims <- [List[]]Append[odelims] | |
800 }{ | |
801 delims <- Val[odelims] | |
802 } | |
803 matched,after,not found <- _Partition[string,delims] | |
804 { dlen <- Length[~] } | 834 { dlen <- Length[~] } |
805 { alen <- Length[~] } | 835 { alen <- Length[~] } |
806 before <- [string]Slice[ [[string]Length]-[[dlen]+[alen]] ] | 836 before <- [string]Slice[ [[string]Length]-[[dlen]+[alen]] ] |
807 } | 837 } |
808 | 838 |
872 wt <- Blueprint Of[with] | 902 wt <- Blueprint Of[with] |
873 If[ [[[wt]=[String()]] Or [[wt]=[String Slice()]]] Or [[wt]=[String Cat()]] ] | 903 If[ [[[wt]=[String()]] Or [[wt]=[String Slice()]]] Or [[wt]=[String Cat()]] ] |
874 { | 904 { |
875 replacement <- with | 905 replacement <- with |
876 }{ | 906 }{ |
877 replacement <- [with]Index[[toreplace]Find[=[delim,?]]] | 907 ,,idx <- [toreplace]Match[delim] |
908 replacement <- [with]Index[idx] | |
878 } | 909 } |
879 out <- [[~]Append[replacement]]Append[Replace[after,toreplace,with]] | 910 out <- [[~]Append[replacement]]Append[Replace[after,toreplace,with]] |
880 } {} {} { | 911 } {} {} { |
881 out <- string | 912 out <- string |
882 } | 913 } |