[英]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#)
(我目前正在突出显示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>());
原始变量是由
std::string original ((istreambuf_iterator<char>(currentFile)), istreambuf_iterator<char>());
, where currentFile
is the script I load in ,其中
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;
}
}
When I run this, it will cout: /dataValues.dat : <span class='yellow_code'>"/dataValues.dat"</span> infinite. 当我运行它时,它将提示:/dataValues.dat:<span class ='yellow_code'>“ / dataValues.dat” </ span>无限。
What is wrong? 怎么了?
Regards, Dani 问候,丹妮
The problem is here: pos = end_pos + 1;
问题在这里:
pos = end_pos + 1;
which can let your index before the string is it is shorter than your preamble. 这可以让您的索引比字符串短。
Let us see what happens with your example: 让我们看看您的示例发生了什么:
original
is bla..."/dataValues.dat"...
and end_pos
is pos + 16
original
是bla..."/dataValues.dat"...
和end_pos
是pos + 16
bla...<span class='yellow_code'>"/dataValues.dat"</span>...
end_pos
has not changed and points to the first l
of yellow! bla...<span class='yellow_code'>"/dataValues.dat"</span>...
end_pos
并未更改,并指向黄色的第一个l
! You must add to end_pos
the lengths of <span...>
and </span>
to make it point immediately after </span>
您必须在
end_pos
添加<span...>
和</span>
的长度,以使其指向</span>
You are not adding the size of the start and end tags that you add to the string. 您没有添加添加到字符串中的开始和结束标签的大小。 Here is a example of how you can do it
这是一个如何做的例子
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 更新
After looking at pos
, found
, and start_pos
it seemed like two of them were redundant. 在查看
pos
, found
和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
}
您应该std::regex
text = std::regex_replace(text,std::regex("string to be replaced"),"string with it earlier should be replaced");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.