Esempio n. 1
0
        public static string ToWrappedString <T>(this IEnumerable <T> source, string sep, int wrap)
        {
            source = source.Ensure();

            if (sep == null)
            {
                sep = String.Empty;
            }

            if (wrap <= 0)
            {
                return(String.Join(sep, source));
            }

            var strings = ListPool <string> .AcquireObject();

            var values = ObjectPool <StringBuilder> .AcquireObject();

            try
            {
                strings.AddRange(source.Select(o => Convert.ToString(o)));

                if (strings.Count == 0)
                {
                    return(String.Empty);
                }

                var capacity = strings.Sum(s => s.Length + sep.Length) + (strings.Count / wrap);

                values.EnsureCapacity(capacity);

                var i = 0;

                foreach (var t in strings)
                {
                    values.Append(t);

                    if (++i < strings.Count)
                    {
                        values.Append(sep);

                        if (i % wrap == 0)
                        {
                            values.Append('\n');
                        }
                    }
                }

                return(values.ToString());
            }
            catch
            {
                return(String.Empty);
            }
            finally
            {
                ObjectPool.Free(ref strings);
                ObjectPool.Free(ref values);
            }
        }
Esempio n. 2
0
        public static string InvertCase(this string str)
        {
            if (String.IsNullOrWhiteSpace(str))
            {
                return(str ?? String.Empty);
            }

            var value = ObjectPool <StringBuilder> .AcquireObject();

            foreach (var c in str)
            {
                if (Char.IsLetter(c))
                {
                    value.Append(Char.IsLower(c) ? Char.ToUpper(c) : Char.IsUpper(c) ? Char.ToLower(c) : c);
                }
                else
                {
                    value.Append(c);
                }
            }

            var s = value.ToString();

            ObjectPool.Free(ref value);

            return(s);
        }
Esempio n. 3
0
        public static string ToUpperWords(this string str, bool strict)
        {
            if (String.IsNullOrWhiteSpace(str))
            {
                return(str ?? String.Empty);
            }

            char c = str[0], lc;

            var value = ObjectPool <StringBuilder> .AcquireObject();

            if (Char.IsLetter(c) && Char.IsLower(c))
            {
                c = Char.ToUpper(c);
            }

            value.Append(c);

            for (var i = 1; i < str.Length; i++)
            {
                c  = str[i];
                lc = str[i - 1];

                if (Char.IsLetter(c) && Char.IsLower(c))
                {
                    if (Char.IsWhiteSpace(lc) || lc == '>')
                    {
                        c = Char.ToUpper(c);
                    }
                    else if (!strict && !Char.IsDigit(lc) && !Char.IsSymbol(lc) && lc != '\'' && lc != '<')
                    {
                        if (Char.IsSeparator(lc) || Char.IsPunctuation(lc))
                        {
                            c = Char.ToUpper(c);
                        }
                    }
                }

                value.Append(c);
            }

            var s = value.ToString();

            ObjectPool.Free(ref value);

            return(s);
        }
Esempio n. 4
0
        public static string ToSimpleString(this TimeZoneInfo tzo, bool dst)
        {
            var build = ObjectPool <StringBuilder> .AcquireObject();

            if (tzo.Id == "UTC")
            {
                return(tzo.Id);
            }

            string value;

            if (dst)
            {
                value = tzo.DaylightName.Replace("Daylight Time", String.Empty);
            }
            else
            {
                value = tzo.StandardName.Replace("Standard Time", String.Empty);
            }

            foreach (var c in value)
            {
                if (!Char.IsWhiteSpace(c) && Char.IsLetter(c) && Char.IsUpper(c))
                {
                    build.Append(c);
                }
            }

            build.Append(dst ? "-DT" : "-ST");

            value = build.ToString();

            ObjectPool.Free(ref build);

            return(value);
        }
Esempio n. 5
0
        public static string ToSimpleString(this TimeSpan time, string format = "h-m-s")
        {
            var build = ObjectPool <StringBuilder> .AcquireObject();

            build.EnsureCapacity(format.Length * 2);

            var noformat  = false;
            var nopadding = false;
            var zeroValue = false;
            var zeroEmpty = 0;

            string fFormat, dFormat;
            object append;

            for (var i = 0; i < format.Length; i++)
            {
                if (format[i] == '#')
                {
                    noformat = !noformat;
                    continue;
                }

                if (noformat)
                {
                    build.Append(format[i]);
                    continue;
                }

                switch (format[i])
                {
                case '!':
                    nopadding = !nopadding;
                    continue;
                }

                fFormat = zeroEmpty > 0 ? (nopadding ? "{0:#.#}" : "{0:#.##}") : (nopadding ? "{0:0.#}" : "{0:0.##}");
                dFormat = zeroEmpty > 0 ? (nopadding ? "{0:#}" : "{0:##}") : (nopadding ? "{0:0}" : "{0:00}");

                append = null;

                switch (format[i])
                {
                case '<':
                    ++zeroEmpty;
                    break;

                case '>':
                {
                    if (zeroEmpty == 0 || (zeroEmpty > 0 && --zeroEmpty == 0))
                    {
                        zeroValue = false;
                    }
                }
                break;

                case '\\':
                {
                    if (i + 1 < format.Length)
                    {
                        append = format[++i];
                    }
                }
                break;

                case 'x':
                    append = ToSimpleString(TimeZoneInfo.Utc, false);
                    break;

                case 'X':
                    append = ToSimpleString(TimeZoneInfo.Utc, DateTime.UtcNow.IsDaylightSavingTime());
                    break;

                case 'z':
                    append = ToSimpleString(TimeZoneInfo.Local, false);
                    break;

                case 'Z':
                    append = ToSimpleString(TimeZoneInfo.Local, DateTime.Now.IsDaylightSavingTime());
                    break;

                case 'D':
                {
                    append    = String.Format(fFormat, time.TotalDays);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'H':
                {
                    append    = String.Format(fFormat, time.TotalHours);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'M':
                {
                    append    = String.Format(fFormat, time.TotalMinutes);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'S':
                {
                    append    = String.Format(fFormat, time.TotalSeconds);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'd':
                {
                    append    = String.Format(dFormat, time.Days);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'h':
                {
                    append    = String.Format(dFormat, time.Hours);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'm':
                {
                    append    = String.Format(dFormat, time.Minutes);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 's':
                {
                    append    = String.Format(dFormat, time.Seconds);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                default:
                    append = format[i];
                    break;
                }

                if (append != null && (!zeroValue || zeroEmpty <= 0))
                {
                    build.Append(append);
                }
            }

            var value = build.ToString();

            ObjectPool.Free(ref build);

            return(value);
        }
Esempio n. 6
0
        public static string ToSimpleString(this DateTime date, string format = "t D d M y")
        {
            var build = ObjectPool <StringBuilder> .AcquireObject();

            build.EnsureCapacity(format.Length * 2);

            var noformat = false;

            for (var i = 0; i < format.Length; i++)
            {
                if (format[i] == '#')
                {
                    noformat = !noformat;
                    continue;
                }

                if (noformat)
                {
                    build.Append(format[i]);
                    continue;
                }

                switch (format[i])
                {
                case '\\':
                    build.Append((i + 1 < format.Length) ? Convert.ToString(format[++i]) : String.Empty);
                    break;

                case 'x':
                case 'z':
                {
                    var utc = date.Kind == DateTimeKind.Utc;
                    var tzo = utc ? TimeZoneInfo.Utc : TimeZoneInfo.Local;

                    build.Append(ToSimpleString(tzo, false));
                }
                break;

                case 'X':
                case 'Z':
                {
                    var utc = date.Kind == DateTimeKind.Utc;
                    var tzo = utc ? TimeZoneInfo.Utc : TimeZoneInfo.Local;

                    build.Append(ToSimpleString(tzo, date.IsDaylightSavingTime()));
                }
                break;

                case 'D':
                    build.Append(date.DayOfWeek);
                    break;

                case 'd':
                    build.Append(date.Day);
                    break;

                case 'M':
                    build.Append(GetMonth(date));
                    break;

                case 'm':
                    build.Append(date.Month);
                    break;

                case 'y':
                    build.Append(date.Year);
                    break;

                case 't':
                {
                    var tf = String.Empty;

                    if (i + 1 < format.Length)
                    {
                        if (format[i + 1] == '@')
                        {
                            ++i;

                            while (++i < format.Length && format[i] != '@')
                            {
                                tf += format[i];
                            }
                        }
                    }

                    build.Append(ToSimpleString(date.TimeOfDay, !String.IsNullOrWhiteSpace(tf) ? tf : "h-m-s"));
                }
                break;

                default:
                    build.Append(format[i]);
                    break;
                }
            }

            var value = build.ToString();

            ObjectPool.Free(ref build);

            return(value);
        }