]> gitweb.factorcode.org Git - factor.git/blob - extra/modern/slices/slices.factor
modern.slices: Add some more slice words
[factor.git] / extra / modern / slices / slices.factor
1 ! Copyright (C) 2016 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs fry kernel locals math sequences
4 sequences.deep sequences.extras strings unicode ;
5 IN: modern.slices
6
7 : >strings ( seq -- str )
8     [ dup slice? [ >string ] when ] deep-map ;
9
10 : matching-delimiter ( ch -- ch' )
11     H{
12         { CHAR: ( CHAR: ) }
13         { CHAR: [ CHAR: ] }
14         { CHAR: { CHAR: } }
15         { CHAR: < CHAR: > }
16         { CHAR: : CHAR: ; }
17     } ?at drop ;
18
19 : matching-delimiter-string ( string -- string' )
20     [ matching-delimiter ] map ;
21
22 : matching-section-delimiter ( string -- string' )
23     dup ":" tail? [
24         rest but-last ";" ">" surround
25     ] [
26         rest ">" append
27     ] if ;
28
29 ERROR: unexpected-end n string ;
30 : nth-check-eof ( n string -- nth )
31     2dup ?nth [ 2nip ] [ unexpected-end ] if* ;
32
33 : peek-from ( n/f string -- ch )
34     over [ ?nth ] [ 2drop f ] if ;
35
36 : previous-from ( n/f string -- ch )
37     over [ [ 1 - ] dip ?nth ] [ 2drop f ] if ;
38
39 ! Allow eof
40 : next-char-from ( n/f string -- n'/f string ch/f )
41     over [
42         2dup ?nth [ [ 1 + ] 2dip ] [ f ] if*
43     ] [
44         [ 2drop f ] [ nip ] 2bi f
45     ] if ;
46
47 : prev-char-from-slice-end ( slice -- ch/f )
48     [ to>> 2 - ] [ seq>> ] bi ?nth ;
49
50 : prev-char-from-slice ( slice -- ch/f )
51     [ from>> 1 - ] [ seq>> ] bi ?nth ;
52
53 : next-char-from-slice ( slice -- ch/f )
54     [ to>> ] [ seq>> ] bi ?nth ;
55
56 : char-before-slice ( slice -- ch/f )
57     [ from>> 1 - ] [ seq>> ] bi ?nth ;
58
59 : char-after-slice ( slice -- ch/f )
60     [ to>> ] [ seq>> ] bi ?nth ;
61
62 : find-from* ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt ? )
63     [ find-from ] keepd
64     pick [ drop t ] [ length -rot nip f ] if ; inline
65
66 : skip-blank-from ( n string -- n' string )
67     over [
68         [ [ blank? not ] find-from* 2drop ] keep
69     ] when ; inline
70
71 : skip-til-eol-from ( n string -- n' string )
72     [ [ "\r\n" member? ] find-from* 2drop ] keep ; inline
73
74 :: take-slice ( n string count -- n' string slice )
75     n count + :> to
76     to
77     string
78     n to string <slice> ;
79
80 ERROR: expected-sequence-error expected actual ;
81 : check-sequence ( expected actual -- actual/* )
82     2dup sequence= [ nip ] [ expected-sequence-error ] if ;
83
84 : expect-and-span ( n string slice expected-string -- n' string slice' )
85     dup length '[ _ take-slice ] 2dip
86     rot check-sequence span-slices ;
87
88 :: split-slice-back ( slice n -- slice1 slice2 )
89     slice [ from>> ] [ to>> ] [ seq>> ] tri :> ( from to seq )
90     from to n - seq <slice>
91     to n - to seq <slice> ;
92
93 ! Don't include the whitespace in the slice
94 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
95     n [
96         n string [ "\s\r\n" member? ] find-from :> ( n' ch )
97         n' string
98         n n' string ?<slice>
99         ch
100     ] [
101         f string f f
102     ] if ; inline
103
104 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
105     n string quot find-from :> ( n' ch )
106     n' string
107     n n' string ?<slice>
108     ch ; inline
109
110 : slice-until ( n string quot -- n' string slice/f )
111     (slice-until) drop ; inline
112
113 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
114     n [
115         n string [ "\s\r\n" member? not ] find-from :> ( n' ch )
116         n' string
117         n n' string ?<slice>
118         ch
119     ] [
120         n string f f
121     ] if ; inline
122
123 : skip-whitespace ( n/f string -- n'/f string )
124     slice-til-not-whitespace 2drop ;
125
126 : empty-slice-end ( seq -- slice )
127     [ length dup ] [ ] bi <slice> ; inline
128
129 : empty-slice-from ( n seq -- slice )
130     dupd <slice> ; inline
131
132 :: slice-til-eol ( n string -- n' string slice/f ch/f )
133     n [
134         n string '[ "\r\n" member? ] find-from :> ( n' ch )
135         n' string
136         n n' string ?<slice>
137         ch
138     ] [
139         n string string empty-slice-end f
140     ] if ; inline
141
142 :: merge-slice-til-eol-slash'' ( n string -- n' string slice/f ch/f )
143     n [
144         n string '[ "\r\n\\" member? ] find-from :> ( n' ch )
145         n' string
146         n n' string ?<slice>
147         ch
148     ] [
149         n string string empty-slice-end f
150     ] if ; inline
151
152 : merge-slice-til-whitespace ( n string slice --  n' string slice' )
153     pick [
154         [ slice-til-whitespace drop ] dip merge-slices
155     ] when ;
156
157 : merge-slice-til-eol ( n string slice --  n' string slice' )
158     [ slice-til-eol drop ] dip merge-slices ;
159
160 : slice-between ( slice1 slice2 -- slice )
161     ! ensure-same-underlying
162     slice-order-by-from
163     [ to>> ]
164     [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
165
166 : slice-before ( slice -- slice' )
167     [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
168
169 : (?nth) ( n/f string/f -- obj/f )
170     over [ (?nth) ] [ 2drop f ] if ;
171
172 :: merge-slice-til-eol-slash' ( n string slice -- n' string slice/f ch/f )
173     n string merge-slice-til-eol-slash'' :> ( n' string' slice' ch' )
174     ch' CHAR: \\ = [
175         n' 1 + string' (?nth) "\r\n" member? [
176             n' 2 + string' slice slice' span-slices merge-slice-til-eol-slash'
177         ] [
178             "omg" throw
179         ] if
180     ] [
181         n' string' slice slice' span-slices ch'
182     ] if ;
183
184 ! Supports \ at eol (with no space after it)
185 : slice-til-eol-slash ( n string -- n' string slice/f ch/f )
186     2dup empty-slice-from merge-slice-til-eol-slash' ;
187
188 :: slice-til-separator-inclusive ( n string tokens -- n' string slice/f ch/f )
189     n string '[ tokens member? ] find-from [ dup [ 1 + ] when ] dip  :> ( n' ch )
190     n' string
191     n n' string ?<slice>
192     ch ; inline
193
194 : slice-til-separator-exclusive ( n string tokens -- n' string slice/f ch/f )
195     slice-til-separator-inclusive dup [
196         [ [ 1 - ] change-to ] dip
197     ] when ;
198
199 ! Takes at least one character if not whitespace
200 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
201     n [
202         n string '[ tokens member? ] find-from
203         dup "\s\r\n" member? [
204             :> ( n' ch )
205             n' string
206             n n' string ?<slice>
207             ch
208         ] [
209             [ dup [ 1 + ] when ] dip :> ( n' ch )
210             n' string
211             n n' string ?<slice>
212             ch
213         ] if
214     ] [
215         f string f f
216     ] if ; inline
217
218 ERROR: subseq-expected-but-got-eof n string expected ;
219
220 :: slice-til-string ( n string search --  n' string payload end-string )
221     search string n subseq-start-from :> n'
222     n' [ n string search subseq-expected-but-got-eof ] unless
223     n' search length +  string
224     n n' string ?<slice>
225     n' dup search length + string ?<slice> ;
226
227 : modify-from ( slice n -- slice' )
228     '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
229
230 : modify-to ( slice n -- slice' )
231     [ [ from>> ] [ to>> ] [ seq>> ] tri ] dip
232     swap [ + ] dip <slice> ;
233
234 ! { CHAR: \] [ read-closing ] }
235 ! { CHAR: \} [ read-closing ] }
236 ! { CHAR: \) [ read-closing ] }
237 : read-closing ( n string tok -- n string tok )
238     dup length 1 = [
239         -1 modify-to [ 1 - ] 2dip
240     ] unless ;
241
242 : rewind-slice ( n string slice -- n' string )
243     pick [
244         length swap [ - ] dip
245     ] [
246         [ nip ] dip [ [ length ] bi@ - ] keepd
247     ] if ; inline