2017-03-28 386 views
1

我寫這個軟件使我在網站上的工作變得更容易一些。我只是寫了一個腳本,它會突出顯示我的跨代碼。它到達那裏,但我有一個問題。 (我目前正致力於突出UnityC#)C++ - 用雙引號將字符串替換爲帶雙引號的字符串

我想字符串是黃色的,包括雙引號,但現在我嘗試它的方式導致無限循環。我知道我在引號中做錯了什麼,但我無法弄清楚這是什麼。請幫我:)

變量原來由std::string original ((istreambuf_iterator<char>(currentFile)), istreambuf_iterator<char>());,其中currentFile是腳本我

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; 
    } 
} 

加載當我運行它,它會創建清點:/dataValues.dat:<跨度類= 'yellow_code'>「/dataValues.dat」</span >無限。

出了什麼問題?

問候, 達尼

+1

如果你允許使用C++ 11,可以考慮使用'的std :: regex'。它會很容易地爲你做到這一點。 –

+1

我的老教授堅持把他們稱爲「引號人物」。引用是動詞,而不是名詞;-) – Bathsheba

+0

你能給我們一個例子嗎? – Shravan40

回答

3

的問題是在這裏:pos = end_pos + 1;它可以讓你的索引之前的字符串是它比你的序言短。

讓我們來看看你的榜樣會發生什麼:

  • 原本originalbla..."/dataValues.dat"...end_pospos + 16
  • 更換後你bla...<span class='yellow_code'>"/dataValues.dat"</span>...end_pos沒有改變,點的黃色第一l
  • 在未來你會發現搜索一樣了引號的字符串...

您必須添加到end_pos<span...></span>長度,使它指向後立即</span>

+0

謝謝你,我覺得啞 – danivdwerf

0

你應該std::regex

text = std::regex_replace(text,std::regex("string to be replaced"),"string with it earlier should be replaced"); 
+0

我很欣賞這個答案,但另一個實際上給了我一個問題的答案,而不是告訴我使用一些東西。 – danivdwerf

2

您不添加尺寸您添加到字符串的開始和結束標記。 這裏是你如何能做到這

std::string start_tag = "<span class='yellow_code'>"; 
std::string end_tag = "\"</span>"; 

std::size_t pos = 0; 
while(original.find("\"", pos) != std::string::npos) 
{ 
    std::size_t found = original.find("\"", pos); 
    if(found != std::string::npos) 
    { 
     std::size_t start_pos = found; 
     std::size_t end_pos = original.find_first_of("\"", start_pos+1); 
     if (end_pos == std::string::npos) // If the number of quotation characters is odd you would get undefined behavior 
      break; 
     std::size_t length = end_pos - start_pos; 
     std::string originalWord = original.substr(start_pos+1, length-1); 
     std::string newWord = start_tag + "\"" + originalWord + end_tag; // CHANGED: Added the start and end tags as std::string 
     std::cout<<originalWord<<" : "<<newWord<<std::endl; 
     original.replace(start_pos, length+1, newWord); 
     pos = end_pos + start_tag.size() + end_tag.size(); // CHANGED: Added the size of the two tags 
    } 
} 

UPDATE

看着posfound後一個例子,start_pos它似乎像他們兩個人是多餘的。

std::size_t start_pos = 0; 
while((start_pos = original.find("\"", start_pos)) != std::string::npos) 
{ 
    std::size_t end_pos = original.find_first_of("\"", start_pos+1); 
    if (end_pos == std::string::npos) // If the number of quotation characters is odd you would get undefined behavior 
     break; 
    std::size_t length = end_pos-start_pos; 
    std::string originalWord = original.substr(start_pos+1, length-1); 
    std::string newWord = start_tag + "\"" + originalWord + end_tag; // CHANGED: Added the start and end tags as std::string 
    std::cout << originalWord << " : " << newWord <<std::endl; 
    original.replace(start_pos, length+1, newWord); 
    start_pos = end_pos + start_tag.size() + end_tag.size(); // CHANGED: Added the size of the two tags 
} 

試試吧online

+0

我覺得愚蠢的,非常感謝 – danivdwerf

+1

@danivdwerf看着你的問題upvotes的數量,很明顯,你的問題是真實的,切合主題,又有趣。所以基本上,不是愚蠢的! – Jonas