1

I've written a recursive method in C# that should indent strings. For example, this string:

for (int i = 0; i < sb.Length; i++)
{
   if (sb[i] == '{')
   {
      startIndex = i;
      break;
   }
}

should be converted to:

for (int i = 0; i < sb.Length; i++)
{
        if (sb[i] == '{')
        {
          startIndex = i;
          break;
        }
}

My method is (updated):

private static string IndentText(string t,bool first = true)
{
    if (first == false)
    {
        t = t.PadLeft(2);
    }

    int startIndex = t.IndexOf('{') + 1;
    int stopIndex = t.LastIndexOf('}') - 1;

    int blockLength = stopIndex - startIndex + 1;
    if (blockLength <= 1 )
    {
        return "";
    }

    string start = t.Substring(0, startIndex);
    string end = t.Substring(stopIndex + 1);
    string indentBlock = t.Substring(startIndex, blockLength);

    if (!CheckNestedBlocks(indentBlock))
    {
        return indentBlock;
    }

    return start + IndentText(indentBlock,false) + end;
}

private static bool CheckNestedBlocks(string t)
{
    for (int i = 0; i < t.Length; i++)
    {
        if (t[i] == '{')  // { and } always come in pairs, so I can check of only one of then
        {
            return true;
        }
    }
    return false;
}

But I'm getting a StackOverflow exception in mscorlib.dll

What is my mistake? Thanks in advance.

By the way, because I think I'm complicating this problem, is there a better (and working) way to indent strings like this?

matan129
  • 1,508
  • 2
  • 20
  • 33
  • 1
    those 2 code blocks at the beginning are the same just differently indented, i'm sorry i can see it now – nio Nov 09 '13 at 13:55
  • @nio That's the point of his exercise. – Sergey Kalinichenko Nov 09 '13 at 13:57
  • 1
    The two loops in your code can be replaced with `indexOf` and `lastIndexOf`. Besides, your code cannot possibly indent anything by more than 2, because you do not pass by how many levels to indent! Finally, indenting does not mean prepending two spaces to the entire block - this is something that you must do to each string. – Sergey Kalinichenko Nov 09 '13 at 14:00
  • What if your string looks like this? `..{ .. } { .. } ..` – John Alexiou Nov 09 '13 at 14:22
  • @ja72, alright, so what can I do? Can someone help me with a working indenting method? – matan129 Nov 09 '13 at 14:32
  • The approach you have is going to be buggy. I mean what if the opening and closing braces are at different indents already? You are getting into code formatting which is a rather complex task. I think you need to look into [CodeDOM](http://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx) parsing and formatting. – John Alexiou Nov 09 '13 at 14:36
  • I'll look into this, thanks. Also I want to point out that the input is always with no indentation what so ever – matan129 Nov 09 '13 at 14:38
  • Specifically `CodeDomProvider.Parse()` – John Alexiou Nov 09 '13 at 14:43
  • @matan129 you cannot enforce that. What is stopping pre-indented code to be used in the future? – John Alexiou Nov 09 '13 at 14:45
  • It is not "enforced"; I just want to use the IndentText methods in my own application, and I know that the input isn't indented. – matan129 Nov 09 '13 at 14:46
  • Related post: http://stackoverflow.com/q/2445177/380384 – John Alexiou Nov 09 '13 at 14:57

1 Answers1

4

You should not include the braces in the "block" that is passed in the recursive call:

        if (t[i] == '{')
        {
            startIndex = i + 1;   // Start one character beyond {
            break;
        }

        // ...

        if (t[i] == '}')
        {
            stopIndex = i - 1;    // Stop one character prior to }
            break;
        }
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • when I tried this the output was `Class car \n {}` (`car` is my test class name). It deleted all the content within the curly brackets! – matan129 Nov 09 '13 at 14:03
  • There are several issues in your code; my answer above just resolves the infinite recursion. To stop the method from losing the content inside the braces, you need to handle the "base case": what happens when `t` does not contain any braces? – Douglas Nov 09 '13 at 14:08
  • I've updated my code, but it still deletes the content in the braces. – matan129 Nov 09 '13 at 14:16