Skip to content

Commit

Permalink
Improve parseAddressByHeader performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 19, 2025
1 parent 08782be commit 47773b2
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,32 @@ protected Address parseAddressByHeader() {
}
String host = header;
int port = -1;
char ch;
for (int i = header.length() - 1; i > 0; i--) {
ch = header.charAt(i);
if (ch == ':') {
host = header.substring(0, i);
// port
try {
port = Integer.parseInt(header.substring(i + 1));
} catch (NumberFormatException ignore) {
}
break;
} else if (!Character.isDigit(ch)) {
break;
int length = header.length();
for (int i = length - 1; i > 0; i--) {
switch (header.charAt(i)) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
continue;
case ':':
host = header.substring(0, i);
// port
if (i < length - 1) {
try {
port = Integer.parseInt(header.substring(i + 1));
} catch (NumberFormatException ignore) {
}
}
break;
default:
break;
}
}
return validateHost(host) ? new Address(host, port < 0 ? null : port) : null;
Expand Down

0 comments on commit 47773b2

Please sign in to comment.