示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns>True if collection was shown completely</returns>
        public static bool ShowCollectionValue <T>(ICollectionValue <T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            string startdelim = "{ ", enddelim = " }";
            bool   showIndexes        = false;
            bool   showMultiplicities = false;
            //TODO: do not test here at run time, but select code at compile time
            //      perhaps by delivering the print type to this metod
            IList <T>       list;
            ICollection <T> coll = items as ICollection <T>;

            if ((list = items as IList <T>) != null)
            {
                startdelim = "[ ";
                enddelim   = " ]";
                //TODO: should have been (items as IIndexed<T>).IndexingSpeed
                showIndexes = list.IndexingSpeed == Speed.Constant;
            }
            else if (coll != null)
            {
                if (coll.AllowsDuplicates)
                {
                    startdelim = "{{ ";
                    enddelim   = " }}";
                    if (coll.DuplicatesByCounting)
                    {
                        showMultiplicities = true;
                    }
                }
            }

            stringbuilder.Append(startdelim);
            rest -= 2 * startdelim.Length;
            bool first    = true;
            bool complete = true;
            int  index    = 0;

            if (showMultiplicities)
            {
                foreach (KeyValuePair <T, int> p in coll.ItemMultiplicities())
                {
                    complete = false;
                    if (rest <= 0)
                    {
                        break;
                    }
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stringbuilder.Append(", ");
                        rest -= 2;
                    }
                    if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))
                    {
                        string multiplicityString = string.Format("(*{0})", p.Value);
                        stringbuilder.Append(multiplicityString);
                        rest -= multiplicityString.Length;
                    }
                }
            }
            else
            {
                foreach (T x in items)
                {
                    complete = false;
                    if (rest <= 0)
                    {
                        break;
                    }
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stringbuilder.Append(", ");
                        rest -= 2;
                    }
                    if (showIndexes)
                    {
                        string indexString = string.Format("{0}:", index++);
                        stringbuilder.Append(indexString);
                        rest -= indexString.Length;
                    }
                    complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
                }
            }
            if (!complete)
            {
                stringbuilder.Append("...");
                rest -= 3;
            }
            stringbuilder.Append(enddelim);
            return(complete);
        }
示例#2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="format"></param>
 /// <param name="formatProvider"></param>
 /// <returns></returns>
 public string ToString(string format, IFormatProvider formatProvider)
 {
     return(Showing.ShowString(this, format, formatProvider));
 }