I would like to know if my function that checks if a string syntax is correct. It's currently very messy, but seems to do the job.
//Example Valid String
[12345678]
{
something
whatever
}
Function:
private static bool CheckIfSyntaxIsValid(string input)
{
int mode = 1;
char last = ' ';
foreach (char c in input)
{
if (last == '{' && c == '}')
{
last = ' ';
mode = 1;
continue;
}
if (last == '{' && c == '{')
return false;
if (last == '[' && c == ']')
{
last = ' ';
mode = 2;
continue;
}
if (last == '[' && !Char.IsDigit(c))
return false;
if (c == '[')
{
if (mode == 2)
return false;
last = c;
mode = 1;
}
if (c == '{')
{
if (mode == 1)
return false;
last = c;
mode = 2;
}
if (c == ']' || c == '}')
return false;
}
if (last != ' ')
return false;
return true;
}
The rule is basically:
[]
must contain only numbers/digits- It must start with
[]
before{}
, and they must come after each other
So, [] {} [] {}
etc. Anything else is simply invalid.
Okay, not everything else is invalid. Text outside of those are ignored, so the only thing that matters is that the bracket orders are correct, etc.
[208208]{ something, {whatever}}
be valid, or invalid input? \$\endgroup\$