]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/wrap/strings/strings.factor
factor: trim using lists
[factor.git] / basis / wrap / strings / strings.factor
index ed3fc3967e16da6922c9885d90c86e9990ce3dbb..56863cdddaf8585e6f43e19684cf70aad3d00bac 100644 (file)
@@ -1,26 +1,26 @@
 ! Copyright (C) 2009 Daniel Ehrenberg
 ! See http://factorcode.org/license.txt for BSD license.
-USING: fry kernel math sequences splitting strings wrap ;
+USING: kernel math sequences splitting strings wrap ;
 IN: wrap.strings
 
 <PRIVATE
 
-: split-lines ( string -- elements-lines )
-    string-lines [
-        " \t" split harvest
-        [ dup length 1 <element> ] map!
-    ] map! ;
+: wrap-split-line ( string -- elements )
+    dup [ " \t" member? not ] find drop 0 or
+    [ f swap ] [ cut ] if-zero
+    " \t" split harvest [ dup length 1 <element> ] map!
+    swap [ 0 over length <element> prefix ] when* ;
 
-: join-elements ( wrapped-lines -- lines )
-    [ " " join ] map! ;
+: wrap-split-lines ( string -- elements-lines )
+    split-lines [ wrap-split-line ] map! ;
 
-: join-lines ( strings -- string )
-    "\n" join ;
+: join-elements ( wrapped-lines -- lines )
+    [ join-words ] map! ;
 
 PRIVATE>
 
 : wrap-lines ( string width -- newlines )
-    [ split-lines ] dip '[ _ dup wrap join-elements ] map! concat ;
+    [ wrap-split-lines ] dip '[ _ wrap join-elements ] map! concat ;
 
 : wrap-string ( string width -- newstring )
     wrap-lines join-lines ;