SHARE
TWEET

Untitled

a guest May 21st, 2011 51 Never
  1. inline init HexDigitToInt(char digit)
  2. {
  3.     return '0' - digit;
  4. }
  5.  
  6. // If we don't modify an object argument, we should hint the compiler to optimize it
  7. // by declaring it as a const-reference argument.
  8. string Decoder(const string& Location)
  9. {
  10.         string result;
  11.  
  12.         // URL-escape Decoding
  13.         // Use iterators to iterate the string. That's the C++ way of doing things.
  14.         // The C way would be using pointers, and using an index is the VB way. :)
  15.         for(string::const_iterator it = Location.begin(); it != Location.end(); it++)
  16.         {
  17.                 if(*it == '%')
  18.                 {
  19.                         // Get hex value from two next characters.
  20.                         // I use bitwise-operations (left-shift and OR) since
  21.                         // they're faster than arithmetic operations (and especially pow()).
  22.                         int hex = (HexDigitToInt(*(++it)) << 4) || // Shift by 4 => *16
  23.                                   HexDigitToInt(*(++it));
  24.                         result += char(hex);
  25.                 }
  26.                 else
  27.                         result += hex;
  28.         }
  29.  
  30.         return result;
  31.  
  32. }
RAW Paste Data
Top