Ejemplo n.º 1
0
        private string GetStringSortKey(IConvertible value)
        {
            string text = value.ToString(null);

            if (_compareOptions.HasFlag(CompareOptions.Ordinal))
            {
                return(text);
            }
            if (_compareOptions.HasFlag(CompareOptions.OrdinalIgnoreCase))
            {
                return(text.ToUpperInvariant());
            }
            return(ToComparableBase64String(_compareInfo.GetSortKey(text, _compareOptions).KeyData));
        }
Ejemplo n.º 2
0
 private NaturalStringComparer(CompareOptions options, CultureInfo culture = null)
 {
     _options    = options;
     _ignoreCase = _options.HasFlag(CompareOptions.IgnoreCase);
     _culture    = culture;
     if (culture != null)
     {
         _mappings = MappingCache.GetMapping(culture, _ignoreCase);
     }
     else
     {
         _mappings = Array.Empty <ushort>();
     }
 }
        /// <inheritdoc />
        public object ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            ReferenceCodec.MarkValueField(reader.Session);
            uint           type    = default;
            CompareOptions options = default;
            int            lcid    = default;
            uint           fieldId = 0;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    type = UInt32Codec.ReadValue(ref reader, header);
                    break;

                case 1:
                    options = (CompareOptions)UInt64Codec.ReadValue(ref reader, header);
                    break;

                case 2:
                    lcid = Int32Codec.ReadValue(ref reader, header);
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            if (type == 0)
            {
                return(null);
            }
            else if (type == 1)
            {
                if (options.HasFlag(CompareOptions.IgnoreCase))
                {
                    return(StringComparer.OrdinalIgnoreCase);
                }
                else
                {
                    return(StringComparer.Ordinal);
                }
            }
            else if (type == 2)
            {
                if (lcid == CultureInfo.InvariantCulture.LCID)
                {
                    if (options == CompareOptions.None)
                    {
                        return(StringComparer.InvariantCulture);
                    }
                    else if (options == CompareOptions.IgnoreCase)
                    {
                        return(StringComparer.InvariantCultureIgnoreCase);
                    }

                    // Otherwise, perhaps some other options were specified, in which case we fall-through to create a new comparer.
                }

                var cultureInfo = CultureInfo.GetCultureInfo(lcid);
                var result      = StringComparer.Create(cultureInfo, options);
                return(result);
            }

            ThrowNotSupported(field, type);
            return(null);
        }