static void DoCaseSensitivity(int len) { // Build an upper case and lower case string const int UpperA = 'A'; const int UpperZ = 'Z'; const int UpperToLower = (int)'a' - UpperA; var lowerCaseBytes = new byte[len]; var upperCaseBuilder = new StringBuilderCharSequence(len); for (int i = 0; i < len; ++i) { char upper = (char)(Rand.Next((UpperZ - UpperA) + 1) + UpperA); upperCaseBuilder.Append(upper); lowerCaseBytes[i] = (byte)(upper + UpperToLower); } var upperCaseString = (StringCharSequence)upperCaseBuilder.ToString(); var lowerCaseString = (StringCharSequence) new string(lowerCaseBytes.Select(x => (char)x).ToArray()); var lowerCaseAscii = new AsciiString(lowerCaseBytes, false); var upperCaseAscii = new AsciiString(upperCaseString); // Test upper case hash codes are equal int upperCaseExpected = upperCaseAscii.GetHashCode(); Assert.Equal(upperCaseExpected, AsciiString.GetHashCode(upperCaseBuilder)); Assert.Equal(upperCaseExpected, AsciiString.GetHashCode(upperCaseString)); Assert.Equal(upperCaseExpected, upperCaseAscii.GetHashCode()); // Test lower case hash codes are equal int lowerCaseExpected = lowerCaseAscii.GetHashCode(); Assert.Equal(lowerCaseExpected, AsciiString.GetHashCode(lowerCaseAscii)); Assert.Equal(lowerCaseExpected, AsciiString.GetHashCode(lowerCaseString)); Assert.Equal(lowerCaseExpected, lowerCaseAscii.GetHashCode()); // Test case insensitive hash codes are equal int expectedCaseInsensitive = lowerCaseAscii.GetHashCode(); Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(upperCaseBuilder)); Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(upperCaseString)); Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(lowerCaseString)); Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(lowerCaseAscii)); Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(upperCaseAscii)); Assert.Equal(expectedCaseInsensitive, lowerCaseAscii.GetHashCode()); Assert.Equal(expectedCaseInsensitive, upperCaseAscii.GetHashCode()); // Test that opposite cases are equal Assert.Equal(lowerCaseAscii.GetHashCode(), AsciiString.GetHashCode(upperCaseString)); Assert.Equal(upperCaseAscii.GetHashCode(), AsciiString.GetHashCode(lowerCaseString)); }
private AsciiString Get0(ICharSequence name) { var nameHash = AsciiString.GetHashCode(name); var pseudoHeadersEnd = _pseudoHeaders.Length - 1; for (int i = 0; i < pseudoHeadersEnd; i += 2) { AsciiString roName = _pseudoHeaders[i]; if (roName.GetHashCode() == nameHash && roName.ContentEqualsIgnoreCase(name)) { return(_pseudoHeaders[i + 1]); } } var otherHeadersEnd = _otherHeaders.Length - 1; for (int i = 0; i < otherHeadersEnd; i += 2) { AsciiString roName = _otherHeaders[i]; if (roName.GetHashCode() == nameHash && roName.ContentEqualsIgnoreCase(name)) { return(_otherHeaders[i + 1]); } } return(null); }
public void SubStringHashCode() { var value1 = new AsciiString("123"); var value2 = new AsciiString("a123".Substring(1)); //two "123"s Assert.Equal(AsciiString.GetHashCode(value1), AsciiString.GetHashCode(value2)); }
public void AsciiStringGetHashCodeReturnsDifferentCodeIfAsciiStringsAreNotEqual() { string value = "9quali52ty3"; AsciiString asciiString1 = new AsciiString(value); AsciiString asciiString2 = new AsciiString(value.Reverse()); Assert.AreNotEqual(asciiString1.GetHashCode(), asciiString2.GetHashCode()); }
public void AsciiStringGetHashCodeReturnsSameCodeIfAsciiStringsAreEqual() { string value = "9quali52ty3"; AsciiString asciiString1 = new AsciiString(value); AsciiString asciiString2 = new AsciiString(value); Assert.AreEqual(asciiString1.GetHashCode(), asciiString2.GetHashCode()); }
public void CaseInsensitiveHasherCharBuffer() { const string S1 = "TRANSFER-ENCODING"; var array = new char[128]; const int Offset = 100; for (int i = 0; i < S1.Length; ++i) { array[Offset + i] = S1[i]; } var s = new AsciiString(S1); var b = new AsciiString(array, Offset, S1.Length); Assert.Equal(AsciiString.GetHashCode(s), AsciiString.GetHashCode(b)); }
public void SubSequence() { char[] initChars = { 't', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't' }; byte[] init = initChars.Select(c => (byte)c).ToArray(); var ascii = new AsciiString(init); const int Start = 2; int end = init.Length; AsciiString sub1 = ascii.SubSequence(Start, end, false); AsciiString sub2 = ascii.SubSequence(Start, end, true); Assert.Equal(sub1.GetHashCode(), sub2.GetHashCode()); Assert.Equal(sub1, sub2); for (int i = Start; i < end; ++i) { Assert.Equal(init[i], sub1.ByteAt(i - Start)); } }
/// <summary> /// Returns the lowest index value for the header field name in the dynamic table. Returns -1 if /// the header field name is not in the dynamic table. /// </summary> /// <param name="name"></param> /// <returns></returns> int GetIndex(ICharSequence name) { if (0u >= (uint)Length() || name is null) { return(-1); } int h = AsciiString.GetHashCode(name); int i = Index(h); for (HeaderEntry e = _headerFields[i]; e is object; e = e.Next) { if (e.Hash == h && HpackUtil.EqualsConstantTime(name, e._name) != 0) { return(GetIndex(e.Index)); } } return(-1); }
/// <summary> /// Returns the header entry with the lowest index value for the header field. Returns null if /// header field is not in the dynamic table. /// </summary> /// <param name="name"></param> /// <param name="value"></param> HeaderEntry GetEntryInsensitive(ICharSequence name, ICharSequence value) { if (0u >= (uint)Length() || name is null || value is null) { return(null); } int h = AsciiString.GetHashCode(name); int i = Index(h); for (HeaderEntry e = _headerFields[i]; e is object; e = e.Next) { // Check the value before then name, as it is more likely the value will be different incase there is no // match. if (e.Hash == h && HpackUtil.EqualsVariableTime(value, e._value) && HpackUtil.EqualsVariableTime(name, e._name)) { return(e); } } return(null); }
/// <summary> /// Add the header field to the dynamic table. Entries are evicted from the dynamic table until /// the size of the table and the new header field is less than the table's maxHeaderTableSize. If the size /// of the new entry is larger than the table's maxHeaderTableSize, the dynamic table will be cleared. /// </summary> /// <param name="name"></param> /// <param name="value"></param> /// <param name="headerSize"></param> void Add(ICharSequence name, ICharSequence value, long headerSize) { // Clear the table if the header field size is larger than the maxHeaderTableSize. if (headerSize > _maxHeaderTableSize) { Clear(); return; } // Evict oldest entries until we have enough maxHeaderTableSize. while (_maxHeaderTableSize - _size < headerSize) { _ = Remove(); } int h = AsciiString.GetHashCode(name); int i = Index(h); HeaderEntry old = _headerFields[i]; HeaderEntry e = new HeaderEntry(h, name, value, _head.Before.Index - 1, old); _headerFields[i] = e; e.AddBefore(_head); _size += headerSize; }