kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 1 | /* Copyright 2011, Range Networks, Inc. */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 2 | |
| 3 | #include <URLEncode.h> |
| 4 | #include <string> |
| 5 | #include <string.h> |
| 6 | #include <ctype.h> |
| 7 | |
| 8 | using namespace std; |
| 9 | |
| 10 | //based on javascript encodeURIComponent() |
| 11 | string URLEncode(const string &c) |
| 12 | { |
| 13 | static const char *digits = "01234567890ABCDEF"; |
| 14 | string retVal=""; |
kurtis.heimerl | 1a2d244 | 2012-12-22 04:30:56 +0000 | [diff] [blame] | 15 | for (size_t i=0; i<c.length(); i++) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 16 | { |
| 17 | const char ch = c[i]; |
| 18 | if (isalnum(ch) || strchr("-_.!~'()",ch)) { |
| 19 | retVal += ch; |
| 20 | } else { |
| 21 | retVal += '%'; |
| 22 | retVal += digits[(ch>>4) & 0x0f]; |
| 23 | retVal += digits[ch & 0x0f]; |
| 24 | } |
| 25 | } |
| 26 | return retVal; |
| 27 | } |
| 28 | |