diff --git a/basis/wrap/strings/strings-docs.factor b/basis/wrap/strings/strings-docs.factor index c8c08d3db1..adce6f0d55 100644 --- a/basis/wrap/strings/strings-docs.factor +++ b/basis/wrap/strings/strings-docs.factor @@ -14,14 +14,14 @@ ARTICLE: "wrap.strings" "String word wrapping" } ; HELP: wrap-lines -{ $values { "lines" string } { "width" integer } { "newlines" "sequence of strings" } } -{ $description "Given a string, divides it into a sequence of lines where each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ; +{ $values { "string" string } { "width" integer } { "newlines" "sequence of strings" } } +{ $description "Given a " { $snippet "string" } ", divides it into a sequence of lines where each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ; HELP: wrap-string { $values { "string" string } { "width" integer } { "newstring" string } } -{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ; +{ $description "Given a " { $snippet "string" } ", alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ; HELP: wrap-indented-string -{ $values { "string" string } { "width" integer } { "indent" string } { "newstring" string } } -{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space. Before each line, the indent string is added." } ; +{ $values { "string" string } { "width" integer } { "indent" "string or integer" } { "newstring" string } } +{ $description "Given a " { $snippet "string" } ", alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space. The " { $snippet "indent" } " can be either a " { $link string } " or a number of spaces to prepend to each line." } ; diff --git a/basis/wrap/strings/strings-tests.factor b/basis/wrap/strings/strings-tests.factor index b9abedc4c4..bf6fb6749b 100644 --- a/basis/wrap/strings/strings-tests.factor +++ b/basis/wrap/strings/strings-tests.factor @@ -14,7 +14,7 @@ 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 @@ -27,6 +27,11 @@ word wrap.""" " " 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 = +] unit-test + [ "this text\nhas lots of\nspaces" ] [ "this text has lots of spaces" 12 wrap-string ] unit-test diff --git a/basis/wrap/strings/strings.factor b/basis/wrap/strings/strings.factor index f5ab777444..0825edcc4b 100644 --- a/basis/wrap/strings/strings.factor +++ b/basis/wrap/strings/strings.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. -USING: wrap kernel sequences fry splitting math ; +USING: fry kernel math sequences splitting strings wrap ; IN: wrap.strings -: wrap-lines ( lines width -- newlines ) +: wrap-lines ( string width -- newlines ) [ split-lines ] dip '[ _ dup wrap join-elements ] map! concat ; : wrap-string ( string width -- newstring ) wrap-lines join-lines ; + ] unless ; inline + +PRIVATE> + : wrap-indented-string ( string width indent -- newstring ) - [ length - wrap-lines ] keep '[ _ prepend ] map! join-lines ; + make-indent [ length - wrap-lines ] keep + '[ _ prepend ] map! join-lines ;