]> gitweb.factorcode.org Git - factor.git/blob - extra/modern/slices/slices.factor
modern.html: add some tests, fix some bugs, implement write-html
[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 ERROR: take-slice-error n string count ;
75 :: take-slice ( n string count -- n'/f string slice )
76     n [ n string count take-slice-error ] unless
77     n count + :> to
78     to
79     string
80     n to string <slice> ;
81
82 ERROR: expected-sequence-error expected actual ;
83 : check-sequence ( expected actual -- actual/* )
84     2dup sequence= [ nip ] [ expected-sequence-error ] if ;
85
86 : expect-and-span ( n string slice expected-string -- n' string slice' )
87     dup length '[ _ take-slice ] 2dip
88     rot check-sequence span-slices ;
89
90 :: split-slice-back ( slice n -- slice1 slice2 )
91     slice [ from>> ] [ to>> ] [ seq>> ] tri :> ( from to seq )
92     from to n - seq <slice>
93     to n - to seq <slice> ;
94
95 ! Don't include the whitespace in the slice
96 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
97     n [
98         n string [ "\s\r\n" member? ] find-from :> ( n' ch )
99         n' string
100         n n' string ?<slice>
101         ch
102     ] [
103         f string f f
104     ] if ; inline
105
106 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
107     n string quot find-from :> ( n' ch )
108     n' string
109     n n' string ?<slice>
110     ch ; inline
111
112 : slice-until ( n string quot -- n' string slice/f )
113     (slice-until) drop ; inline
114
115 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
116     n [
117         n string [ "\s\r\n" member? not ] find-from :> ( n' ch )
118         n' string
119         n n' string ?<slice>
120         ch
121     ] [
122         n string f f
123     ] if ; inline
124
125 : skip-whitespace ( n/f string -- n'/f string )
126     slice-til-not-whitespace 2drop ;
127
128 : empty-slice-end ( seq -- slice )
129     [ length dup ] [ ] bi <slice> ; inline
130
131 : empty-slice-from ( n seq -- slice )
132     dupd <slice> ; inline
133
134 :: slice-til-eol ( n string -- n' string slice/f ch/f )
135     n [
136         n string '[ "\r\n" member? ] find-from :> ( n' ch )
137         n' string
138         n n' string ?<slice>
139         ch
140     ] [
141         n string string empty-slice-end f
142     ] if ; inline
143
144 :: merge-slice-til-eol-slash'' ( n string -- n' string slice/f ch/f )
145     n [
146         n string '[ "\r\n\\" member? ] find-from :> ( n' ch )
147         n' string
148         n n' string ?<slice>
149         ch
150     ] [
151         n string string empty-slice-end f
152     ] if ; inline
153
154 : merge-slice-til-whitespace ( n string slice --  n' string slice' )
155     pick [
156         [ slice-til-whitespace drop ] dip merge-slices
157     ] when ;
158
159 : merge-slice-til-eol ( n string slice --  n' string slice' )
160     [ slice-til-eol drop ] dip merge-slices ;
161
162 : slice-between ( slice1 slice2 -- slice )
163     ! ensure-same-underlying
164     slice-order-by-from
165     [ to>> ]
166     [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
167
168 : slice-before ( slice -- slice' )
169     [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
170
171 : (?nth) ( n/f string/f -- obj/f )
172     over [ (?nth) ] [ 2drop f ] if ;
173
174 :: merge-slice-til-eol-slash' ( n string slice -- n' string slice/f ch/f )
175     n string merge-slice-til-eol-slash'' :> ( n' string' slice' ch' )
176     ch' CHAR: \\ = [
177         n' 1 + string' (?nth) "\r\n" member? [
178             n' 2 + string' slice slice' span-slices merge-slice-til-eol-slash'
179         ] [
180             "omg" throw
181         ] if
182     ] [
183         n' string' slice slice' span-slices ch'
184     ] if ;
185
186 ! Supports \ at eol (with no space after it)
187 : slice-til-eol-slash ( n string -- n' string slice/f ch/f )
188     2dup empty-slice-from merge-slice-til-eol-slash' ;
189
190 :: slice-til-separator-inclusive ( n string tokens -- n' string slice/f ch/f )
191     n string '[ tokens member? ] find-from [ dup [ 1 + ] when ] dip  :> ( n' ch )
192     n' string
193     n n' string ?<slice>
194     ch ; inline
195
196 : slice-til-separator-exclusive ( n string tokens -- n' string slice/f ch/f )
197     slice-til-separator-inclusive dup [
198         [ [ 1 - ] change-to ] dip
199     ] when ;
200
201 ! Takes at least one character if not whitespace
202 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
203     n [
204         n string '[ tokens member? ] find-from
205         dup "\s\r\n" member? [
206             :> ( n' ch )
207             n' string
208             n n' string ?<slice>
209             ch
210         ] [
211             [ dup [ 1 + ] when ] dip :> ( n' ch )
212             n' string
213             n n' string ?<slice>
214             ch
215         ] if
216     ] [
217         f string f f
218     ] if ; inline
219
220 ERROR: subseq-expected-but-got-eof n string expected ;
221
222 :: slice-til-string ( n string search --  n' string payload end-string )
223     search string n subseq-start-from :> n'
224     n' [ n string search subseq-expected-but-got-eof ] unless
225     n' search length +  string
226     n n' string ?<slice>
227     n' dup search length + string ?<slice> ;
228
229 : modify-from ( slice n -- slice' )
230     '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
231
232 : modify-to ( slice n -- slice' )
233     [ [ from>> ] [ to>> ] [ seq>> ] tri ] dip
234     swap [ + ] dip <slice> ;
235
236 ! { CHAR: \] [ read-closing ] }
237 ! { CHAR: \} [ read-closing ] }
238 ! { CHAR: \) [ read-closing ] }
239 : read-closing ( n string tok -- n string tok )
240     dup length 1 = [
241         -1 modify-to [ 1 - ] 2dip
242     ] unless ;
243
244 : rewind-slice ( n string slice -- n' string )
245     pick [
246         length swap [ - ] dip
247     ] [
248         [ nip ] dip [ [ length ] bi@ - ] keepd
249     ] if ; inline