0

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

Cango
  • 100
  • 7
  • 1
    You mean you just want to indent? You should be able to do this in a single pass through the file. Can you post the solution you already have and ask a more directed question about your code? – lc. Nov 07 '12 at 05:52
  • 3
    What is exactly the question? – gdoron Nov 07 '12 at 05:52
  • I want to parse this syntax into a human readable form. I posted my current solution, but well i knwo its kinda bad. I need somehow a faster solution cause in a 2mb text file this solution takes ages :7 – Cango Nov 07 '12 at 05:57
  • Why do you use Regex.Split at the beginning? Is it faster than string.split()? Also, why Regex.Match() when string.indexOf() is good enough? Again, is it faster? – yu_ominae Nov 07 '12 at 06:05

1 Answers1

2

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();
    }
yu_ominae
  • 2,975
  • 6
  • 39
  • 76