How to make this function not prematurely split?
<p>I've written this function...</p>

<pre><code>internal static IEnumerable&lt;KeyValuePair&lt;char?, string&gt;&gt; SplitUnescaped(this string input, char[] separators)
{
    int index = 0;
    var state = new Stack&lt;char&gt;();

    for (int i = 0; i &lt; input.Length; ++i)
    {
        char c = input[i];
        char s = state.Count &gt; 0 ? state.Peek() : default(char);

        if (state.Count &gt; 0 &amp;&amp; (s == '\\' || (s == '[' &amp;&amp; c == ']') || ((s == '"' || s == '\'') &amp;&amp; c == s)))
            state.Pop();
        else if (c == '\\' || c == '[' || c == '"' || c == '\'')
            state.Push(c);
        if (state.Count == 0 &amp;&amp; separators.Contains(c))
        {
            yield return new KeyValuePair&lt;char?, string&gt;(c, input.Substring(index, i - index));
            index = i + 1;
        }
    }

    yield return new KeyValuePair&lt;char?, string&gt;(null, input.Substring(index));
}
</code></pre>

<p>Which splits a string on the given separators, as long as they aren't escaped, in quotes, or in brackets. Seems to work pretty well, but there's one problem with it.</p>

<p>There characters I want to split on include a space:</p>

<pre><code>{ '&gt;', '+', '~', ' ' };
</code></pre>

<p>So, given the string</p>

<pre><code>a &gt; b
</code></pre>

<p>I want it to split on <code>&gt;</code> and ignore the spaces, but given</p>

<pre><code>a b
</code></pre>

<p>I <em>do</em> want it to split on the space.</p>

<p>How can I fix the function?</p>
