Add efficient isUnreservedOrSubDelimiter method

isUnreserved and isSubDelimiter are usually checked together. It helps
to have a shortcut with an efficient lookup.

See gh-33639
This commit is contained in:
rstoyanchev
2024-09-30 13:52:33 +01:00
parent e04b115a7e
commit 6ba3bb54a0

View File

@@ -590,25 +590,25 @@ final class HierarchicalUriComponents extends UriComponents {
AUTHORITY {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c;
return (isUnreservedOrSubDelimiter(c) || ':' == c || '@' == c);
}
},
USER_INFO {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c) || ':' == c;
return (isUnreservedOrSubDelimiter(c) || ':' == c);
}
},
HOST_IPV4 {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c);
return isUnreservedOrSubDelimiter(c);
}
},
HOST_IPV6 {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c) || '[' == c || ']' == c || ':' == c;
return (isUnreservedOrSubDelimiter(c) || '[' == c || ']' == c || ':' == c);
}
},
PORT {
@@ -620,7 +620,7 @@ final class HierarchicalUriComponents extends UriComponents {
PATH {
@Override
public boolean isAllowed(int c) {
return isPchar(c) || '/' == c;
return (isPchar(c) || '/' == c);
}
},
PATH_SEGMENT {
@@ -632,7 +632,7 @@ final class HierarchicalUriComponents extends UriComponents {
QUERY {
@Override
public boolean isAllowed(int c) {
return isPchar(c) || '/' == c || '?' == c;
return (isPchar(c) || '/' == c || '?' == c);
}
},
QUERY_PARAM {
@@ -642,14 +642,14 @@ final class HierarchicalUriComponents extends UriComponents {
return false;
}
else {
return isPchar(c) || '/' == c || '?' == c;
return (isPchar(c) || '/' == c || '?' == c);
}
}
},
FRAGMENT {
@Override
public boolean isAllowed(int c) {
return isPchar(c) || '/' == c || '?' == c;
return (isPchar(c) || '/' == c || '?' == c);
}
},
URI {
@@ -659,6 +659,15 @@ final class HierarchicalUriComponents extends UriComponents {
}
};
private static final boolean[] unreservedOrSubDelimiterArray = new boolean[128];
static {
for (int i = 0; i < 128; i++) {
char c = (char) i;
unreservedOrSubDelimiterArray[i] = (URI.isUnreserved(c) || URI.isSubDelimiter(c));
}
}
/**
* Indicates whether the given character is allowed in this URI component.
* @return {@code true} if the character is allowed; {@code false} otherwise
@@ -694,8 +703,8 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isSubDelimiter(int c) {
return ('!' == c || '$' == c || '&' == c || '\'' == c || '(' == c || ')' == c || '*' == c || '+' == c ||
',' == c || ';' == c || '=' == c);
return ('!' == c || '$' == c || '&' == c || '\'' == c ||
'(' == c || ')' == c || '*' == c || '+' == c || ',' == c || ';' == c || '=' == c);
}
/**
@@ -719,8 +728,16 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isPchar(int c) {
return (isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c);
return (isUnreservedOrSubDelimiter(c) || ':' == c || '@' == c);
}
/**
* Combined check whether a character is unreserved or a sub-delimiter.
*/
protected boolean isUnreservedOrSubDelimiter(int c) {
return (c < unreservedOrSubDelimiterArray.length && c >= 0 && unreservedOrSubDelimiterArray[c]);
}
}