Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEXT-39 WordUtils should use toXxxxCase(int) rather than toXxxxCase(char) #39

Merged
merged 3 commits into from May 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
101 changes: 74 additions & 27 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 < strLen;) {
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 < strLen;) {
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;) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this is what I was thinking.

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 @@ -727,7 +753,7 @@ public static boolean containsAllWords(final CharSequence word, final CharSequen
* @param delimiters the delimiters
* @return true if it is a delimiter
*/
private static boolean isDelimiter(final char ch, final char[] delimiters) {
public static boolean isDelimiter(final char ch, final char[] delimiters) {
if (delimiters == null) {
return Character.isWhitespace(ch);
}
Expand All @@ -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
*/
public 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