I’m trying to parse text-formatting. I want to mark inline code, much like SO does, with backticks (`). The rule is supposed to be that if you want to use a backtick inside of an inline code element, You should use double backticks around the inline code.
like this:
“ mark inline code with backticks ( ` ) “
My parser seems to skip over the double backticks completely for some reason. Heres the code for the function that does the inline code parsing:
private string ParseInlineCode(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '`' && input[i - 1] != '\\')
{
if (input[i + 1] == '`')
{
string str = ReadToCharacter('`', i + 2, input);
while (input[i + str.Length + 2] != '`')
{
str += ReadToCharacter('`', i + str.Length + 3, input);
}
string tbr = "``" + str + "``";
str = str.Replace("&", "&");
str = str.Replace("<", "<");
str = str.Replace(">", ">");
input = input.Replace(tbr, "<code>" + str + "</code>");
i += str.Length + 13;
}
else
{
string str = ReadToCharacter('`', i + 1, input);
input = input.Replace("`" + str + "`", "<code>" + str + "</code>");
i += str.Length + 13;
}
}
}
return input;
}
If I use single backticks around something, it wraps it in the <code> tags correctly.
In the
while-loopyou look at the wrong index –
i + str.Length + 2instead ofi + str.Length + 3– and in turn you have to add the backtick in the body. It should probably beBut there are some more bugs in your code. The following line will cause an
IndexOutOfRangeExceptionif the first character of the input is a backtick.And the following line will cause an
IndexOutOfRangeExceptionif the input contains an odd number of separated backticks and the last character of the input is a backtick.You should probably refector your code into smaller methods and not handle to many cases inside a single method – that is very prone to bugs. If you have not jet written unit tests for the code I strongly suggest to do so. And because parsers are not really easy to test because of all kinds of invalid inputs you have to be prepared for you may have a look at PEX – a tool that automatically generates test cases for your code by analyzing all branching points and trying to take every possible code path.
I quickly started PEX and run it against the code – it found the
IndexOutOfRangeExceptionI thought of and some more. And of course PEX found the obviousNullReferenceExceptionsif the input is a null reference. Here are the inputs that PEX found to cause exceptions.My “fix” of your code changed the inputs that cause exceptions (and maybe also introduced new bugs). PEX caught the following in the modified code.
All three inputs did not cause exceptions in the original code while case 4 and 6 no longer cause exceptions in the modified code.