示例#1
0
        /// <summary>
        /// Attempts to parse the specified <paramref name="value"/> into an
        /// <see cref="IdentificationNumber"/> instance. The return value
        /// indicates whether the parsing succeeded. Parsing will fail if
        /// <paramref name="value"/> is <see langword="null"/>, empty, has an invalid
        /// length, doesn't match checksum digits or has an otherwise invalid format.
        /// </summary>
        /// <param name="value">A string containing the number to parse.</param>
        /// <param name="result">The resulting <see cref="IdentificationNumber"/> if parsing succeeded.</param>
        /// <returns>
        /// <see langword="true"/> if <paramref name="value"/> was
        /// parsed successfully; otherwise, <see langword="false"/>.
        /// </returns>
        public static bool TryParse(string?value, out IdentificationNumber result)
        {
            if (value is null)
            {
                result = default;
                return(false);
            }

            return(TryParse(value.AsSpan(), out result));
        }
示例#2
0
        /// <summary>
        /// Attempts to parse the specified <paramref name="value"/> into an
        /// <see cref="IdentificationNumber"/> instance. The return value
        /// indicates whether the parsing succeeded. Parsing will fail if
        /// <paramref name="value"/> is <see langword="null"/>, empty, has an invalid
        /// length, doesn't match checksum digits or has an otherwise invalid format.
        /// </summary>
        /// <param name="value">A string containing the number to parse.</param>
        /// <param name="result">The resulting <see cref="IdentificationNumber"/> if parsing succeeded.</param>
        /// <param name="error">The resulting <see cref="ParseError"/> if parsing failed.</param>
        /// <returns>
        /// <see langword="true"/> if <paramref name="value"/> was
        /// parsed successfully; otherwise, <see langword="false"/>.
        /// </returns>
        public static bool TryParse(string?value, out IdentificationNumber result, out ParseError error)
        {
            if (value is null)
            {
                error  = ParseError.InvalidLength;
                result = default;
                return(false);
            }

            return(TryParse(value.AsSpan(), out result, out error));
        }
        /// <summary>
        /// Attempts to parse the specified <paramref name="value"/> into an
        /// <see cref="IdentificationNumber"/> instance. The return value
        /// indicates whether the parsing succeeded. Parsing will fail if
        /// <paramref name="value"/> is <see langword="null"/>, empty, has an invalid
        /// length, doesn't match checksum digits or has an otherwise invalid format.
        /// </summary>
        /// <param name="value">A string containing the number to parse.</param>
        /// <param name="result">The resulting <see cref="IdentificationNumber"/> if parsing succeeded.</param>
        /// <param name="error">The resulting <see cref="ParseError"/> if parsing failed.</param>
        /// <returns>
        /// <see langword="true"/> if <paramref name="value"/> was
        /// parsed successfully; otherwise, <see langword="false"/>.
        /// </returns>
        public static bool TryParse(ReadOnlySpan <char> value, out IdentificationNumber result, out ParseError error)
        {
            result = default;
            error  = default;

            if (value.Length != Length)
            {
                error = ParseError.InvalidLength;
                return(false);
            }

            var k2 = Checksum.Mod11(value, K2Weights);

            if (k2 == 10)
            {
                error = ParseError.InvalidChecksum;
                return(false);
            }

            if (!TryGetDigit(value[10], out var k2Digit))
            {
                error = ParseError.InvalidCharacter;
                return(false);
            }

            if (k2Digit != k2)
            {
                error = ParseError.InvalidChecksum;
                return(false);
            }

            var k1 = Checksum.Mod11(value, K1Weights);

            if (k1 == 10)
            {
                error = ParseError.InvalidChecksum;
                return(false);
            }

            if (!TryGetDigit(value[9], out var k1Digit))
            {
                error = ParseError.InvalidCharacter;
                return(false);
            }

            if (k1Digit != k1)
            {
                error = ParseError.InvalidChecksum;
                return(false);
            }

            if (!TryReadThreeDecimalDigits(value, 6, out var individual))
            {
                error = ParseError.InvalidCharacter;
                return(false);
            }

            if (!TryReadTwoDecimalDigits(value, 4, out var year))
            {
                error = ParseError.InvalidCharacter;
                return(false);
            }

            var fullYear = GetFullYear(year, individual);

            if (!fullYear.HasValue)
            {
                error = ParseError.InvalidYear;
                return(false);
            }

            if (!TryReadTwoDecimalDigits(value, 2, out var month))
            {
                error = ParseError.InvalidCharacter;
                return(false);
            }

            if (!TryReadTwoDecimalDigits(value, 0, out var day))
            {
                error = ParseError.InvalidCharacter;
                return(false);
            }

            var dateOfBirth = GetDateOfBirth(fullYear.Value, month, day, out var kind, out error);

            if (!dateOfBirth.HasValue)
            {
                return(false);
            }

            var checkDigits = (k1 * 10) + k2;

            result = new IdentificationNumber(dateOfBirth.Value, individual, checkDigits, kind);
            return(true);
        }
 /// <summary>
 /// Attempts to parse the specified <paramref name="value"/> into an
 /// <see cref="IdentificationNumber"/> instance. The return value
 /// indicates whether the parsing succeeded. Parsing will fail if
 /// <paramref name="value"/> is <see langword="null"/>, empty, has an invalid
 /// length, doesn't match checksum digits or has an otherwise invalid format.
 /// </summary>
 /// <param name="value">A string containing the number to parse.</param>
 /// <param name="result">The resulting <see cref="IdentificationNumber"/> if parsing succeeded.</param>
 /// <returns>
 /// <see langword="true"/> if <paramref name="value"/> was
 /// parsed successfully; otherwise, <see langword="false"/>.
 /// </returns>
 public static bool TryParse(ReadOnlySpan <char> value, out IdentificationNumber result)
 {
     return(TryParse(value, out result, out _));
 }
示例#5
0
 private static void Format(IdentificationNumber number, Span <char> buffer)
 {
     WriteTwoDecimalDigits((uint)number.CheckDigits, buffer, 9);
     WriteThreeDecimalDigits((uint)number.IndividualNumber, buffer, 6);
     WriteDateOfBirth(buffer, number.DateOfBirth, number.NumberKind);
 }