As the observing readers of my weblog already I know, I'm working on an
HTTP stack as part of a high-performance Ruby servlet container that I want to implement. This weekend, I finished the work on the HTTP stack, and the tests so far look good, so everything seems quite stable. That means: refactoring. Currently, there's only one piece of code that doesn't look to good in my eyes, as it is to "un-C++-ish", but as I'm not a regular user of the C++ language and especially its standard library, and have no good ideas how.
That piece of code is an URL decoder, and looks like the following:
static inline int h2c(char hi, char lo) {
std::string s;
int value = util::PARSE_FAILED;
s += hi;
s += lo;
std::istringstream is(s);
is >> std::hex >> value;
return value;
}
std::string util::url_decode(const std::string& s) {
std::string result;
std::istringstream iss(s);
char c;
for (iss >> c;!iss.eof();iss >> c) {
if ('+' == c) {
result += ' ';
} else if ('%' == c) {
char c1 = 0, c2 = 0;
iss >> c1;
iss >> c2;
if (!iss.eof()) {
int r = h2c(c1,c2);
if (r != PARSE_FAILED) {
result += static_cast<char>(r);
}
}
} else {
result += c;
}
}
return result;
}
Any recommendations?