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 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
82 ERROR: expected-sequence-error expected actual ;
83 : check-sequence ( expected actual -- actual/* )
84 2dup sequence= [ nip ] [ expected-sequence-error ] if ;
86 : expect-and-span ( n string slice expected-string -- n' string slice' )
87 dup length '[ _ take-slice ] 2dip
88 rot check-sequence span-slices ;
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> ;
95 ! Don't include the whitespace in the slice
96 :: slice-til-whitespace ( n string -- n' string slice/f ch/f )
98 n string [ "\s\r\n" member? ] find-from :> ( n' ch )
106 :: (slice-until) ( n string quot -- n' string slice/f ch/f )
107 n string quot find-from :> ( n' ch )
112 : slice-until ( n string quot -- n' string slice/f )
113 (slice-until) drop ; inline
115 :: slice-til-not-whitespace ( n string -- n' string slice/f ch/f )
117 n string [ "\s\r\n" member? not ] find-from :> ( n' ch )
125 : skip-whitespace ( n/f string -- n'/f string )
126 slice-til-not-whitespace 2drop ;
128 : empty-slice-end ( seq -- slice )
129 [ length dup ] [ ] bi <slice> ; inline
131 : empty-slice-from ( n seq -- slice )
132 dupd <slice> ; inline
134 :: slice-til-eol ( n string -- n' string slice/f ch/f )
136 n string '[ "\r\n" member? ] find-from :> ( n' ch )
141 n string string empty-slice-end f
144 :: merge-slice-til-eol-slash'' ( n string -- n' string slice/f ch/f )
146 n string '[ "\r\n\\" member? ] find-from :> ( n' ch )
151 n string string empty-slice-end f
154 : merge-slice-til-whitespace ( n string slice -- n' string slice' )
156 [ slice-til-whitespace drop ] dip merge-slices
159 : merge-slice-til-eol ( n string slice -- n' string slice' )
160 [ slice-til-eol drop ] dip merge-slices ;
162 : slice-between ( slice1 slice2 -- slice )
163 ! ensure-same-underlying
166 [ [ from>> 2dup < [ swap ] unless ] [ seq>> ] bi ] bi* <slice> ;
168 : slice-before ( slice -- slice' )
169 [ drop 0 ] [ from>> ] [ seq>> ] tri <slice> ;
171 : (?nth) ( n/f string/f -- obj/f )
172 over [ (?nth) ] [ 2drop f ] if ;
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' )
177 n' 1 + string' (?nth) "\r\n" member? [
178 n' 2 + string' slice slice' span-slices merge-slice-til-eol-slash'
183 n' string' slice slice' span-slices ch'
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' ;
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 )
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
201 ! Takes at least one character if not whitespace
202 :: slice-til-either ( n string tokens -- n'/f string slice/f ch/f )
204 n string '[ tokens member? ] find-from
205 dup "\s\r\n" member? [
211 [ dup [ 1 + ] when ] dip :> ( n' ch )
220 ERROR: subseq-expected-but-got-eof n string expected ;
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
227 n' dup search length + string ?<slice> ;
229 : modify-from ( slice n -- slice' )
230 '[ from>> _ + ] [ to>> ] [ seq>> ] tri <slice> ;
232 : modify-to ( slice n -- slice' )
233 [ [ from>> ] [ to>> ] [ seq>> ] tri ] dip
234 swap [ + ] dip <slice> ;
236 ! { CHAR: \] [ read-closing ] }
237 ! { CHAR: \} [ read-closing ] }
238 ! { CHAR: \) [ read-closing ] }
239 : read-closing ( n string tok -- n string tok )
241 -1 modify-to [ 1 - ] 2dip
244 : rewind-slice ( n string slice -- n' string )
246 length swap [ - ] dip
248 [ nip ] dip [ [ length ] bi@ - ] keepd