我写了一个函数,其百分比-字符串编码,如下所示:更换
string percent_encode(string str)
{
string reserved =
// gen-delims
":/?#[]@"
// sub-delims
"!$&'()*+,;="
;
for(string::iterator i = str.begin(); i < str.end(); i++) {
int c = *i;
// replaces reserved, unreserved non-ascii and space characters.
if(c > 127 || c == 32 || reserved.find(*i) != string::npos) {
std::stringstream ss;
ss << std::hex << c;
str.replace(i, i + 1, "%" + ss.str());
}
}
return str;
}
当我调用该函数对于喜欢“一个& b”的一个字符串,一个out_of_range例外是抛出:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::replace
我用一个调试器跟踪这个异常,看到替换工作得很好,但它迭代超出end();
这是我得到的,当我看迭代“我”:
{_M_current = 0x7fc43d61bd78 "a&b"}
{_M_current = 0x7fc43d61bd79 "&b"}
{_M_current = 0x7fc43d61bd7a "b"}
{_M_current = 0x7fc43d61bd7b ""}
{_M_current = 0x7fc43d61bd7c "o = a&b\n"}
{_M_current = 0x7fc43d61bd7d " = a&b\n"}
,然后尝试更换“=”和失败的out_of_range例外。 我不明白,迭代器如何明显超越end()。
如果有人能解释我,这是如何可能的,因为我找不到网络中有人遇到同样的问题,我将不胜感激。
感谢和问候,
reeaal
编辑:
哎呀,我真的以为复杂的。 x) 这就是我现在解决它的方法。
string percent_encode(string str)
{
string reserved =
// gen-delims
":/?#[]@"
// sub-delims
"!$&'()*+,;="
;
std::stringstream ss;
for(string::iterator i = str.begin(); i < str.end(); i++) {
// encodes reserved, unreserved non-ascii and space characters.
int c = *i;
if(c > 126 || c == 32 || reserved.find(*i) != string::npos) {
ss << '%' << std::hex << c;
} else {
ss << *i;
}
}
return ss.str();
}
感谢迭戈:)
恩......好主意,没想过,谢谢。 :) – reeaal 2011-05-19 22:30:49
好的,你的解决方案的作品。它有一点点的开销,因为stringstream,但无论如何:) – 2011-05-19 22:44:08
你觉得怎么样? :)无论如何,我需要将其转换为十六进制表示。 – reeaal 2011-05-19 22:55:21