All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tomitribe.crest.help.Wrap Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.tomitribe.crest.help;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Wrap {

    private Wrap() {
    }

    public static int length(final CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen = length(cs);
        if (strLen == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 

Wraps a single line of text, identifying words by {@code ' '}.

* *

New lines will be separated by the system property line separator. * Very long words, such as URLs will not be wrapped.

* *

Leading spaces on a new line are stripped. * Trailing spaces are not stripped.

* * @param str the String to be word wrapped, may be null * @param wrapLength the column to wrap the words at, less than 1 is treated as 1 * @return a line with newlines inserted, {@code null} if null input */ public static String wrap(final String str, final int wrapLength) { return wrap(str, wrapLength, null, false); } /** *

Wraps a single line of text, identifying words by {@code ' '}.

* *

Leading spaces on a new line are stripped. Trailing spaces are not stripped.

* * @param str the String to be word wrapped, may be null * @param wrapLength the column to wrap the words at, less than 1 is treated as 1 * @param newLineStr the string to insert for a new line, * {@code null} uses the system property line separator * @param wrapLongWords true if long words (such as URLs) should be wrapped * @return a line with newlines inserted, {@code null} if null input **/ public static String wrap(final String str, final int wrapLength, final String newLineStr, final boolean wrapLongWords) { return wrap(str, wrapLength, newLineStr, wrapLongWords, " "); } /** *

Wraps a single line of text, identifying words by {@code wrapOn}.

* *

Leading spaces on a new line are stripped. * Trailing spaces are not stripped.

* * @param str the String to be word wrapped, may be null * @param wrapLength the column to wrap the words at, less than 1 is treated as 1 * @param newLineStr the string to insert for a new line, * {@code null} uses the system property line separator * @param wrapLongWords true if long words (such as URLs) should be wrapped * @param wrapOn regex expression to be used as a breakable characters, * if blank string is provided a space character will be used * @return a line with newlines inserted, {@code null} if null input */ public static String wrap(final String str, int wrapLength, String newLineStr, final boolean wrapLongWords, String wrapOn) { if (str == null) { return null; } if (newLineStr == null) { newLineStr = System.lineSeparator(); } if (wrapLength < 1) { wrapLength = 1; } if (isBlank(wrapOn)) { wrapOn = " "; } final Pattern patternToWrapOn = Pattern.compile(wrapOn); final int inputLineLength = str.length(); int offset = 0; final StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32); while (offset < inputLineLength) { int spaceToWrapAt = -1; Matcher matcher = patternToWrapOn.matcher( str.substring(offset, Math.min((int) Math.min(Integer.MAX_VALUE, offset + wrapLength + 1L), inputLineLength))); if (matcher.find()) { if (matcher.start() == 0) { offset += matcher.end(); continue; } spaceToWrapAt = matcher.start() + offset; } // only last line without leading spaces is left if (inputLineLength - offset <= wrapLength) { break; } while (matcher.find()) { spaceToWrapAt = matcher.start() + offset; } if (spaceToWrapAt >= offset) { // normal case wrappedLine.append(str, offset, spaceToWrapAt); wrappedLine.append(newLineStr); offset = spaceToWrapAt + 1; } else { // really long word or URL if (wrapLongWords) { // wrap really long word one line at a time wrappedLine.append(str, offset, wrapLength + offset); wrappedLine.append(newLineStr); offset += wrapLength; } else { // do not wrap really long word, just extend beyond limit matcher = patternToWrapOn.matcher(str.substring(offset + wrapLength)); if (matcher.find()) { spaceToWrapAt = matcher.start() + offset + wrapLength; } if (spaceToWrapAt >= 0) { wrappedLine.append(str, offset, spaceToWrapAt); wrappedLine.append(newLineStr); offset = spaceToWrapAt + 1; } else { wrappedLine.append(str, offset, str.length()); offset = inputLineLength; } } } } // Whatever is left in line is short enough to just pass through wrappedLine.append(str, offset, str.length()); return wrappedLine.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy