Created
June 18, 2018 04:25
-
-
Save darkain/23da34f00ebf3f1572322df3605be51f to your computer and use it in GitHub Desktop.
Converting C++ into C for memory efficiency (with a side effect of increased performance too!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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