nhttpd simplify encodeString function

This commit is contained in:
Jacek Jendrzej
2015-03-31 17:02:48 +02:00
parent 0f2ac568c8
commit cc7c25eb21
2 changed files with 17 additions and 22 deletions

View File

@@ -216,29 +216,24 @@ std::string decodeString(std::string encodedString) {
//-----------------------------------------------------------------------------
// HTMLEncode std::string
//-----------------------------------------------------------------------------
std::string encodeString(std::string decodedString) {
unsigned int len = sizeof(char) * decodedString.length() * 5 + 1;
std::string result(len, '\0');
char *newString = (char *) result.c_str();
char one_char;
if (len == result.length()) // got memory needed
{
char *dstring = (char *) decodedString.c_str();
while ((one_char = *dstring++)) /* use the null character as a loop terminator */
{
if (isalnum(one_char))
*newString++ = one_char;
else
newString += snprintf(newString,result.length(), "&#%d;",
(unsigned char) one_char);
}
std::string encodeString(const std::string &decodedString)
{
std::string result="";
char buf[10]= {0};
*newString = '\0'; /* when done copying the string,need to terminate w/ null char */
result.resize((unsigned int) (newString - result.c_str()), '\0');
return result;
} else {
return "";
for (unsigned int i=0; i<decodedString.length(); i++)
{
const char one_char = decodedString[i];
if (isalnum(one_char)) {
result += one_char;
} else {
snprintf(buf,sizeof(buf), "&#%d;",(unsigned char) one_char);
result +=buf;
}
}
result+='\0';
result.reserve();
return result;
}
//-----------------------------------------------------------------------------