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 ;
7 : >strings ( seq -- str )
8 [ dup slice? [ >string ] when ] deep-map ;
10 : matching-delimiter ( ch -- ch' )
19 : matching-delimiter-string ( string -- string' )
20 [ matching-delimiter ] map ;
22 : matching-section-delimiter ( string -- string' )
24 rest but-last ";" ">" surround
29 ERROR: unexpected-end n string ;
30 : nth-check-eof ( n string -- nth )
31 2dup ?nth [ 2nip ] [ unexpected-end ] if* ;
33 : peek-from ( n/f string -- ch )
34 over [ ?nth ] [ 2drop f ] if ;
36 : previous-from ( n/f string -- ch )
37 over [ [ 1 - ] dip ?nth ] [ 2drop f ] if ;
40 : next-char-from ( n/f string -- n'/f string ch/f )
42 2dup ?nth [ [ 1 + ] 2dip ] [ f ] if*
44 [ 2drop f ] [ nip ] 2bi f
47 : prev-char-from-slice-end ( slice -- ch/f )
48 [ to>> 2 - ] [ seq>> ] bi ?nth ;
50 : prev-char-from-slice ( slice -- ch/f )
51 [ from>> 1 - ] [ seq>> ] bi ?nth ;
53 : next-char-from-slice ( slice -- ch/f )
54 [ to>> ] [ seq>> ] bi ?nth ;
56 : char-before-slice ( slice -- ch/f )
57 [ from>> 1 - ] [ seq>> ] bi ?nth ;
59 : char-after-slice ( slice -- ch/f )
60 [ to>> ] [ seq>> ] bi ?nth ;
62 : find-from* ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt ? )
64 pick [ drop t ] [ length -rot nip f ] if ; inline
66 : skip-blank-from ( n string -- n' string )
68 [ [ blank? not ] find-from* 2drop ] keep
71 : skip-til-eol-from ( n string -- n' string )
72 [ [ "\r\n" member? ] find-from* 2drop ] keep ; inline
74 :: take-slice ( n string count -- n' string slice )
80 ERROR: expected-sequence-error expected actual ;
81 : check-sequence ( expected actual -- actual/* )
82 2dup sequence= [ nip ] [ expected-sequence-error ] if ;
84 : expect-and-span ( n string slice expected-string -- n' string slice' )
85 dup length '[ _ take-slice ] 2dip
86 rot check-sequence span-slices ;
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> ;
93 ! Don't include the whitespace in the slice
94 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
96 n string [ "\s\r\n" member? ] find-from :> ( n' ch )
104 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
105 n string quot find-from :> ( n' ch )
110 : slice-until ( n string quot -- n' string slice/f )
111 (slice-until) drop ; inline
113 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
115 n string [ "\s\r\n" member? not ] find-from :> ( n' ch )
123 : skip-whitespace ( n/f string -- n'/f string )
124 slice-til-not-whitespace 2drop ;
126 : empty-slice-end ( seq -- slice )
127 [ length dup ] [ ] bi <slice> ; inline
129 : empty-slice-from ( n seq -- slice )
130 dupd <slice> ; inline
132 :: slice-til-eol ( n string -- n' string slice/f ch/f )
134 n string '[ "\r\n" member? ] find-from :> ( n' ch )
139 n string string empty-slice-end f
142 :: merge-slice-til-eol-slash'' ( n string -- n' string slice/f ch/f )
144 n string '[ "\r\n\\" member? ] find-from :> ( n' ch )
149 n string string empty-slice-end f
152 : merge-slice-til-whitespace ( n string slice -- n' string slice' )
154 [ slice-til-whitespace drop ] dip merge-slices
157 : merge-slice-til-eol ( n string slice -- n' string slice' )
158 [ slice-til-eol drop ] dip merge-slices ;
160 : slice-between ( slice1 slice2 -- slice )
161 ! ensure-same-underlying
164 [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
166 : slice-before ( slice -- slice' )
167 [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
169 : (?nth) ( n/f string/f -- obj/f )
170 over [ (?nth) ] [ 2drop f ] if ;
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' )
175 n' 1 + string' (?nth) "\r\n" member? [
176 n' 2 + string' slice slice' span-slices merge-slice-til-eol-slash'
181 n' string' slice slice' span-slices ch'
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' ;
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 )
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
199 ! Takes at least one character if not whitespace
200 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
202 n string '[ tokens member? ] find-from
203 dup "\s\r\n" member? [
209 [ dup [ 1 + ] when ] dip :> ( n' ch )
218 ERROR: subseq-expected-but-got-eof n string expected ;
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
225 n' dup search length + string ?<slice> ;
227 : modify-from ( slice n -- slice' )
228 '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
230 : modify-to ( slice n -- slice' )
231 [ [ from>> ] [ to>> ] [ seq>> ] tri ] dip
232 swap [ + ] dip <slice> ;
234 ! { CHAR: \] [ read-closing ] }
235 ! { CHAR: \} [ read-closing ] }
236 ! { CHAR: \) [ read-closing ] }
237 : read-closing ( n string tok -- n string tok )
239 -1 modify-to [ 1 - ] 2dip
242 : rewind-slice ( n string slice -- n' string )
244 length swap [ - ] dip
246 [ nip ] dip [ [ length ] bi@ - ] keepd