Example #1
0
 /// <summary>
 /// Returns the smaller of two 32-bit signed integers.
 /// </summary>
 /// <param name="val1">The first of two 32-bit signed integers to compare. </param>
 /// <param name="val2">The second of two 32-bit signed integers to compare. </param>
 /// <returns>Parameter val1 or val2, whichever is smaller.</returns>
 public static int Min(int val1, int val2)
 {
     return(MathInternal.Min(val1, val2));
 }
Example #2
0
        /// <summary>
        /// Replaces the format items in a string with the string representations of corresponding objects in a specified array.
        /// </summary>
        /// <param name="format">A composite format string</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.</returns>
        public static string Format(string format, params object[] args)
        {
            var index     = 0;
            var alignment = 0;
            var chr       = '\0';
            var len       = format.Length;
            var fmt       = Empty;
            var token     = Empty;
            var output    = Empty;

            if (format is null)
            {
                throw new ArgumentNullException("format can't be null");
            }

            for (var i = 0; i < len; i++)
            {
                token = Empty;
                chr   = format[i];

                if (chr == '{')
                {
                    if (i + 1 == len)
                    {
                        throw new ArgumentException("Format error: no closed brace, column " + i);
                    }

                    if (format[i + 1] == '{')
                    {
                        output += chr;
                        i++;
                        continue;
                    }
                    else
                    {
                        alignment = 0;
                        fmt       = Empty;

                        for (i++; i < len; i++)
                        {
                            chr = format[i];

                            if (chr >= '0' && chr <= '9')
                            {
                                token += chr;
                            }
                            else if (chr == ',' || chr == ':' || chr == '}')
                            {
                                break;
                            }
                            else
                            {
                                throw new ArgumentException("Format error: wrong symbol at {}, column " + i);
                            }
                        }

                        if (token.Length > 0)
                        {
                            index = int.Parse(token);
                        }
                        else
                        {
                            throw new ArgumentException("Format error: empty {}, column " + i);
                        }

                        if (chr == ',')
                        {
                            if (format[i + 1] == '-')
                            {
                                token = "-";
                                i++;
                            }
                            else
                            {
                                token = Empty;
                            }

                            for (i++; i < len; i++)
                            {
                                chr = format[i];

                                if (chr >= '0' && chr <= '9')
                                {
                                    token += chr;
                                }
                                else if (chr == ':' || chr == '}')
                                {
                                    break;
                                }
                                else
                                {
                                    throw new ArgumentException("Format error: wrong symbol at alignment, column " + i);
                                }
                            }

                            if (token.Length > 0)
                            {
                                alignment = int.Parse(token);
                            }
                            else
                            {
                                throw new ArgumentException("Format error: empty alignment, column " + i);
                            }
                        }

                        if (chr == ':')
                        {
                            token = Empty;
                            for (i++; i < len; i++)
                            {
                                chr = format[i];

                                if (chr == '}')
                                {
                                    break;
                                }
                                else
                                {
                                    token += chr;
                                }
                            }

                            if (token.Length > 0)
                            {
                                fmt = token;
                            }
                            else
                            {
                                throw new ArgumentException("Format error: empty format after ':', column " + i);
                            }
                        }
                    }

                    if (chr != '}')
                    {
                        throw new ArgumentException("Format error: no closed brace, column " + i);
                    }

                    if (fmt.Length > 0)
                    {
                        var method = args[index].GetType().GetMethod("ToString", new Type[] { typeof(string) });
                        token = (method is null)
                            ? args[index].ToString()
                            : method.Invoke(args[index], new object[] { token }).ToString();
                    }
                    else
                    {
                        token = args[index].ToString();
                    }

                    if (alignment > 0)
                    {
                        output += token.PadLeft(alignment);
                    }
                    else if (alignment < 0)
                    {
                        output += token.PadRight(MathInternal.Abs(alignment));
                    }
                    else
                    {
                        output += token;
                    }
                }
                else if (chr == '}')
                {
                    if (i + 1 == len)
                    {
                        throw new ArgumentException("Format error: no closed brace, column " + i);
                    }

                    if (format[i + 1] == '}')
                    {
                        output += chr;
                        i++;
                    }
                    else
                    {
                        throw new ArgumentException("Format error: no closed brace, column " + i);
                    }
                }
                else
                {
                    output += chr;
                }
            }

            return(output);
        }
Example #3
0
 /// <summary>
 /// Returns the absolute value of a 32-bit signed integer.
 /// </summary>
 /// <param name="val">A number that is greater than Int32..::..MinValue, but less than or equal to Int32..::..MaxValue.</param>
 /// <returns>A 32-bit signed integer, x, such that 0 ≤ x ≤ Int32..::..MaxValue.</returns>
 public static int Abs(int val)
 {
     return(MathInternal.Abs(val));
 }