Improving HttpField.nameHashCode

This commit is contained in:
Joakim Erdfelt 2015-02-20 10:54:28 -07:00
parent 08b4bd439e
commit 22a0cb4a28
1 changed files with 15 additions and 9 deletions

View File

@ -28,6 +28,8 @@ public class HttpField
private final HttpHeader _header;
private final String _name;
private final String _value;
// cached hashcode for case insensitive name
private int hash = 0;
public HttpField(HttpHeader header, String name, String value)
{
@ -380,18 +382,22 @@ public class HttpField
private int nameHashCode()
{
int hash = 13;
int h = this.hash;
int len = _name.length();
for (int i = 0; i < len; i++)
if (h == 0 && len > 0)
{
// simple case insensitive hash
char c = _name.charAt(i);
// assuming us-ascii (per last paragraph on http://tools.ietf.org/html/rfc7230#section-3.2.4)
if ((c >= 'a' && c <= 'z'))
c -= 0x20;
hash = 15 * hash + c;
for (int i = 0; i < len; i++)
{
// simple case insensitive hash
char c = _name.charAt(i);
// assuming us-ascii (per last paragraph on http://tools.ietf.org/html/rfc7230#section-3.2.4)
if ((c >= 'a' && c <= 'z'))
c -= 0x20;
h = 31 * h + c;
}
this.hash = h;
}
return hash;
return h;
}
@Override