1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Internal.Runtime.CompilerServices;
#if BIT64
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif
namespace System
{
internal static partial class Marvin
{
/// <summary>
/// Compute a Marvin OrdinalIgnoreCase hash and collapse it into a 32-bit hash.
/// n.b. <paramref name="count"/> is specified as char count, not byte count.
/// </summary>
public static int ComputeHash32OrdinalIgnoreCase(ref char data, int count, uint p0, uint p1)
{
uint ucount = (uint)count; // in chars
nuint byteOffset = 0; // in bytes
uint tempValue;
// We operate on 32-bit integers (two chars) at a time.
while (ucount >= 2)
{
tempValue = Unsafe.ReadUnaligned<uint>(ref Unsafe.As<char, byte>(ref Unsafe.AddByteOffset(ref data, byteOffset)));
if (!Utf16Utility.AllCharsInUInt32AreAscii(tempValue))
{
goto NotAscii;
}
p0 += Utf16Utility.ConvertAllAsciiCharsInUInt32ToUppercase(tempValue);
Block(ref p0, ref p1);
byteOffset += 4;
ucount -= 2;
}
// We have either one char (16 bits) or zero chars left over.
Debug.Assert(ucount < 2);
if (ucount > 0)
{
tempValue = Unsafe.AddByteOffset(ref data, byteOffset);
if (tempValue > 0x7Fu)
{
goto NotAscii;
}
// addition is written with -0x80u to allow fall-through to next statement rather than jmp past it
p0 += Utf16Utility.ConvertAllAsciiCharsInUInt32ToUppercase(tempValue) + (0x800000u - 0x80u);
}
p0 += 0x80u;
Block(ref p0, ref p1);
Block(ref p0, ref p1);
return (int)(p1 ^ p0);
NotAscii:
Debug.Assert(0 <= ucount && ucount <= Int32.MaxValue); // this should fit into a signed int
return ComputeHash32OrdinalIgnoreCaseSlow(ref Unsafe.AddByteOffset(ref data, byteOffset), (int)ucount, p0, p1);
}
private static int ComputeHash32OrdinalIgnoreCaseSlow(ref char data, int count, uint p0, uint p1)
{
Debug.Assert(count > 0);
char[] borrowedArr = null;
Span<char> scratch = (uint)count <= 64 ? stackalloc char[64] : (borrowedArr = ArrayPool<char>.Shared.Rent(count));
int charsWritten = new ReadOnlySpan<char>(ref data, count).ToUpperInvariant(scratch);
Debug.Assert(charsWritten == count); // invariant case conversion should involve simple folding; preserve code unit count
// Slice the array to the size returned by ToUpperInvariant.
// Multiplication below will not overflow since going from positive Int32 to UInt32.
int hash = ComputeHash32(ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(scratch)), (uint)charsWritten * 2, p0, p1);
// Return the borrowed array if necessary.
if (borrowedArr != null)
{
ArrayPool<char>.Shared.Return(borrowedArr);
}
return hash;
}
}
}
|