class HtmlEscaper implements ResultMapper {

    String escape(CharSequence value) {
        if (Objects.requireNonNull(value).length() == 0) {
            return value.toString();
        }
        for (int i = 0; i < value.length(); i++) {
            String replacement = replacementFor(value.charAt(i));
            if (replacement != null) {
                // In most cases we will not need to escape the value at all
                return doEscape(value, i, new StringBuilder(value.subSequence(0, i)).append(replacement));
            }
        }
        return value.toString();
    }

}