public static String unwrap()

in mailet/standard/src/main/java/org/apache/james/transport/mailets/UnwrapText.java [96:179]


    public static String unwrap(String text, int qwidth) {
        String[] lines = text.split("\r\n|\n", -1);
        
        //P1: Manage spaces without trims
        Pattern p1 = Pattern.compile("([> ]*)(.*[^ .?!][ ]*)$", 0);
        
        //P2: Quotation char at the begin of a line and the first word starts with a lowercase char or a number. The word ends with a space, a tab or a lineend. 
        Pattern p2 = Pattern.compile("^([> ]*)(([a-zàèéìòù][^ \t\r\n]*|[0-9][0-9,.]*)([ \t].*$|$))", 0);
        
        // Width computation
        int width = 0;
        for (int i = 0; i < lines.length - 1; i++) {
            String l = lines[i].trim();
            if (l.length() > width) {
                width = l.length();
            }
        }
        
        if (width < 40) {
            return text;
        }
        if (qwidth < 0) {
            qwidth = width - qwidth;
        }
        
        StringBuilder result = new StringBuilder();
        int prevWrapped = 0;
        for (int i = 0; i < lines.length; i++) {
            if (prevWrapped != 0) {
                if (prevWrapped > 0) {
                    if (result.charAt(result.length() - 1) != ' ') {
                        result.append(" ");
                    }
                } else {
                    result.append("\r\n");
                }
            }
            String l = lines[i];
            Matcher m1 = p1.matcher(l);
            Matcher m2 = i < lines.length - 1 ? p2.matcher(lines[i + 1]) : null;
            boolean b;
            int w;
            // if patterns match, the quote level are identical and if the line length added to the length of the following word is greater than width then it is a wrapped line.
            if (m1.matches() && i < lines.length - 1 && m2.matches() && (
                    // The following line has the same quoting of the previous.
                    ((b = m1.group(1).trim().equals(m2.group(1).trim())) && l.length() + m2.group(3).length() + 1 > width)
                    ||
                    // The following line has no quoting (while the previous yes)
                    (!b && m2.group(1).trim().equals("") && (w = l.length() + m2.group(2).trim().length() + 1) > width && w <= qwidth)
                )) {
                
                if (b) {
                    if (prevWrapped > 0 && m1.groupCount() >= 2) {
                        result.append(m1.group(2));
                    } else {
                        result.append(l);
                    }
                    prevWrapped = 1;
                    
                } else {
                    lines[i + 1] = l + (l.charAt(l.length() - 1) != ' ' ? " " : "") + m2.group(2).trim();
                    // Revert the previous append
                    if (prevWrapped != 0) {
                        if (prevWrapped > 0) {
                            result.deleteCharAt(result.length() - 1);
                        } else {
                            result.delete(result.length() - 2, result.length());
                        }
                    }
                }
                
            } else {
                Matcher m3 = p2.matcher(l);
                if (prevWrapped > 0 && m3.matches()) {
                    result.append(m3.group(2));
                } else {
                    result.append(lines[i]);
                }
                prevWrapped = -1;
            }
        }
        
        return result.toString();
    }