Michael Karnerfors created TEXT-231:
---------------------------------------

             Summary: WordUtils.wrap should react to prexisting "new line 
string" as a wrap
                 Key: TEXT-231
                 URL: https://issues.apache.org/jira/browse/TEXT-231
             Project: Commons Text
          Issue Type: Improvement
    Affects Versions: 1.10.0
            Reporter: Michael Karnerfors


WordUtils.wrap ignores pre-existing occurrences of the "new line string" and 
counts them as part of a line, instead of as a wrap.

Example: 

 
{code:java}
public static final String LINE_SEPARATOR = "\n"; 

void wrap() {
    String line = "Alpha"+ LINE_SEPARATOR + "Bravo Charlie Delta Echo Foxtrot";
    System.out.println(WordUtils.wrap(line, 13));
} {code}
The default new line string is just newline ("\n"). So in this case I would 
expect this to output...

 
{noformat}
Alpha
Bravo Charlie
Delta Echo 
Foxtrot
{noformat}
 

 

However, since WordUtils.wrap does not consider the pre-existing newline after 
"Alpha" as one of its own wraps, I instead get...

 
{noformat}
Alpha
Bravo
Charlie Delta
Echo Foxtrot
{noformat}

There is a work-around, but it is not as elegant...

 
{code:java}
String wrappedLine = 
    Arrays
        .stream(
            line.split(LINE_SEPARATOR)
        )
        .map(
            subLine -> WordUtils.wrap(subLine, 13)
        )
        .collect(
            Collectors.joining(LINE_SEPARATOR)
        );
System.out.println(wrappedLine);
{code}
 

Hence, I suggest that WordUtils.wrap should consider matches of "newLineStr" as 
a wrap, and any directly following text as the beginning of the next line. 

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to