]> gitweb.factorcode.org Git - factor.git/blob - basis/wrap/strings/strings.factor
factor: trim using lists
[factor.git] / basis / wrap / strings / strings.factor
1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences splitting strings wrap ;
4 IN: wrap.strings
5
6 <PRIVATE
7
8 : wrap-split-line ( string -- elements )
9     dup [ " \t" member? not ] find drop 0 or
10     [ f swap ] [ cut ] if-zero
11     " \t" split harvest [ dup length 1 <element> ] map!
12     swap [ 0 over length <element> prefix ] when* ;
13
14 : wrap-split-lines ( string -- elements-lines )
15     split-lines [ wrap-split-line ] map! ;
16
17 : join-elements ( wrapped-lines -- lines )
18     [ join-words ] map! ;
19
20 PRIVATE>
21
22 : wrap-lines ( string width -- newlines )
23     [ wrap-split-lines ] dip '[ _ wrap join-elements ] map! concat ;
24
25 : wrap-string ( string width -- newstring )
26     wrap-lines join-lines ;
27
28 <PRIVATE
29
30 : make-indent ( indent -- indent' )
31     dup string? [ CHAR: \s <string> ] unless ; inline
32
33 PRIVATE>
34
35 : wrap-indented-string ( string width indent -- newstring )
36     make-indent [ length - wrap-lines ] keep
37     over empty? [ nip ] [ '[ _ prepend ] map! join-lines ] if ;