Skip to content

Commit

Permalink
Fixed TEXT-39 WordUtils now using toXxxxCase(int) rather than toXxxxC…
Browse files Browse the repository at this point in the history
…ase(char)
  • Loading branch information
ameyjadiye committed May 5, 2017
1 parent f651b78 commit 18969d7
Showing 1 changed file with 73 additions and 26 deletions.
99 changes: 73 additions & 26 deletions src/main/java/org/apache/commons/text/WordUtils.java
Expand Up @@ -414,18 +414,29 @@ public static String capitalize(final String str, final char... delimiters) {
if (StringUtils.isEmpty(str) || delimLen == 0) {
return str;
}
final char[] buffer = str.toCharArray();
int strLen = str.length();
int [] newCodePoints = new int[strLen];
int outOffset = 0;

boolean capitalizeNext = true;
for (int i = 0; i < buffer.length; i++) {
final char ch = buffer[i];
if (isDelimiter(ch, delimiters)) {
for (int index = 0; index < str.length();) {
final int codePoint = str.codePointAt(index);

if (isDelimiter(codePoint, delimiters)) {
capitalizeNext = true;
newCodePoints[outOffset++] = codePoint;
index += Character.charCount(codePoint);
} else if (capitalizeNext) {
buffer[i] = Character.toTitleCase(ch);
int titleCaseCodePoint = Character.toTitleCase(codePoint);
newCodePoints[outOffset++] = titleCaseCodePoint;
index += Character.charCount(titleCaseCodePoint);
capitalizeNext = false;
} else {
newCodePoints[outOffset++] = codePoint;
index += Character.charCount(codePoint);
}
}
return new String(buffer);
return new String(newCodePoints, 0, outOffset);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -537,18 +548,29 @@ public static String uncapitalize(final String str, final char... delimiters) {
if (StringUtils.isEmpty(str) || delimLen == 0) {
return str;
}
final char[] buffer = str.toCharArray();
int strLen = str.length();
int [] newCodePoints = new int[strLen];
int outOffset = 0;

boolean uncapitalizeNext = true;
for (int i = 0; i < buffer.length; i++) {
final char ch = buffer[i];
if (isDelimiter(ch, delimiters)) {
for (int index = 0; index < str.length();) {
final int codePoint = str.codePointAt(index);

if (isDelimiter(codePoint, delimiters)) {
uncapitalizeNext = true;
newCodePoints[outOffset++] = codePoint;
index += Character.charCount(codePoint);
} else if (uncapitalizeNext) {
buffer[i] = Character.toLowerCase(ch);
int titleCaseCodePoint = Character.toLowerCase(codePoint);
newCodePoints[outOffset++] = titleCaseCodePoint;
index += Character.charCount(titleCaseCodePoint);
uncapitalizeNext = false;
} else {
newCodePoints[outOffset++] = codePoint;
index += Character.charCount(codePoint);
}
}
return new String(buffer);
return new String(newCodePoints, 0, outOffset);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -578,30 +600,34 @@ public static String swapCase(final String str) {
if (StringUtils.isEmpty(str)) {
return str;
}
final char[] buffer = str.toCharArray();

final int strLen = str.length();
int [] newCodePoints = new int[strLen];
int outOffset = 0;
boolean whitespace = true;

for (int i = 0; i < buffer.length; i++) {
final char ch = buffer[i];
if (Character.isUpperCase(ch)) {
buffer[i] = Character.toLowerCase(ch);
for (int index = 0; index < strLen;) {
final int oldCodepoint = str.codePointAt(index);
final int newCodePoint;
if (Character.isUpperCase(oldCodepoint)) {
newCodePoint = Character.toLowerCase(oldCodepoint);
whitespace = false;
} else if (Character.isTitleCase(ch)) {
buffer[i] = Character.toLowerCase(ch);
} else if (Character.isTitleCase(oldCodepoint)) {
newCodePoint = Character.toLowerCase(oldCodepoint);
whitespace = false;
} else if (Character.isLowerCase(ch)) {
} else if (Character.isLowerCase(oldCodepoint)) {
if (whitespace) {
buffer[i] = Character.toTitleCase(ch);
newCodePoint = Character.toTitleCase(oldCodepoint);
whitespace = false;
} else {
buffer[i] = Character.toUpperCase(ch);
newCodePoint = Character.toUpperCase(oldCodepoint);
}
} else {
whitespace = Character.isWhitespace(ch);
whitespace = Character.isWhitespace(oldCodepoint);
newCodePoint = oldCodepoint;
}
newCodePoints[outOffset++] = newCodePoint;
index += Character.charCount(newCodePoint);
}
return new String(buffer);
return new String(newCodePoints, 0, outOffset);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -739,6 +765,27 @@ private static boolean isDelimiter(final char ch, final char[] delimiters) {
return false;
}

//-----------------------------------------------------------------------
/**
* Is the codePoint a delimiter.
*
* @param codePoint the codePint to check
* @param delimiters the delimiters
* @return true if it is a delimiter
*/
private static boolean isDelimiter(final int codePoint, final char[] delimiters) {
if (delimiters == null) {
return Character.isWhitespace(codePoint);
}
for (int index = 0; index < delimiters.length; index++) {
int delimiterCodePoint = Character.codePointAt(delimiters, index);
if (delimiterCodePoint == codePoint) {
return true;
}
}
return false;
}

//-----------------------------------------------------------------------
/**
* Abbreviates the words nicely.
Expand Down

0 comments on commit 18969d7

Please sign in to comment.