【发布时间】:2017-08-21 04:15:51
【问题描述】:
我正在编写这个软件,让我在我的网站上的工作更轻松一些。我只是输入了一个脚本,它会用 span 为我突出显示代码。它正在到达那里,但我有一个问题。 (我目前正在努力突出 UnityC#)
我希望字符串是黄色的,包括双引号,但我现在尝试它的方式会导致无限循环。我知道我对引号做错了什么,但我不知道那是什么。请帮帮我:)
变量original由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;
}
}
当我运行它时,它将 cout: /dataValues.dat : "/dataValues.dat" 无限。
怎么了?
问候, 丹妮
【问题讨论】:
-
如果允许使用 C++11,请考虑使用
std::regex。它会很容易地为您做到这一点。 -
我的老教授坚持称他们为“引用字符”。引号是动词,不是名词;-)
-
你能给我们举个例子吗?
-
@Shravan40 你的例子是什么意思?我给了代码和输出
-
@danivdwerf 他的意思是输入和输出。以便我们验证您的问题