private void ParseRelationStrings(CollationStrength strength, int i) { // Parse // prefix | str / extension // where prefix and extension are optional. StringCharSequence prefix = new StringCharSequence(""); string extension = ""; i = ParseTailoringString(i, rawBuilder.Value); char next = (i < rules.Length) ? rules[i] : (char)0; if (next == 0x7c) { // '|' separates the context prefix from the string. prefix = new StringCharSequence(rawBuilder.ToString()); i = ParseTailoringString(i + 1, rawBuilder.Value); next = (i < rules.Length) ? rules[i] : (char)0; } // str = rawBuilder (do not modify rawBuilder any more in this function) if (next == 0x2f) { // '/' separates the string from the extension. StringBuilder extBuilder = new StringBuilder(); i = ParseTailoringString(i + 1, extBuilder); extension = extBuilder.ToString(); } if (prefix.Length != 0) { int prefix0 = prefix.Value.CodePointAt(0); int c = rawBuilder.Value.CodePointAt(0); if (!nfc.HasBoundaryBefore(prefix0) || !nfc.HasBoundaryBefore(c)) { SetParseError("in 'prefix|str', prefix and str must each start with an NFC boundary"); return; } } try { sink.AddRelation(strength, prefix, rawBuilder, extension); } catch (Exception e) { SetParseError("adding relation failed", e); return; } ruleIndex = i; }
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)); }