Exemplo n.º 1
0
        public static string CombineWith(this string path, params object[] thesePaths)
        {
            if (thesePaths.Length == 1 && thesePaths[0] == null)
            {
                return(path);
            }

            var sb = StringBuilderThreadStatic.Allocate();

            sb.Append(path.TrimEnd('/', '\\'));
            AppendPaths(sb, ToStrings(thesePaths));
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 2
0
        public static string Join <T>(this IEnumerable <T> values, string seperator)
        {
            var sb = StringBuilderThreadStatic.Allocate();

            foreach (var value in values)
            {
                if (sb.Length > 0)
                {
                    sb.Append(seperator);
                }
                sb.Append(value);
            }
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 3
0
        public static string Join <K, V>(this Dictionary <K, V> values, string itemSeperator, string keySeperator)
        {
            var sb = StringBuilderThreadStatic.Allocate();

            foreach (var entry in values)
            {
                if (sb.Length > 0)
                {
                    sb.Append(itemSeperator);
                }

                sb.Append(entry.Key).Append(keySeperator).Append(entry.Value);
            }
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 4
0
        public static string AppendUrlPathsRaw(this string uri, params string[] uriComponents)
        {
            var sb = StringBuilderThreadStatic.Allocate();

            sb.Append(uri.WithTrailingSlash());
            var i = 0;

            foreach (var uriComponent in uriComponents)
            {
                if (i++ > 0)
                {
                    sb.Append('/');
                }
                sb.Append(uriComponent);
            }
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 5
0
        public static string CombineWith(this string path, params string[] thesePaths)
        {
            if (path == null)
            {
                path = "";
            }

            if (thesePaths.Length == 1 && thesePaths[0] == null)
            {
                return(path);
            }
            var startPath = path.Length > 1 ? path.TrimEnd('/', '\\') : path;

            var sb = StringBuilderThreadStatic.Allocate();

            sb.Append(startPath);
            AppendPaths(sb, thesePaths);
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 6
0
        public static Type GetCachedGenericType(this Type type, params Type[] argTypes)
        {
            if (!type.IsGenericTypeDefinition)
            {
                throw new ArgumentException(type.FullName + " is not a Generic Type Definition");
            }

            if (argTypes == null)
            {
                argTypes = TypeConstants.EmptyTypeArray;
            }

            var sb = StringBuilderThreadStatic.Allocate()
                     .Append(type.FullName);

            foreach (var argType in argTypes)
            {
                sb.Append('|')
                .Append(argType.FullName);
            }

            var key = StringBuilderThreadStatic.ReturnAndFree(sb);

            if (GenericTypeCache.TryGetValue(key, out var genericType))
            {
                return(genericType);
            }

            genericType = type.MakeGenericType(argTypes);

            Dictionary <string, Type> snapshot, newCache;

            do
            {
                snapshot      = GenericTypeCache;
                newCache      = new Dictionary <string, Type>(GenericTypeCache);
                newCache[key] = genericType;
            } while (!ReferenceEquals(
                         Interlocked.CompareExchange(ref GenericTypeCache, newCache, snapshot), snapshot));

            return(genericType);
        }
Exemplo n.º 7
0
        public static string Dump(this Delegate fn)
        {
            var method = fn.GetType().GetMethod("Invoke");
            var sb     = StringBuilderThreadStatic.Allocate();

            foreach (var param in method.GetParameters())
            {
                if (sb.Length > 0)
                {
                    sb.Append(", ");
                }

                sb.AppendFormat("{0} {1}", param.ParameterType.Name, param.Name);
            }

            var methodName = fn.Method.Name;
            var info       = $"{method.ReturnType.Name} {methodName}({StringBuilderThreadStatic.ReturnAndFree(sb)})";

            return(info);
        }
Exemplo n.º 8
0
        public static string ToPascalCase(this string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(value);
            }

            if (value.IndexOf('_') >= 0)
            {
                var parts = value.Split('_');
                var sb    = StringBuilderThreadStatic.Allocate();
                foreach (var part in parts)
                {
                    var str = part.ToCamelCase();
                    sb.Append(char.ToUpper(str[0]) + str.SafeSubstring(1, str.Length));
                }
                return(StringBuilderThreadStatic.ReturnAndFree(sb));
            }

            var camelCase = value.ToCamelCase();

            return(char.ToUpper(camelCase[0]) + camelCase.SafeSubstring(1, camelCase.Length));
        }
Exemplo n.º 9
0
        public static string ToLowercaseUnderscore(this string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(value);
            }
            value = value.ToCamelCase();

            var sb = StringBuilderThreadStatic.Allocate();

            foreach (char t in value)
            {
                if (char.IsDigit(t) || (char.IsLetter(t) && char.IsLower(t)) || t == '_')
                {
                    sb.Append(t);
                }
                else
                {
                    sb.Append("_");
                    sb.Append(char.ToLowerInvariant(t));
                }
            }
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 10
0
        public static string IndentJson(this string json)
        {
            var indent = 0;
            var quoted = false;
            var sb     = StringBuilderThreadStatic.Allocate();

            for (var i = 0; i < json.Length; i++)
            {
                var ch = json[i];
                switch (ch)
                {
                case '{':
                case '[':
                    sb.Append(ch);
                    if (!quoted)
                    {
                        sb.AppendLine();
                        Times(++indent, () => sb.Append(Indent));
                    }
                    break;

                case '}':
                case ']':
                    if (!quoted)
                    {
                        sb.AppendLine();
                        Times(--indent, () => sb.Append(Indent));
                    }
                    sb.Append(ch);
                    break;

                case '"':
                    sb.Append(ch);
                    var escaped = false;
                    var index   = i;
                    while (index > 0 && json[--index] == '\\')
                    {
                        escaped = !escaped;
                    }
                    if (!escaped)
                    {
                        quoted = !quoted;
                    }
                    break;

                case ',':
                    sb.Append(ch);
                    if (!quoted)
                    {
                        sb.AppendLine();
                        Times(indent, () => sb.Append(Indent));
                    }
                    break;

                case ':':
                    sb.Append(ch);
                    if (!quoted)
                    {
                        sb.Append(" ");
                    }
                    break;

                default:
                    sb.Append(ch);
                    break;
                }
            }
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Exemplo n.º 11
0
        public static string Format(string serializedText)
        {
            if (string.IsNullOrEmpty(serializedText))
            {
                return(null);
            }

            var tabCount          = 0;
            var sb                = StringBuilderThreadStatic.Allocate();
            var firstKeySeparator = true;
            var inString          = false;

            for (var i = 0; i < serializedText.Length; i++)
            {
                var current  = serializedText[i];
                var previous = i - 1 >= 0 ? serializedText[i - 1] : 0;
                var next     = i < serializedText.Length - 1 ? serializedText[i + 1] : 0;

                if (current == JsWriter.MapStartChar || current == JsWriter.ListStartChar)
                {
                    if (previous == JsWriter.MapKeySeperator)
                    {
                        if (next == JsWriter.MapEndChar || next == JsWriter.ListEndChar)
                        {
                            sb.Append(current);
                            sb.Append(serializedText[++i]); //eat next
                            continue;
                        }

                        AppendTabLine(sb, tabCount);
                    }

                    sb.Append(current);
                    AppendTabLine(sb, ++tabCount);
                    firstKeySeparator = true;
                    continue;
                }

                if (current == JsWriter.MapEndChar || current == JsWriter.ListEndChar)
                {
                    AppendTabLine(sb, --tabCount);
                    sb.Append(current);
                    firstKeySeparator = true;
                    continue;
                }

                if (current == JsWriter.QuoteChar)
                {
                    sb.Append(current);
                    inString = !inString;
                    continue;
                }

                if (current == JsWriter.ItemSeperator && !inString)
                {
                    sb.Append(current);
                    AppendTabLine(sb, tabCount);
                    firstKeySeparator = true;
                    continue;
                }

                sb.Append(current);

                if (current == JsWriter.MapKeySeperator && firstKeySeparator)
                {
                    sb.Append(" ");
                    firstKeySeparator = false;
                }
            }

            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }