tlx
Loading...
Searching...
No Matches
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 <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
12
13#include <cstring>
14
15namespace tlx {
16
17std::string escape_uri(const std::string& str) {
18 std::string result;
19 result.reserve(str.size() + str.size() / 16);
20
21 for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
22 {
23 switch (*it) {
24 // alnum
25 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
26 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
27 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
28 case 'V': case 'W': case 'X': case 'Y': case 'Z':
29 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
30 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
31 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
32 case 'v': case 'w': case 'x': case 'y': case 'z':
33 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
34 case '7': case '8': case '9':
35 // mark
36 case '-': case '_': case '.': case '~':
37 result.append(1, *it);
38 break;
39 // escape
40 default: {
41 char first = (*it & 0xF0) / 16;
42 first += first > 9 ? 'A' - 10 : '0';
43 char second = *it & 0x0F;
44 second += second > 9 ? 'A' - 10 : '0';
45
46 result.append(1, '%');
47 result.append(1, first);
48 result.append(1, second);
49 break;
50 }
51 }
52 }
53
54 return result;
55}
56
57std::string escape_uri(const char* str) {
58 size_t slen = strlen(str);
59 std::string result;
60 result.reserve(slen + slen / 16);
61
62 for (const char* it = str; *it != 0; ++it)
63 {
64 switch (*it) {
65 // alnum
66 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
67 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
68 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
69 case 'V': case 'W': case 'X': case 'Y': case 'Z':
70 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
71 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
72 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
73 case 'v': case 'w': case 'x': case 'y': case 'z':
74 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
75 case '7': case '8': case '9':
76 // mark
77 case '-': case '_': case '.': case '~':
78 result.append(1, *it);
79 break;
80 // escape
81 default: {
82 char first = (*it & 0xF0) / 16;
83 first += first > 9 ? 'A' - 10 : '0';
84 char second = *it & 0x0F;
85 second += second > 9 ? 'A' - 10 : '0';
86
87 result.append(1, '%');
88 result.append(1, first);
89 result.append(1, second);
90 break;
91 }
92 }
93 }
94
95 return result;
96}
97
98} // namespace tlx
99
100/******************************************************************************/
std::string escape_uri(const std::string &str)
Escape a string into a URI-encoding.
Definition: escape_uri.cpp:17