mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +01:00
Improve StringUtils.escapeForXML()
use CharSequence as input.
This commit is contained in:
parent
1c716bc1e0
commit
46f9b9ab39
1 changed files with 9 additions and 10 deletions
|
@ -41,18 +41,17 @@ public class StringUtils {
|
||||||
public static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
|
public static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escapes all necessary characters in the String so that it can be used
|
* Escapes all necessary characters in the CharSequence so that it can be used
|
||||||
* in an XML doc.
|
* in an XML doc.
|
||||||
*
|
*
|
||||||
* @param string the string to escape.
|
* @param input the CharSequence to escape.
|
||||||
* @return the string with appropriate characters escaped.
|
* @return the string with appropriate characters escaped.
|
||||||
*/
|
*/
|
||||||
public static CharSequence escapeForXML(final String string) {
|
public static CharSequence escapeForXML(final CharSequence input) {
|
||||||
if (string == null) {
|
if (input == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final char[] input = string.toCharArray();
|
final int len = input.length();
|
||||||
final int len = input.length;
|
|
||||||
final StringBuilder out = new StringBuilder((int)(len*1.3));
|
final StringBuilder out = new StringBuilder((int)(len*1.3));
|
||||||
CharSequence toAppend;
|
CharSequence toAppend;
|
||||||
char ch;
|
char ch;
|
||||||
|
@ -60,7 +59,7 @@ public class StringUtils {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
toAppend = null;
|
toAppend = null;
|
||||||
ch = input[i];
|
ch = input.charAt(i);
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case '<':
|
case '<':
|
||||||
toAppend = LT_ENCODE;
|
toAppend = LT_ENCODE;
|
||||||
|
@ -82,7 +81,7 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
if (toAppend != null) {
|
if (toAppend != null) {
|
||||||
if (i > last) {
|
if (i > last) {
|
||||||
out.append(input, last, i - last);
|
out.append(input, last, i);
|
||||||
}
|
}
|
||||||
out.append(toAppend);
|
out.append(toAppend);
|
||||||
last = ++i;
|
last = ++i;
|
||||||
|
@ -91,10 +90,10 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (last == 0) {
|
if (last == 0) {
|
||||||
return string;
|
return input;
|
||||||
}
|
}
|
||||||
if (i > last) {
|
if (i > last) {
|
||||||
out.append(input, last, i - last);
|
out.append(input, last, i);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue