Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
escape_uri.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/escape_uri.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
12 
13 namespace tlx {
14 
16  std::string result;
17  result.reserve(src.size() + src.size() / 16);
18 
19  for (std::string::const_iterator it = src.begin(); it != src.end(); ++it)
20  {
21  switch (*it) {
22  // alnum
23  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
24  case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
25  case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
26  case 'V': case 'W': case 'X': case 'Y': case 'Z':
27  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
28  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
29  case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
30  case 'v': case 'w': case 'x': case 'y': case 'z':
31  case '0': case '1': case '2': case '3': case '4': case '5': case '6':
32  case '7': case '8': case '9':
33  // mark
34  case '-': case '_': case '.': case '~':
35  result.append(1, *it);
36  break;
37  // escape
38  default: {
39  char first = (*it & 0xF0) / 16;
40  first += first > 9 ? 'A' - 10 : '0';
41  char second = *it & 0x0F;
42  second += second > 9 ? 'A' - 10 : '0';
43 
44  result.append(1, '%');
45  result.append(1, first);
46  result.append(1, second);
47  break;
48  }
49  }
50  }
51 
52  return result;
53 }
54 
55 } // namespace tlx
56 
57 /******************************************************************************/
std::string escape_uri(const std::string &src)
Escape a string into a URI-encoding.
Definition: escape_uri.cpp:15
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220