std::string replace を用いた文字の置換
Posted: 2012年12月12日(水) 13:30
お世話になっております。chibagoです。
以下の様なプログラムで文字の置換を行っております。
置換した文字を標準出力で確認すると問題なく動作しているのが
確認出来るのですが、テストケースで確認すると落ちてしまいます。
テスト自体は失敗に終わりますので、
照合した後に原因究明のため標準出力で情報を確認しております。
Hello World!
Hello World!
13
12
とくに、文章の後に余計なスペースが入っているわけでもないのですが、
置換した文のほうは、文字数が多くなっております。
何が原因なのでしょうか。
ご指導いただければ幸いです。
以下の様なプログラムで文字の置換を行っております。
///header///
static std::string replace_string(const std::string base,
const std::string from,
const std::string to);
///cpp////
std::string StringUtil::replace_string(const std::string base,
const std::string from,
const std::string to){
std::string result = base;
std::string::size_type pos = 0;
while(pos = result.find(from, pos), pos != std::string::npos){
result.replace(pos, from.length(), to);
result += to.length();
}
return result;
}
確認出来るのですが、テストケースで確認すると落ちてしまいます。
std::string base = "Hello (XXX)!";
std::string from = "(XXX)";
std::string to = "World";
std::string result = StringUtil::replace_string(base, from, to);
std::string reference = "Hello World!";
BOOST_CHECK_EQUAL(result, reference);
std::cout<<result<<std::endl;
std::cout<<reference<<std::endl;
std::cout<<result.size()<<std::endl;
std::cout<<reference.size()<<std::endl;
照合した後に原因究明のため標準出力で情報を確認しております。
Hello World!
Hello World!
13
12
とくに、文章の後に余計なスペースが入っているわけでもないのですが、
置換した文のほうは、文字数が多くなっております。
何が原因なのでしょうか。
ご指導いただければ幸いです。