]> gitweb.factorcode.org Git - factor.git/commitdiff
wrap.strings: preserve leading whitespace when wrapping.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 16 Mar 2021 22:08:53 +0000 (15:08 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 16 Mar 2021 22:08:53 +0000 (15:08 -0700)
basis/wrap/strings/strings-tests.factor
basis/wrap/strings/strings.factor

index a28dad2b8334b9e50ccf487f67b3740e230f1545..81142a1934fa90e2878ec6bb5e8f3297e680e946 100644 (file)
@@ -26,6 +26,30 @@ word wrap."
     "  " wrap-indented-string
 ] unit-test
 
+{
+    "   This is
+a long
+piece of
+text that
+we wish to
+word wrap."
+} [
+    "  This is a long piece of text that we wish to word wrap." 10
+    wrap-string
+] unit-test
+
+{
+    "     This is
+  a long
+  piece of
+  text that
+  we wish to
+  word wrap."
+} [
+    "  This is a long piece of text that we wish to word wrap." 12
+    "  " wrap-indented-string
+] unit-test
+
 { t } [
     "This is a long piece of text that we wish to word wrap." 12
     [ "  " wrap-indented-string ] [ 2 wrap-indented-string ] 2bi =
index fa9d0f04d65d30aa94658e72454333eeaceb39d7..bcc49ac8949d59d257f46aea4a9318aaa9664157 100644 (file)
@@ -5,11 +5,14 @@ IN: wrap.strings
 
 <PRIVATE
 
+: 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* ;
+
 : split-lines ( string -- elements-lines )
-    string-lines [
-        " \t" split harvest
-        [ dup length 1 <element> ] map!
-    ] map! ;
+    string-lines [ split-line ] map! ;
 
 : join-elements ( wrapped-lines -- lines )
     [ " " join ] map! ;