Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c5ffaed

Browse files
committed
Change hash function for HashedStringSegment
1 parent f6951de commit c5ffaed

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/ServiceStack.Text/Json/JsonTypeSerializer.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,17 @@ private static StringSegment UnEscapeJsonString(StringSegment json, ref int inde
460460
}
461461
else
462462
{
463-
var strEndPos = json.IndexOfAny(IsSafeJsonChars, index);
464-
if (strEndPos == -1) return new StringSegment(buffer, offset + index, jsonLength - index);
463+
var i = index + offset;
464+
var end = offset + jsonLength;
465+
466+
while (i < end)
467+
{
468+
var c = buffer[i];
469+
if (c == JsonUtils.QuoteChar || c == JsonUtils.EscapeChar)
470+
break;
471+
i++;
472+
}
473+
if (i == end) return new StringSegment(buffer, offset + index, jsonLength - index);
465474
}
466475

467476
return Unescape(json);

src/ServiceStack.Text/Support/HashedStringSegment.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,23 @@ public override bool Equals(object obj)
3030

3131
public static int ComputeHashCode(StringSegment value)
3232
{
33-
return value.Length;
33+
var length = value.Length;
34+
if (length == 0)
35+
return 0;
3436

35-
//this implementation of case insensitive hash code is temporary commented
36-
//because we need to implement fast char.ToUpperInvariant() at first
37-
/* int c;
38-
int last = value.Offset + value.Length;
39-
int i = value.Offset;
40-
int hash1 = 5381;
41-
int hash2 = hash1;
37+
var offset = value.Offset;
38+
var hash = 37 * length;
4239

43-
while (i < last)
40+
char c1 = Char.ToUpperInvariant(value.Buffer[offset]);
41+
hash += 53 * c1;
42+
43+
if (length > 1)
4444
{
45-
c = char.ToUpperInvariant(value.Buffer[i]);
46-
hash1 = ((hash1 << 5) + hash1) ^ c;
47-
if ((i += 5) >= last)
48-
break;
49-
c = char.ToUpperInvariant(value.Buffer[i]);
50-
hash2 = ((hash2 << 5) + hash2) ^ c;
51-
i += 5;
45+
char c2 = Char.ToUpperInvariant(value.Buffer[offset + length - 1]);
46+
hash += 37 * c2;
5247
}
5348

54-
return hash1 + (hash2 * 1566083941);
55-
*/
49+
return hash;
5650
}
5751
}
5852
}

0 commit comments

Comments
 (0)