Skip to content

Commit 6ba3bb5

Browse files
committed
Add efficient isUnreservedOrSubDelimiter method
isUnreserved and isSubDelimiter are usually checked together. It helps to have a shortcut with an efficient lookup. See gh-33639
1 parent e04b115 commit 6ba3bb5

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java

+28-11
Original file line numberDiff line numberDiff line change
@@ -590,25 +590,25 @@ public boolean isAllowed(int c) {
590590
AUTHORITY {
591591
@Override
592592
public boolean isAllowed(int c) {
593-
return isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c;
593+
return (isUnreservedOrSubDelimiter(c) || ':' == c || '@' == c);
594594
}
595595
},
596596
USER_INFO {
597597
@Override
598598
public boolean isAllowed(int c) {
599-
return isUnreserved(c) || isSubDelimiter(c) || ':' == c;
599+
return (isUnreservedOrSubDelimiter(c) || ':' == c);
600600
}
601601
},
602602
HOST_IPV4 {
603603
@Override
604604
public boolean isAllowed(int c) {
605-
return isUnreserved(c) || isSubDelimiter(c);
605+
return isUnreservedOrSubDelimiter(c);
606606
}
607607
},
608608
HOST_IPV6 {
609609
@Override
610610
public boolean isAllowed(int c) {
611-
return isUnreserved(c) || isSubDelimiter(c) || '[' == c || ']' == c || ':' == c;
611+
return (isUnreservedOrSubDelimiter(c) || '[' == c || ']' == c || ':' == c);
612612
}
613613
},
614614
PORT {
@@ -620,7 +620,7 @@ public boolean isAllowed(int c) {
620620
PATH {
621621
@Override
622622
public boolean isAllowed(int c) {
623-
return isPchar(c) || '/' == c;
623+
return (isPchar(c) || '/' == c);
624624
}
625625
},
626626
PATH_SEGMENT {
@@ -632,7 +632,7 @@ public boolean isAllowed(int c) {
632632
QUERY {
633633
@Override
634634
public boolean isAllowed(int c) {
635-
return isPchar(c) || '/' == c || '?' == c;
635+
return (isPchar(c) || '/' == c || '?' == c);
636636
}
637637
},
638638
QUERY_PARAM {
@@ -642,14 +642,14 @@ public boolean isAllowed(int c) {
642642
return false;
643643
}
644644
else {
645-
return isPchar(c) || '/' == c || '?' == c;
645+
return (isPchar(c) || '/' == c || '?' == c);
646646
}
647647
}
648648
},
649649
FRAGMENT {
650650
@Override
651651
public boolean isAllowed(int c) {
652-
return isPchar(c) || '/' == c || '?' == c;
652+
return (isPchar(c) || '/' == c || '?' == c);
653653
}
654654
},
655655
URI {
@@ -659,6 +659,15 @@ public boolean isAllowed(int c) {
659659
}
660660
};
661661

662+
private static final boolean[] unreservedOrSubDelimiterArray = new boolean[128];
663+
664+
static {
665+
for (int i = 0; i < 128; i++) {
666+
char c = (char) i;
667+
unreservedOrSubDelimiterArray[i] = (URI.isUnreserved(c) || URI.isSubDelimiter(c));
668+
}
669+
}
670+
662671
/**
663672
* Indicates whether the given character is allowed in this URI component.
664673
* @return {@code true} if the character is allowed; {@code false} otherwise
@@ -694,8 +703,8 @@ protected boolean isGenericDelimiter(int c) {
694703
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
695704
*/
696705
protected boolean isSubDelimiter(int c) {
697-
return ('!' == c || '$' == c || '&' == c || '\'' == c || '(' == c || ')' == c || '*' == c || '+' == c ||
698-
',' == c || ';' == c || '=' == c);
706+
return ('!' == c || '$' == c || '&' == c || '\'' == c ||
707+
'(' == c || ')' == c || '*' == c || '+' == c || ',' == c || ';' == c || '=' == c);
699708
}
700709

701710
/**
@@ -719,8 +728,16 @@ protected boolean isUnreserved(int c) {
719728
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
720729
*/
721730
protected boolean isPchar(int c) {
722-
return (isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c);
731+
return (isUnreservedOrSubDelimiter(c) || ':' == c || '@' == c);
723732
}
733+
734+
/**
735+
* Combined check whether a character is unreserved or a sub-delimiter.
736+
*/
737+
protected boolean isUnreservedOrSubDelimiter(int c) {
738+
return (c < unreservedOrSubDelimiterArray.length && c >= 0 && unreservedOrSubDelimiterArray[c]);
739+
}
740+
724741
}
725742

726743

0 commit comments

Comments
 (0)