Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:42:02+00:00 2026-05-15T03:42:02+00:00

I’m trying to parse text-formatting. I want to mark inline code, much like SO

  • 0

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("&", "&amp;");
                    str = str.Replace("<", "&lt;");
                    str = str.Replace(">", "&gt;");
                    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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:42:03+00:00Added an answer on May 15, 2026 at 3:42 am

    In the while-loop

    while (input[i + str.Length + 2] != '`')
    {
        str += ReadToCharacter('`', i + str.Length + 3, input);
    }
    

    you look at the wrong index – i + str.Length + 2 instead of i + str.Length + 3 – and in turn you have to add the backtick in the body. It should probably be

    while (input[i + str.Length + 3] != '`')
    {
        str += '`' + ReadToCharacter('`', i + str.Length + 3, input);
    }
    

    But there are some more bugs in your code. The following line will cause an IndexOutOfRangeException if the first character of the input is a backtick.

     if (input[i] == '`' && input[i - 1] != '\\')
    

    And the following line will cause an IndexOutOfRangeException if the input contains an odd number of separated backticks and the last character of the input is a backtick.

    if (input[i + 1] == '`')
    

    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 IndexOutOfRangeException I thought of and some more. And of course PEX found the obvious NullReferenceExceptions if the input is a null reference. Here are the inputs that PEX found to cause exceptions.

    case1 = "`"
    
    case2 = "\0`"
    
    case3 = "\0``"
    
    case4 = "\0`\0````````````\u0001``````````````\0\0\0\0\0\0\0\0\0\0\0````"
    
    case5 = "\0`\0````````````\u0001``````````````\0\0\0\0\0\0\0\0\0\0\0```\0````````````\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0`"
    
    case6 = "\0`\0````````````\u0001``````````````\0\0\0\0\0\0\0\0\0\0\0```\0````````````\0\0\0\0\0\0\0\0\0\0``<\0\0`````````````````````````````````````````````````````````````````````````````````````\0\0\0\0\0\0\0\0\0\0``<\0\0```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````\0\0\0\0\0\0\0\0\0`\0```````````````"
    

    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.

    case7 = "\0```"
    
    case8 = "\0`\0````````````\u0001``````````````\0\0\0\0\0\0\0\0\0\0\0```\0`\0"
    
    case9 = "\0`\0````````````\u0001``````````````\0\0\0\0\0\0\0\0\0\0\0```\0````````````\0\0\0\0\0\0\0\0\0\0``<\0\0`````````````````````````````````````````````````````````````````````````````````````\0\0\0\0\0\0\0\0\0\0``\0`\0`\0``"
    

    All three inputs did not cause exceptions in the original code while case 4 and 6 no longer cause exceptions in the modified code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Perhaps: SELECT a.Adjectives, b.Content FROM A, B WHERE b.Content Like… May 15, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer You could attach an onmousemove handler to the element, and… May 15, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer You could setup a repo at the root, and: declare… May 15, 2026 at 2:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.