mardi 28 mars 2017

c++ - Replace string with double quotes to a string with double quotes

I an writing this software the make my work on my website a little easier. I just put in a script and it will highlight the code for me with spans. It is getting there, but I have a problem. (I am currently working on highlighting UnityC#)

I want strings to be yellow, including the double quotation marks, but the way I try it now causes an infinite loop. I know I am doing something wrong with the quotation marks, but I can't figure out what that is. Please help me :)

The variable original is created by std::string original ((istreambuf_iterator<char>(currentFile)), istreambuf_iterator<char>());, where currentFile is the script I load in

std::size_t pos = 0;
while(original.find("\"", pos) != std::string::npos)
{
    //find the first occurrence of the quote
    std::size_t found = original.find("\"", pos);
    if(found != std::string::npos)
    {
        //save the start position of the string
        std::size_t start_pos = found;
        //save the end position by searching for the next quotation mark
        std::size_t end_pos = original.find_first_of("\"", start_pos+1);
        //Calculate the size of the string
        std::size_t length = end_pos-start_pos;
        //Make a copy of the word without the quotation marks
        std::string originalWord = original.substr(start_pos+1, length-1);
        //Make the new word with span, original word(without the quotation marks) and add quotation marks around the word
        std::string newWord = std::string("<span class='yellow_code'>") + "\"" + originalWord + "\"" + "</span>";
        std::cout<<originalWord<<" : "<<newWord<<std::endl;
        //Replace the string WITH the quotation marks for the newWord
        original.replace(start_pos, length+1, newWord);
        //Set the position to after the string
        pos = end_pos+1;
    }
    else
    {
        pos = found+1;
    }
}

When I run this, it will cout: /dataValues.dat : <span class='yellow_code'>"/dataValues.dat"</span> infinite.

What is wrong?

Regards, Dani

Aucun commentaire:

Enregistrer un commentaire