vlsi commented on a change in pull request #694: URL: https://github.com/apache/jmeter/pull/694#discussion_r790945001
########## File path: src/jorphan/src/main/java/org/apache/jorphan/util/StringWrap.java ########## @@ -0,0 +1,166 @@ +/* + * 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.apache.jorphan.util; + +import java.text.BreakIterator; + +import org.apiguardian.api.API; + +/** + * Wraps text in such a way so the lines do not exceed given maximum length. + */ +@API(since = "5.5", status = API.Status.EXPERIMENTAL) +public class StringWrap { + private final int minWrap; + private final int maxWrap; + + private final BreakCursor wordCursor = new BreakCursor(BreakIterator.getLineInstance()); + private final BreakCursor charCursor = new BreakCursor(BreakIterator.getCharacterInstance()); + + /** + * Stores the current and the next position for a given {@link BreakIterator}. + * It allows reducing the number of calls to {@link BreakIterator}. + */ + private static class BreakCursor { + private static final int UNINITIALIZED = -2; + + private final BreakIterator iterator; + private int pos; + private int next; + + BreakCursor(BreakIterator iterator) { + this.iterator = iterator; + } + + void setText(String text) { + iterator.setText(text); + pos = 0; + next = UNINITIALIZED; + } + + public int getPos() { + return pos; + } + + /** + * Advances the cursor if possible. + * @param startWrap the start index of the wrap to consider + * @param endWrap the end index of the wrap to consider + * @return true if the next break is detected within startWrap..endWrap boundaries + */ + public boolean advance(int startWrap, int endWrap) { + if (pos == BreakIterator.DONE || pos > endWrap) { + return false; + } + pos = next != UNINITIALIZED ? next : iterator.following(startWrap); + if (pos == BreakIterator.DONE || pos > endWrap) { + return false; + } + // Try adding more items up to endWrap + while (true) { + next = iterator.next(); + if (next == BreakIterator.DONE || next > endWrap) { + break; + } + pos = next; + } + return true; + } + } + + /** + * Creates string wrapper instance. + * + * @param minWrap minimal word length for the wrap + * @param maxWrap maximum word length for the wrap + */ + public StringWrap(int minWrap, int maxWrap) { + this.minWrap = minWrap; + this.maxWrap = maxWrap; + } + + public int getMinWrap() { + return minWrap; + } + + public int getMaxWrap() { + return maxWrap; + } + + /** + * Wraps given {@code input} text accoding to + * + * @param input input text + * @param delimiter delimiter when inserting soft wraps + * @return modified text with added soft wraps, or input if wraps are not needed + */ + public String wrap(String input, String delimiter) { + if (input.length() <= minWrap) { + return input; + } + wordCursor.setText(input); + charCursor.setText(input); + int pos = 0; + StringBuilder sb = new StringBuilder(input.length() + input.length() / minWrap * delimiter.length()); + boolean hasChanges = false; + int nextLineSeparator = -2; Review comment: ah, indeed, this should be `BreakCursor.UNITIALIZED` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
