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 // HTMLEncode std::string
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::string encodeString(std::string decodedString) { std::string encodeString(const std::string &decodedString)
unsigned int len = sizeof(char) * decodedString.length() * 5 + 1; {
std::string result(len, '\0'); std::string result="";
char *newString = (char *) result.c_str(); char buf[10]= {0};
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);
}
*newString = '\0'; /* when done copying the string,need to terminate w/ null char */ for (unsigned int i=0; i<decodedString.length(); i++)
result.resize((unsigned int) (newString - result.c_str()), '\0'); {
return result; const char one_char = decodedString[i];
if (isalnum(one_char)) {
result += one_char;
} else { } else {
return ""; snprintf(buf,sizeof(buf), "&#%d;",(unsigned char) one_char);
result +=buf;
} }
}
result+='\0';
result.reserve();
return result;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -23,7 +23,7 @@ void correctTime(struct tm *zt);
std::string itoa(unsigned int conv); std::string itoa(unsigned int conv);
std::string itoh(unsigned int conv); std::string itoh(unsigned int conv);
std::string decodeString(std::string encodedString); std::string decodeString(std::string encodedString);
std::string encodeString(std::string decodedString); std::string encodeString(const std::string &decodedString);
std::string string_tolower(std::string str); std::string string_tolower(std::string str);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------