Exemple #1
0
 public static bool TryParse(ReadOnlySpan <char> input, [NotNullWhen(true)] out Version?result) =>
 (result = ParseVersion(input, throwOnFailure: false)) != null;
Exemple #2
0
        private static Version?ParseVersion(ReadOnlySpan <char> input, bool throwOnFailure)
        {
            // Find the separator between major and minor.  It must exist.
            int majorEnd = input.IndexOf('.');

            if (majorEnd < 0)
            {
                if (throwOnFailure)
                {
                    throw new ArgumentException(SR.Arg_VersionString, nameof(input));
                }
                return(null);
            }

            // Find the ends of the optional minor and build portions.
            // We musn't have any separators after build.
            int buildEnd = -1;
            int minorEnd = input.Slice(majorEnd + 1).IndexOf('.');

            if (minorEnd != -1)
            {
                minorEnd += (majorEnd + 1);
                buildEnd  = input.Slice(minorEnd + 1).IndexOf('.');
                if (buildEnd != -1)
                {
                    buildEnd += (minorEnd + 1);
                    if (input.Slice(buildEnd + 1).Contains('.'))
                    {
                        if (throwOnFailure)
                        {
                            throw new ArgumentException(SR.Arg_VersionString, nameof(input));
                        }
                        return(null);
                    }
                }
            }

            int minor, build, revision;

            // Parse the major version
            if (!TryParseComponent(input.Slice(0, majorEnd), nameof(input), throwOnFailure, out int major))
            {
                return(null);
            }

            if (minorEnd != -1)
            {
                // If there's more than a major and minor, parse the minor, too.
                if (!TryParseComponent(input.Slice(majorEnd + 1, minorEnd - majorEnd - 1), nameof(input), throwOnFailure, out minor))
                {
                    return(null);
                }

                if (buildEnd != -1)
                {
                    // major.minor.build.revision
                    return
                        (TryParseComponent(input.Slice(minorEnd + 1, buildEnd - minorEnd - 1), nameof(build), throwOnFailure, out build) &&
                         TryParseComponent(input.Slice(buildEnd + 1), nameof(revision), throwOnFailure, out revision) ?
                         new Version(major, minor, build, revision) :
                         null);
                }
                else
                {
                    // major.minor.build
                    return(TryParseComponent(input.Slice(minorEnd + 1), nameof(build), throwOnFailure, out build) ?
                           new Version(major, minor, build) :
                           null);
                }
            }
            else
            {
                // major.minor
                return(TryParseComponent(input.Slice(majorEnd + 1), nameof(input), throwOnFailure, out minor) ?
                       new Version(major, minor) :
                       null);
            }
        }
Exemple #3
0
 bool ISpanFormattable.TryFormat(Span <char> destination, out int charsWritten, ReadOnlySpan <char> format, IFormatProvider?provider)
 {
     // format and provider are ignored.
     return(TryFormat(destination, out charsWritten));
 }
Exemple #4
0
 public static Version Parse(ReadOnlySpan <char> input) =>
 ParseVersion(input, throwOnFailure: true) !;
Exemple #5
0
 public static bool TryParse(ReadOnlySpan <char> s, NumberStyles style, IFormatProvider provider, out int result)
 {
     NumberFormatInfo.ValidateParseStyleInteger(style);
     return(Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result, out _));
 }
Exemple #6
0
        public static bool TryFormat(this int value, Span <char> destination, out int charsWritten, ReadOnlySpan <char> format = default, IFormatProvider provider = null)
        {
#if NETCOREAPP2_1
            return(value.TryFormat(destination, out charsWritten, format, provider));
#else
            var str = value.ToString(format.ToString(), provider);
            if (str.AsSpan().TryCopyTo(destination))
            {
                charsWritten = str.Length;
                return(true);
            }
            charsWritten = 0;
            return(false);
#endif
        }
Exemple #7
0
 public static int Parse(ReadOnlySpan <char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
 {
     NumberFormatInfo.ValidateParseStyleInteger(style);
     return(Number.ParseInt32(s, style, NumberFormatInfo.GetInstance(provider)));
 }
Exemple #8
0
 public static bool TryParse(ReadOnlySpan <char> s, out int result)
 {
     return(Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result, out _));
 }
Exemple #9
0
 public bool TryFormat(Span <char> destination, out int charsWritten, ReadOnlySpan <char> format = default, IFormatProvider provider = null)
 {
     return(Number.TryFormatInt32(m_value, format, provider, destination, out charsWritten));
 }
Exemple #10
0
 public static bool TryParse(ReadOnlySpan <char> s, out Int16 result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
 {
     NumberFormatInfo.ValidateParseStyleInteger(style);
     return(TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result));
 }
Exemple #11
0
        internal static int GetHashCodeOrdinalIgnoreCase(ReadOnlySpan <char> value)
        {
            ulong seed = Marvin.DefaultSeed;

            return(Marvin.ComputeHash32OrdinalIgnoreCase(ref MemoryMarshal.GetReference(value), value.Length /* in chars, not bytes */, (uint)seed, (uint)(seed >> 32)));
        }
Exemple #12
0
        public static int GetHashCode(ReadOnlySpan <char> value)
        {
            ulong seed = Marvin.DefaultSeed;

            return(Marvin.ComputeHash32(ref Unsafe.As <char, byte>(ref MemoryMarshal.GetReference(value)), value.Length * 2 /* in bytes, not chars */, (uint)seed, (uint)(seed >> 32)));
        }
Exemple #13
0
 internal static int CompareOrdinal(ReadOnlySpan <char> strA, ReadOnlySpan <char> strB)
 => SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(strA), strA.Length, ref MemoryMarshal.GetReference(strB), strB.Length);