private string GetNextPiece (ref string remaining, out char next)
{
bool inString = false;
int end = 0;
remaining = remaining.TrimStart ();
if (remaining.Length == 0) {
next = Char.MaxValue;
return null;
}
piece = piece ?? new StringBuilder ();
piece.Length = 0;
// If we're inside a quoted string we append all chars to our piece until we hit the ending quote.
while (end < remaining.Length && (inString || (remaining [end] != '}' && remaining [end] != ',' && remaining [end] != '='))) {
if (remaining [end] == '\'')
inString = !inString;
// If this is an escape char, consume it and append the next char to our piece.
if (remaining [end] == '\\') {
end ++;
if (end == remaining.Length)
break;;
}
piece.Append (remaining [end]);
end++;
}
if (end == remaining.Length && !remaining.EndsWith ("}"))
throw new Exception ("Binding did not end with '}'");
if (end == 0) {
next = Char.MaxValue;
return null;
}
next = remaining [end];
remaining = remaining.Substring (end + 1);
// Whitespace is trimmed from the end of the piece before stripping
// quote chars from the start/end of the string.
while (piece.Length > 0 && char.IsWhiteSpace (piece [piece.Length - 1]))
piece.Length --;
if (piece.Length >= 2) {
char first = piece [0];
char last = piece [piece.Length - 1];
if ((first == '\'' && last == '\'') || (first == '"' && last == '"')) {
piece.Remove (piece.Length - 1, 1);
piece.Remove (0, 1);
}
}
return piece.ToString ();
}