csexton (owner)

Revisions

gist: 226657 Download_button fork
public
Public Clone URL: git://gist.github.com/226657.git
Embed All Files: show embed
C++ #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string UriEscape(const string& input)
{
  int i = 0;
  string ret;
  char buffer [16]; //TODO: Should this buffer be 16?
                    // I only one character into hex at a time, so it should
                    // never go over 4 chars (percent sign, two hex characters and the null).
  unsigned char c;
 
  for (i = 0; (c = input[i]) != '\0'; i++) {
    if ((c == 0x2D) || (c == 0x2E) || // Hyphen-Minus, Full Stop
        ((0x30 <= c) && (c <= 0x39)) || // Digits [0-9]
        ((0x41 <= c) && (c <= 0x5A)) || // Uppercase [A-Z]
        ((0x61 <= c) && (c <= 0x7A))) { // Lowercase [a-z]
        ret += c;
    } else {
      sprintf (buffer, "%%%0.2X", c);
      ret += buffer;
    }
  }
  return ret;
}