Beispiel #1
0
 static void RunTest(IEqualityComparer <string> comparer, bool expectedIsOrdinal, bool expectedIgnoreCase)
 {
     Assert.Equal(expectedIsOrdinal, StringComparer.IsWellKnownOrdinalComparer(comparer, out bool actualIgnoreCase));
     Assert.Equal(expectedIgnoreCase, actualIgnoreCase);
 }
        /// <inheritdoc />
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) where TBufferWriter : IBufferWriter <byte>
        {
            uint           type;
            CompareOptions compareOptions = default;
            CompareInfo    compareInfo    = default;

            if (value is null)
            {
                type = 0;
            }
            else
            {
#if NET6_0_OR_GREATER
                var comparer = (IEqualityComparer <string>)value;
                if (StringComparer.IsWellKnownOrdinalComparer(comparer, out var ignoreCase))
                {
                    // Ordinal. This also handles EqualityComparer<string>.Default.
                    type = 1;
                    if (ignoreCase)
                    {
                        compareOptions = CompareOptions.IgnoreCase;
                    }
                }
                else if (StringComparer.IsWellKnownCultureAwareComparer(comparer, out compareInfo, out compareOptions))
                {
                    type = 2;
                }
                else
                {
                    ThrowNotSupported(value.GetType());
                    return;
                }
#else
                var isOrdinal           = _ordinalComparer.Equals(value) || _defaultEqualityComparer.Equals(value);
                var isOrdinalIgnoreCase = _ordinalIgnoreCaseComparer.Equals(value);
                if (isOrdinal)
                {
                    type = 1;
                }
                else if (isOrdinalIgnoreCase)
                {
                    type           = 1;
                    compareOptions = CompareOptions.IgnoreCase;
                }
                else if (TryGetWellKnownCultureAwareComparerInfo(value, out compareInfo, out compareOptions, out var ignoreCase))
                {
                    type = 2;
                    if (ignoreCase)
                    {
                        compareOptions |= CompareOptions.IgnoreCase;
                    }
                }
                else
                {
                    ThrowNotSupported(value.GetType());
                    return;
                }
#endif
            }

            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(WellKnownStringComparerCodec), WireType.TagDelimited);

            UInt32Codec.WriteField(ref writer, 0, typeof(int), type);
            UInt64Codec.WriteField(ref writer, 1, typeof(ulong), (ulong)compareOptions);

            if (compareInfo is not null)
            {
                Int32Codec.WriteField(ref writer, 1, typeof(int), compareInfo.LCID);
            }

            writer.WriteEndObject();
        }