Switched to using codePoints in toHtmlEntities

This commit is contained in:
Erik C. Thauvin 2023-03-15 22:57:27 -07:00
parent 772ffd55f4
commit b3930804c6
3 changed files with 13 additions and 8 deletions

View file

@ -44,14 +44,19 @@ public class EncodeHtmlEntities implements ValueRenderer {
* @param text the String to convert.
* @return the converted string.
*/
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
public static String toHtmlEntities(String text) {
var buff = new StringBuilder(text.length() * 6);
// https://stackoverflow.com/a/6766497/8356718
var sb = new StringBuilder(text.length() * 6);
for (var i = 0; i < text.length(); i++) {
buff.append("&#").append((int) text.charAt(i)).append(';');
var codePoint = text.codePointAt(i);
// Skip over the second char in a surrogate pair
if (codePoint > 0xffff) {
i++;
}
return buff.toString();
sb.append(String.format("&#%s;", codePoint));
}
return sb.toString();
}
/**

View file

@ -33,7 +33,7 @@ public final class RenderUtils {
*
* @param template the template
* @param valueId the ID of the value to fetch
* @return The fetched value.
* @return the fetched value.
*/
public static String fetchValue(Template template, String valueId) {
Object value = null;

View file

@ -37,8 +37,8 @@ public class Rot13 implements ValueRenderer {
/**
* Translates a String to/from ROT13.
*
* @param src The source String.
* @return The translated String.
* @param src the source String.
* @return the translated String.
*/
public static String rot13(String src) {
if (src == null || src.isEmpty()) {