]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2002 2004 2006 Joel de Guzman | |
3 | Copyright (c) 2004 Eric Niebler | |
4 | http://spirit.sourceforge.net/ | |
5 | ||
6 | Use, modification and distribution is subject to the Boost Software | |
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
8 | http://www.boost.org/LICENSE_1_0.txt) | |
9 | =============================================================================*/ | |
10 | #include "utils.hpp" | |
11 | ||
12 | #include <cctype> | |
13 | #include <cstring> | |
14 | #include <map> | |
15 | ||
16 | namespace quickbook { namespace detail | |
17 | { | |
b32b8144 | 18 | std::string encode_string(quickbook::string_view str) |
7c673cae FG |
19 | { |
20 | std::string result; | |
21 | result.reserve(str.size()); | |
22 | ||
b32b8144 | 23 | for (string_iterator it = str.begin(); |
7c673cae FG |
24 | it != str.end(); ++it) |
25 | { | |
26 | switch (*it) | |
27 | { | |
28 | case '<': result += "<"; break; | |
29 | case '>': result += ">"; break; | |
30 | case '&': result += "&"; break; | |
31 | case '"': result += """; break; | |
32 | default: result += *it; break; | |
33 | } | |
34 | } | |
35 | ||
36 | return result; | |
37 | } | |
38 | ||
39 | void print_char(char ch, std::ostream& out) | |
40 | { | |
41 | switch (ch) | |
42 | { | |
43 | case '<': out << "<"; break; | |
44 | case '>': out << ">"; break; | |
45 | case '&': out << "&"; break; | |
46 | case '"': out << """; break; | |
47 | default: out << ch; break; | |
48 | // note ' is not included. see the curse of apos: | |
49 | // http://fishbowl.pastiche.org/2003/07/01/the_curse_of_apos | |
50 | } | |
51 | } | |
52 | ||
b32b8144 | 53 | void print_string(quickbook::string_view str, std::ostream& out) |
7c673cae | 54 | { |
b32b8144 | 55 | for (string_iterator cur = str.begin(); |
7c673cae FG |
56 | cur != str.end(); ++cur) |
57 | { | |
58 | print_char(*cur, out); | |
59 | } | |
60 | } | |
61 | ||
b32b8144 | 62 | std::string make_identifier(quickbook::string_view text) |
7c673cae | 63 | { |
b32b8144 FG |
64 | std::string id(text.begin(), text.end()); |
65 | for (std::string::iterator i = id.begin(); i != id.end(); ++i) { | |
66 | if (!std::isalnum(static_cast<unsigned char>(*i))) { | |
67 | *i = '_'; | |
68 | } | |
69 | else { | |
70 | *i = static_cast<char>(std::tolower(static_cast<unsigned char>(*i))); | |
71 | } | |
72 | } | |
73 | ||
74 | return id; | |
7c673cae FG |
75 | } |
76 | ||
b32b8144 | 77 | static std::string escape_uri_impl(quickbook::string_view uri_param, char const* mark) |
7c673cae FG |
78 | { |
79 | // Extra capital characters for validating percent escapes. | |
80 | static char const hex[] = "0123456789abcdefABCDEF"; | |
81 | ||
82 | std::string uri; | |
b32b8144 | 83 | uri.reserve(uri_param.size()); |
7c673cae | 84 | |
b32b8144 | 85 | for (std::string::size_type n = 0; n < uri_param.size(); ++n) |
7c673cae | 86 | { |
b32b8144 FG |
87 | if (static_cast<unsigned char>(uri_param[n]) > 127 || |
88 | (!std::isalnum(static_cast<unsigned char>(uri_param[n])) && | |
89 | !std::strchr(mark, uri_param[n])) || | |
90 | (uri_param[n] == '%' && !(n + 2 < uri_param.size() && | |
91 | std::strchr(hex, uri_param[n+1]) && | |
92 | std::strchr(hex, uri_param[n+2])))) | |
7c673cae | 93 | { |
b32b8144 FG |
94 | char escape[] = { '%', hex[uri_param[n] / 16], hex[uri_param[n] % 16], '\0' }; |
95 | uri += escape; | |
7c673cae | 96 | } |
b32b8144 | 97 | else |
7c673cae | 98 | { |
b32b8144 | 99 | uri += uri_param[n]; |
7c673cae FG |
100 | } |
101 | } | |
102 | ||
103 | return uri; | |
104 | } | |
105 | ||
b32b8144 | 106 | std::string escape_uri(quickbook::string_view uri_param) |
7c673cae | 107 | { |
b32b8144 | 108 | std::string uri(uri_param.begin(), uri_param.end()); |
7c673cae FG |
109 | return escape_uri_impl(uri_param, "-_.!~*'()?\\/"); |
110 | } | |
111 | ||
b32b8144 | 112 | std::string partially_escape_uri(quickbook::string_view uri_param) |
7c673cae FG |
113 | { |
114 | return escape_uri_impl(uri_param, "-_.!~*'()?\\/:&=#%+"); | |
115 | } | |
116 | }} |