Question

Hi all i kinda need some help at creating a function for turning a plain syntax in human readable format:

Syntax looks like this:

[OPENTAG]
(type)name:value
[OPENTAG]
(type)name:value
(type)name:value
(type)name:value
[/CLOSETAG]
[/CLOSETAG]

Would like to turn this into this:

    [OPENTAG]
         (type)name:value
         [OPENTAG]
             (type)name:value
             (type)name:value
             (type)name:value
         [/CLOSETAG]
    [/CLOSETAG]

    private string textFormater(string input)
    {
        string[] lines = Regex.Split(input, "\r\n");
        int tabs = 0;
        string newtext = "";
        foreach (string line in lines)
        {

            Match m = Regex.Match(line, "\\[.*\\]");
            bool isTop;
            bool isTopClose = false;
            string tabtext = "";

            if (m.Success)
            {
                if (line.Contains("/"))
                {
                    tabs--;
                    isTopClose = true;
                }
                else
                {
                    tabs++;
                }
                isTop = true;
            }
            else
            {
                isTop = false;
            }

            if (isTop && !isTopClose && tabs == 1)
            {
                newtext += line;
            }
            else if (isTop && !isTopClose)
            {
                for (int i = 1; i <= tabs - 1; i++)
                {
                    tabtext += "\t";
                }
                newtext += "\r\n" + tabtext + line;
            }
            else
            {
                for (int i = 1; i <= tabs; i++)
                {
                    tabtext += "\t";
                }
                newtext += "\r\n" + tabtext + line;
            }

        }
        return newtext;
    }

I have atm a solution but the code is so messy and slow, in a 2mb file it takes ages :) Thanks for your help!

Cheers

Was it helpful?

Solution

Try to use a StringBuilder for your output text instead of just a string, that should speed things up.

EDIT:

Does this do what you want?

private static string textFormater2(string input)
    {
        string[] lines = Regex.Split(input, "\r\n");
        int tabCount = 0;
        StringBuilder output = new StringBuilder();

        using (StringReader sr = new StringReader(input))
        {
            string l;
            while (!string.IsNullOrEmpty(l = sr.ReadLine()))
            {
                if (l.Substring(0, 1) == "[")
                    if (l.Contains('/'))
                        tabCount--;

                string tabs = string.Empty;
                for (int i = 0; i < tabCount; i++)
                    tabs += "\t";

                output.AppendLine(tabs + l);

                if (l.Substring(0, 1) == "[")
                    if (!l.Contains('/'))
                        tabCount++;
            }
        }

        return output.ToString();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top