Repository: commons-text Updated Branches: refs/heads/master 0b6a285bb -> 4d84a33e4
port changes from commons-lang WordUtils to commons-text WordUtils Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/c0f7877b Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/c0f7877b Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/c0f7877b Branch: refs/heads/master Commit: c0f7877b0e72b0b27fbd14c02da315e3247a2e95 Parents: 0b6a285 Author: Pascal Schumacher <pascalschumac...@gmx.net> Authored: Thu May 4 12:30:02 2017 +0200 Committer: Pascal Schumacher <pascalschumac...@gmx.net> Committed: Thu May 4 12:30:02 2017 +0200 ---------------------------------------------------------------------- .../java/org/apache/commons/text/WordUtils.java | 36 +++++++++++--------- .../org/apache/commons/text/WordUtilsTest.java | 8 +++++ 2 files changed, 28 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/c0f7877b/src/main/java/org/apache/commons/text/WordUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/WordUtils.java b/src/main/java/org/apache/commons/text/WordUtils.java index 0cee2f6..f4a4c8e 100644 --- a/src/main/java/org/apache/commons/text/WordUtils.java +++ b/src/main/java/org/apache/commons/text/WordUtils.java @@ -16,10 +16,12 @@ */ package org.apache.commons.text; -import java.lang.reflect.Array; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; + /** * <p>Operations on Strings that contain words.</p> * @@ -31,8 +33,6 @@ import java.util.regex.Pattern; */ public class WordUtils { - private static final int THIRTY_TWO = 32; - /** * <p><code>WordUtils</code> instances should NOT be constructed in * standard programming. Instead, the class should be used as @@ -108,7 +108,7 @@ public class WordUtils { * <table border="1" summary="Wrap Results"> * <tr> * <th>input</th> - * <th>wrapLenght</th> + * <th>wrapLength</th> * <th>newLineString</th> * <th>wrapLongWords</th> * <th>result</th> @@ -189,7 +189,7 @@ public class WordUtils { * <table border="1" summary="Wrap Results"> * <tr> * <th>input</th> - * <th>wrapLenght</th> + * <th>wrapLength</th> * <th>newLineString</th> * <th>wrapLongWords</th> * <th>wrapOn</th> @@ -280,18 +280,18 @@ public class WordUtils { return null; } if (newLineStr == null) { - newLineStr = System.getProperty("line.separator"); + newLineStr = System.lineSeparator(); } if (wrapLength < 1) { wrapLength = 1; } - if (wrapOn == null || wrapOn.length() == 0 || wrapOn.trim().length() == 0) { + if (StringUtils.isBlank(wrapOn)) { wrapOn = " "; } final Pattern patternToWrapOn = Pattern.compile(wrapOn); final int inputLineLength = str.length(); int offset = 0; - final StringBuilder wrappedLine = new StringBuilder(inputLineLength + THIRTY_TWO); + final StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32); while (offset < inputLineLength) { int spaceToWrapAt = -1; @@ -301,8 +301,9 @@ public class WordUtils { if (matcher.start() == 0) { offset += matcher.end(); continue; + } else { + spaceToWrapAt = matcher.start() + offset; } - spaceToWrapAt = matcher.start(); } // only last line without leading spaces is left @@ -410,7 +411,7 @@ public class WordUtils { */ public static String capitalize(final String str, final char... delimiters) { final int delimLen = delimiters == null ? -1 : delimiters.length; - if (str == null || str.length() == 0 || delimLen == 0) { + if (StringUtils.isEmpty(str) || delimLen == 0) { return str; } final char[] buffer = str.toCharArray(); @@ -478,7 +479,7 @@ public class WordUtils { */ public static String capitalizeFully(String str, final char... delimiters) { final int delimLen = delimiters == null ? -1 : delimiters.length; - if (str == null || str.length() == 0 || delimLen == 0) { + if (StringUtils.isEmpty(str) || delimLen == 0) { return str; } str = str.toLowerCase(); @@ -533,7 +534,7 @@ public class WordUtils { */ public static String uncapitalize(final String str, final char... delimiters) { final int delimLen = delimiters == null ? -1 : delimiters.length; - if (str == null || str.length() == 0 || delimLen == 0) { + if (StringUtils.isEmpty(str) || delimLen == 0) { return str; } final char[] buffer = str.toCharArray(); @@ -574,7 +575,7 @@ public class WordUtils { * @return the changed String, <code>null</code> if null String input */ public static String swapCase(final String str) { - if (str == null || str.length() == 0) { + if (StringUtils.isEmpty(str)) { return str; } final char[] buffer = str.toCharArray(); @@ -623,6 +624,7 @@ public class WordUtils { * @param str the String to get initials from, may be null * @return String of initial letters, <code>null</code> if null String input * @see #initials(String,char[]) + * @since 2.2 */ public static String initials(final String str) { return initials(str, null); @@ -652,9 +654,10 @@ public class WordUtils { * @param delimiters set of characters to determine words, null means whitespace * @return String of initial characters, <code>null</code> if null String input * @see #initials(String) + * @since 2.2 */ public static String initials(final String str, final char... delimiters) { - if (str == null || str.length() == 0) { + if (StringUtils.isEmpty(str)) { return str; } if (delimiters != null && delimiters.length == 0) { @@ -701,13 +704,14 @@ public class WordUtils { * @param word The CharSequence to check, may be null * @param words The array of String words to search for, may be null * @return {@code true} if all search words are found, {@code false} otherwise + * @since 3.5 */ public static boolean containsAllWords(final CharSequence word, final CharSequence... words) { - if (word == null || word.length() == 0 || words == null || Array.getLength(words) == 0) { + if (StringUtils.isEmpty(word) || ArrayUtils.isEmpty(words)) { return false; } for (final CharSequence w : words) { - if (w == null || w.length() == 0 || String.valueOf(w).trim().length() == 0) { + if (StringUtils.isBlank(w)) { return false; } final Pattern p = Pattern.compile(".*\\b" + w + "\\b.*"); http://git-wip-us.apache.org/repos/asf/commons-text/blob/c0f7877b/src/test/java/org/apache/commons/text/WordUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/WordUtilsTest.java b/src/test/java/org/apache/commons/text/WordUtilsTest.java index 0ab8b09..ced2716 100644 --- a/src/test/java/org/apache/commons/text/WordUtilsTest.java +++ b/src/test/java/org/apache/commons/text/WordUtilsTest.java @@ -417,4 +417,12 @@ public class WordUtilsTest { assertEquals(expect, WordUtils.swapCase(test)); } + @Test + public void testLANG1292() throws Exception { + // Prior to fix, this was throwing StringIndexOutOfBoundsException + WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",70); + } + }