Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converting C++ into C for memory efficiency (with a side effect of increased performance too!)
//OLD C++ STYLE
String ESP8266WebServer::urlDecode(const String& text)
{
String decoded = "";
char temp[] = "0x00";
unsigned int len = text.length();
unsigned int i = 0;
while (i < len)
{
char decodedChar;
char encodedChar = text.charAt(i++);
if ((encodedChar == '%') && (i + 1 < len))
{
temp[2] = text.charAt(i++);
temp[3] = text.charAt(i++);
decodedChar = strtol(temp, NULL, 16);
}
else {
if (encodedChar == '+')
{
decodedChar = ' ';
}
else {
decodedChar = encodedChar; // normal ascii char
}
}
decoded += decodedChar;
}
return decoded;
}
//NEW C STYLE
char *ESP8266WebServer::urlDecode(char *text, int len) {
if (!text) return text;
char *src = text;
char *dst = text;
char temp[] = "00";
while (*src) {
if (len-- < 1) break;
if (*src == '%') {
if (!src[1] || !src[2]) break;
temp[0] = src[1];
temp[1] = src[2];
len -= 2;
src += 2;
*dst = (char) strtol(temp, nullptr, 16);
} else if (*src == '+') {
*dst = ' ';
} else {
*dst = *src;
}
src++;
dst++;
}
*dst = NULL;
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.