From cc7c25eb21bb11f8f8a8879e1ce19718cd3cc85f Mon Sep 17 00:00:00 2001 From: Jacek Jendrzej Date: Tue, 31 Mar 2015 17:02:48 +0200 Subject: [PATCH] nhttpd simplify encodeString function --- src/nhttpd/yhttpd_core/helper.cpp | 37 +++++++++++++------------------ src/nhttpd/yhttpd_core/helper.h | 2 +- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/nhttpd/yhttpd_core/helper.cpp b/src/nhttpd/yhttpd_core/helper.cpp index fc1d7ba0a..e8bc5a235 100644 --- a/src/nhttpd/yhttpd_core/helper.cpp +++ b/src/nhttpd/yhttpd_core/helper.cpp @@ -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