Prevent string allocation when not passing in any allowed characters.

This commit is contained in:
Geert Bevin 2022-12-31 13:14:23 -05:00
parent 262e9d6d29
commit 3e397a65d2

View file

@ -126,6 +126,20 @@ public final class UrlEncoder {
return out.toString(); return out.toString();
} }
/**
* Transforms a provided <code>String</code> object into a new string,
* containing only valid URL characters in the UTF-8 encoding.
*
* @param source The string that has to be transformed into a valid URL
* string.
* @return The encoded <code>String</code> object.
* @see #decode(String)
* @since 1.0
*/
public static String encode(String source) {
return encode(source, (String)null);
}
/** /**
* Transforms a provided <code>String</code> object into a new string, * Transforms a provided <code>String</code> object into a new string,
* containing only valid URL characters in the UTF-8 encoding. * containing only valid URL characters in the UTF-8 encoding.
@ -162,7 +176,7 @@ public final class UrlEncoder {
var i = 0; var i = 0;
while(i < source.length()) { while(i < source.length()) {
ch = source.charAt(i); ch = source.charAt(i);
if (isUnreservedUriChar(ch) || allow.indexOf(ch) != -1) { if (isUnreservedUriChar(ch) || (allow != null && allow.indexOf(ch) != -1)) {
if (out != null) { if (out != null) {
out.append(ch); out.append(ch);
} }