Ejemplo n.º 1
0
        public override int GetHashCode()
        {
            if (_encodedBytes == null)
            {
                return(0);
            }

            if (!_HasCachedHashCode)
            {
                // Intentionally hashes similarly to the expanded strings.
                _cachedHashCode = GetString().GetHashCode();
                _flags         |= _SmallFlags.HasHashCode;
            }

            return(_cachedHashCode);
        }
Ejemplo n.º 2
0
        public SmallString(string value, bool precacheHashCode = false)
        {
            _flags          = _SmallFlags.None;
            _cachedHashCode = 0;

            if (!string.IsNullOrEmpty(value))
            {
                if (precacheHashCode)
                {
                    _flags         |= _SmallFlags.HasHashCode;
                    _cachedHashCode = value.GetHashCode();
                }

                long numValue;
                if (long.TryParse(value, System.Globalization.NumberStyles.None, null, out numValue))
                {
                    _flags       |= _SmallFlags.IsInt64;
                    _encodedBytes = BitConverter.GetBytes(numValue);

                    // It's possible that this doesn't round trip with full fidelity.
                    // If this assert ever gets hit, consider adding an overload that opts
                    // out of this optimization.
                    // (Note that the parameters are not evaluated on retail builds)
                    Assert.AreEqual(this.GetString(), value);

                    return;
                }

                _encodedBytes = s_Encoder.GetBytes(value);
                Assert.IsNotNull(_encodedBytes);
            }
            else
            {
                _encodedBytes = null;
            }
        }