コード例 #1
0
        private static bool TryGetFormatErrors(
            string key,
            ResourceManagerExt.CulturesAndKeys culturesAndKeys,
            IEnumerable <CultureInfo> cultures,
            out FormatError formatErrors)
        {
            int?count        = null;
            var translations = culturesAndKeys.GetTranslationsFor(key, cultures);

            foreach (var translation in translations)
            {
                if (!FormatString.IsValidFormat(translation.Value, out var indexCount, out _))
                {
                    formatErrors = new FormatError(key, translations);
                    return(true);
                }

                if (count == null)
                {
                    count = indexCount;
                    continue;
                }

                if (count != indexCount)
                {
                    formatErrors = new FormatError(key, translations);
                    return(true);
                }
            }

            formatErrors = null;
            return(false);
        }
コード例 #2
0
        internal static void Format(string format, object[] args, string formatParameterName, string argsParameterName)
        {
            NotNullOrEmpty(format, nameof(format));
            if (!FormatString.IsValidFormat(format, out var count, out _) || count < 0)
            {
                var message = $"Expected the format items to be [0..1..n). They format was: {format}";
                throw new ArgumentException(message, $"{formatParameterName},{argsParameterName}");
            }

            if (count == 0)
            {
                if (args == null || args.Length == 0)
                {
                    return;
                }

                var message = $"The format string: {format} contains no arguments but: {string.Join(",", args)} was passed as args";
                throw new ArgumentException(message, $"{formatParameterName},{argsParameterName}");
            }

            if (args == null || args.Length == 0)
            {
                var message = $"The format string: {format} contains {count} arguments but: no arguments were passed.";
                throw new ArgumentException(message, $"{formatParameterName},{argsParameterName}");
            }

            if (args.Length != count)
            {
                var message = $"The format string: {format} contains {count} arguments but: {args.Length} arguments were provided";
                throw new ArgumentException(message, $"{formatParameterName},{argsParameterName}");
            }
        }
コード例 #3
0
        private static bool TryGetFormatErrors(string key, IReadOnlyDictionary <CultureInfo, string> translations, out FormatError formatErrors)
        {
            int?count = null;

            foreach (var translation in translations.Values)
            {
                int  indexCount;
                bool?anyItemHasFormat;
                if (!FormatString.IsValidFormat(translation, out indexCount, out anyItemHasFormat))
                {
                    formatErrors = new FormatError(key, translations);
                    return(true);
                }

                if (count == null)
                {
                    count = indexCount;
                    continue;
                }

                if (count != indexCount)
                {
                    formatErrors = new FormatError(key, translations);
                    return(true);
                }
            }

            formatErrors = null;
            return(false);
        }
コード例 #4
0
        /// <summary>
        /// Call with Validate.IsValidFormat("First: {0:N}", 1, 2, 3..);
        /// Throws a <see cref="FormatException"/> if error(s) are found.
        /// </summary>
        /// <param name="format">The format string ex: 'First: {0:N}</param>
        /// <param name="args">The arguments.</param>
        public static void Format(string format, params object[] args)
        {
            int  count;
            bool?anyItemHasFormat;

            if (!FormatString.IsValidFormat(format, out count, out anyItemHasFormat))
            {
                throw new FormatException($"Invalid format string: \"{format}\".");
            }

            if (args == null || args.Length == 0)
            {
                if (count == 0)
                {
                    return;
                }

                throw new FormatException($"Invalid format string: \"{format}\" when no arguments.");
            }

            if (count != args.Length)
            {
                throw new FormatException($"Invalid format string: \"{format}\" for the arguments: {string.Join(", ", args)}.");
            }
        }
コード例 #5
0
        private static bool IsValidFormat(string format, int argumentCount)
        {
            if (!FormatString.IsValidFormat(format, out var count, out _))
            {
                return(false);
            }

            return(count == argumentCount);
        }
コード例 #6
0
        internal static bool FormatMatches(string format, object[] args)
        {
            if (!FormatString.IsValidFormat(format, out var count, out _) || count < 0)
            {
                return(false);
            }

            return(count == (args?.Length ?? 0));
        }
コード例 #7
0
        private static bool IsValidFormat(string format, int argumentCount)
        {
            int  count;
            bool?anyItemHasFormat;

            if (!FormatString.IsValidFormat(format, out count, out anyItemHasFormat))
            {
                return(false);
            }

            return(count == argumentCount);
        }
コード例 #8
0
        internal static bool FormatMatches(string format, object[] args)
        {
            int  count;
            bool?anyItemHasFormat;

            if (!FormatString.IsValidFormat(format, out count, out anyItemHasFormat) || count < 0)
            {
                return(false);
            }

            return(count == (args?.Length ?? 0));
        }
コード例 #9
0
        /// <summary>
        /// Call with Validate.IsValidFormat("First: {0:N}", 1.2);
        /// Throws a <see cref="FormatException"/> if error(s) are found.
        /// </summary>
        /// <typeparam name="T0">The type of <paramref name="arg0"/> generic to avoid boxing</typeparam>
        /// <typeparam name="T1">The type of <paramref name="arg1"/> generic to avoid boxing</typeparam>
        /// <param name="format">The format string ex: 'First: {0:N}</param>
        /// <param name="arg0">The first argument.</param>
        /// <param name="arg1">The second argument.</param>
        public static void Format <T0, T1>(string format, T0 arg0, T1 arg1)
        {
            if (!FormatString.IsValidFormat(format, out var count, out _))
            {
                throw new FormatException($"Invalid format string: \"{format}\".");
            }

            // not sure if we should bother with checking individual format items here
            if (count != 2)
            {
                throw new FormatException($"Invalid format string: \"{format}\" for the two arguments: {arg0}, {arg1}.");
            }
        }
コード例 #10
0
        /// <summary>
        /// Call with Validate.IsValidFormat("First: {0:N}", 1.2);
        /// Throws a <see cref="FormatException"/> if error(s) are found.
        /// </summary>
        /// <typeparam name="T0">The type of <paramref name="arg0"/> generic to avoid boxing</typeparam>
        /// <param name="format">The format string ex: 'First: {0:N}</param>
        /// <param name="arg0">The argument</param>
        public static void Format <T0>(string format, T0 arg0)
        {
            int  count;
            bool?anyItemHasFormat;

            if (!FormatString.IsValidFormat(format, out count, out anyItemHasFormat))
            {
                throw new FormatException($"Invalid format string: \"{format}\".");
            }

            // not sure if we should bother with checking individual format items here
            if (count != 1)
            {
                throw new FormatException($"Invalid format string: \"{format}\" for the single argument: {arg0}.");
            }
        }