Esempio n. 1
0
        /// <summary>
        /// Format the provided format string with the provided arguments.
        /// </summary>
        /// <remarks>
        /// This should work like <see cref="System.String#Format"/>.
        /// </remarks>
        /// <returns>The formatted string.</returns>
        /// <param name="args">Arguments to be inserted into the formatted output.</param>
        public static utf8 Format(utf8 fmt, params object[] args)
        {
            var ub = new Utf8Builder();

            ub.AppendFormat(fmt, args);
            return(ub.ToUtf8());
        }
Esempio n. 2
0
 /// <summary>
 /// Create a Utf8Enumerator from the given utf8 string.
 /// </summary>
 public Utf8Enumerator(utf8 data)
 {
     _data          = data._bytes.Array;
     _offset        = data._bytes.Offset;
     _count         = data._bytes.Count;
     _current       = new UtfIndex();
     _current.Index = -1;
     _nextIndex     = 0;
 }
Esempio n. 3
0
        /// <summary>
        /// Parse a string into a long with the given radix.
        /// </summary>
        public static long ParseLong(utf8 str, int radix = 10)
        {
            long v;

            if (TryParseLong(str, out v, radix))
            {
                return(v);
            }
            throw new ArgumentException("input string was not in the correct format");
        }
Esempio n. 4
0
        /// <summary>
        /// Try to parse a string into an integer with the given radix.
        /// </summary>
        /// <returns><c>true</c> if the value could be successfully parsed, otherwise <c>false</c></returns>
        /// <param name="str">The string to parse.</param>
        /// <param name="v">The output integer.</param>
        /// <param name="radix">The radix in which the integer has been formatted.</param>
        public static bool TryParseInt(utf8 str, out int v, int radix = 10)
        {
            long vv;

            if (TryParseLong(str, out vv, radix))
            {
                v = (int)vv;
                return(true);
            }
            v = 0;
            return(false);
        }
Esempio n. 5
0
 /// <summary>
 /// Check the equality with the given utf8 string.
 /// </summary>
 public bool Equals(utf8 other)
 {
     if (this.Length != other.Length)
     {
         return(false);
     }
     for (int i = 0; i < this.Length; i++)
     {
         if (this[i] != other[i])
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 6
0
 /// <summary>
 /// Determine whether this string ends with the target string.
 /// </summary>
 public bool EndsWith(utf8 other)
 {
     if (other.Length > Length)
     {
         return(false);
     }
     for (int i = other.Length - 1; i <= 0; i--)
     {
         if (this[Length - i] != other[i])
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 7
0
 /// <summary>
 /// Determine whether this string begins with the target string.
 /// </summary>
 public bool StartsWith(utf8 other)
 {
     if (other.Length > Length)
     {
         return(false);
     }
     for (int i = 0; i < other.Length; i++)
     {
         if (this[i] != other[i])
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 8
0
        /// <summary>
        /// Try to parse a string into an integer with the given radix.
        /// </summary>
        /// <returns><c>true</c> if the value could be successfully parsed, otherwise <c>false</c></returns>
        /// <param name="str">The string to parse.</param>
        /// <param name="v">The output long.</param>
        /// <param name="radix">The radix in which the integer has been formatted.</param>
        public static bool TryParseLong(utf8 str, out long v, int radix = 10)
        {
            if (radix > 36)
            {
                throw new ArgumentException("radix must be at most 36");
            }
            if (str.Length == 0)
            {
                throw new ArgumentException("string was empty");
            }
            bool negative = str[0] == (byte)'-';

            if (negative)
            {
                str = str.Substring(1);
            }
            long acc = 0;

            for (int i = 0; i < str.Length; i++)
            {
                var digit = str[i];
                for (int j = 0; j < radix; j++)
                {
                    if (digit == digits[j])
                    {
                        acc *= radix;
                        acc += j;
                        goto found;
                    }
                }
                v = 0;
                return(false);

                found : {}
            }
            if (negative)
            {
                acc *= -1;
            }
            v = acc;
            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// Locate the target string within this string.
        /// </summary>
        public int IndexOf(utf8 other)
        {
            // Naive approach.
            var end = Length - other.Length;

            for (int i = 0; i < end; i++)
            {
                for (int j = 0; j < other.Length; j++)
                {
                    if (this[i + j] != other[j])
                    {
                        goto next;
                    }
                }
                return(i);

                next : {}
            }
            return(-1);
        }
Esempio n. 10
0
 /// <summary>
 /// Append the specified utf8 string.
 /// </summary>
 /// <param name="str">String.</param>
 public void Append(utf8 str)
 {
     _out.Write(str._bytes.Array, str._bytes.Offset, str._bytes.Count);
 }
Esempio n. 11
0
 /// <summary>
 /// Append a formatted string.
 /// </summary>
 /// <param name="provider">The format provider to use to format arguments.</param>
 /// <param name="fmt">The format string.</param>
 /// <param name="args">Format arguments to use to generate the formatted value.</param>
 public void AppendFormat(IFormatProvider provider, utf8 fmt, params object[] args)
 {
     while (fmt.Length > 0)
     {
         var nextCmd = fmt.IndexOf('{');
         if (nextCmd < 0)
         {
             Append(fmt);
             return;
         }
         Append(fmt.Substring(0, nextCmd));
         fmt = fmt.Substring(nextCmd + 1);
         if (fmt.StartsWith(OBRACE))
         {
             fmt = fmt.Substring(1);
             Append(OBRACE);
             continue;
         }
         var end = fmt.IndexOf('}');
         var cmd = fmt.Substring(0, end);
         fmt = fmt.Substring(end + 1);
         var  opts = cmd.IndexOf(':');
         int  index;
         utf8 indexStr;
         utf8 argFormat;
         if (opts < 0)
         {
             argFormat = utf8.Empty;
             indexStr  = cmd;
         }
         else
         {
             indexStr  = cmd.Substring(0, opts);
             argFormat = cmd.Substring(opts + 1);
         }
         if (!utf8.TryParseInt(indexStr, out index))
         {
             throw new FormatException(
                       string.Format("Invalid format string: '{0}' is not a valid argument specifier", indexStr));
         }
         if (index < 0 || index >= args.Length)
         {
             throw new FormatException(
                       string.Format("Invalid format string: referenced argument {0}, provided {1} arguments", index, args.Length));
         }
         var arg = args[index];
         if (arg == null)
         {
             // null -> empty
             continue;
         }
         if (argFormat.IsEmpty)
         {
             Append(arg.ToString());
         }
         else
         {
             var formattable = arg as IFormattable;
             if (formattable == null)
             {
                 throw new FormatException(
                           string.Format("Invalid format string: argument {0} has format options {1}, but it does not implement IFormattable", index, argFormat));
             }
             Console.WriteLine("formatting {0} with options {1}", formattable, argFormat);
             Append(formattable.ToString(argFormat.ToString(), provider));
         }
     }
 }
Esempio n. 12
0
 /// <summary>
 /// Append a formatted string.
 /// </summary>
 /// <param name="fmt">The format string.</param>
 /// <param name="args">Format arguments to use to generate the formatted value.</param>
 public void AppendFormat(utf8 fmt, params object[] args)
 {
     AppendFormat(null, fmt, args);
 }
Esempio n. 13
0
 /// <summary>
 /// Append the given string and insert a following newline.
 /// </summary>
 public void AppendLine(utf8 str)
 {
     Append(str);
     _out.WriteByte((byte)'\n');
 }
Esempio n. 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:FastString.ReverseUtf8Enumerator"/> struct.
 /// </summary>
 /// <param name="data">The data to iterate over.</param>
 public ReverseUtf8Enumerator(utf8 data)
 {
     _data          = data;
     _current       = new UtfIndex();
     _current.Index = _data.Length;
 }
Esempio n. 15
0
 /// <summary>
 /// Determine whether this string contains the target string.
 /// </summary>
 public bool Contains(utf8 other)
 {
     return(IndexOf(other) >= 0);
 }
Esempio n. 16
0
 /// <summary>
 /// Parse a string into an integer with the given radix.
 /// </summary>
 public static int ParseInt(utf8 str, int radix = 10)
 {
     return((int)ParseLong(str, radix));
 }