Added unicode support.
This commit is contained in:
parent
9d7316d7cb
commit
de9351746b
1 changed files with 19 additions and 21 deletions
|
@ -36,34 +36,32 @@ import rife.template.ValueRenderer;
|
||||||
public class SwapCase implements ValueRenderer {
|
public class SwapCase implements ValueRenderer {
|
||||||
/**
|
/**
|
||||||
* Swaps the case of a String.
|
* Swaps the case of a String.
|
||||||
* @param s the String to swap the case of
|
*
|
||||||
|
* @param src the String to swap the case of
|
||||||
* @return the modified String or null
|
* @return the modified String or null
|
||||||
*/
|
*/
|
||||||
public static String swapCase(String s) {
|
public static String swapCase(final String src) {
|
||||||
if (s == null || s.isEmpty()) {
|
if (src == null || src.isEmpty()) {
|
||||||
return s;
|
return src;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = s.toCharArray();
|
int offset = 0;
|
||||||
var whitespace = true;
|
var len = src.length();
|
||||||
|
var buff = new int[len];
|
||||||
for (var i = 0; i < buffer.length; i++) {
|
for (var i = 0; i < len; ) {
|
||||||
var ch = buffer[i];
|
int newCodePoint;
|
||||||
if (Character.isUpperCase(ch) || Character.isTitleCase(ch)) {
|
var curCodePoint = src.codePointAt(i);
|
||||||
buffer[i] = Character.toLowerCase(ch);
|
if (Character.isUpperCase(curCodePoint) || Character.isTitleCase(curCodePoint)) {
|
||||||
whitespace = false;
|
newCodePoint = Character.toLowerCase(curCodePoint);
|
||||||
} else if (Character.isLowerCase(ch)) {
|
} else if (Character.isLowerCase(curCodePoint)) {
|
||||||
if (whitespace) {
|
newCodePoint = Character.toUpperCase(curCodePoint);
|
||||||
buffer[i] = Character.toTitleCase(ch);
|
|
||||||
whitespace = false;
|
|
||||||
} else {
|
|
||||||
buffer[i] = Character.toUpperCase(ch);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
whitespace = Character.isWhitespace(ch);
|
newCodePoint = curCodePoint;
|
||||||
}
|
}
|
||||||
|
buff[offset++] = newCodePoint;
|
||||||
|
i += Character.charCount(newCodePoint);
|
||||||
}
|
}
|
||||||
return new String(buffer);
|
return new String(buff, 0, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue