]>
Commit | Line | Data |
---|---|---|
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 | { | |
18 | std::string encode_string(quickbook::string_view str) | |
19 | { | |
20 | std::string result; | |
21 | result.reserve(str.size()); | |
22 | ||
23 | for (string_iterator it = str.begin(); | |
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 | ||
53 | void print_string(quickbook::string_view str, std::ostream& out) | |
54 | { | |
55 | for (string_iterator cur = str.begin(); | |
56 | cur != str.end(); ++cur) | |
57 | { | |
58 | print_char(*cur, out); | |
59 | } | |
60 | } | |
61 | ||
62 | std::string make_identifier(quickbook::string_view text) | |
63 | { | |
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; | |
75 | } | |
76 | ||
77 | static std::string escape_uri_impl(quickbook::string_view uri_param, char const* mark) | |
78 | { | |
79 | // Extra capital characters for validating percent escapes. | |
80 | static char const hex[] = "0123456789abcdefABCDEF"; | |
81 | ||
82 | std::string uri; | |
83 | uri.reserve(uri_param.size()); | |
84 | ||
85 | for (std::string::size_type n = 0; n < uri_param.size(); ++n) | |
86 | { | |
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])))) | |
93 | { | |
94 | char escape[] = { '%', hex[uri_param[n] / 16], hex[uri_param[n] % 16], '\0' }; | |
95 | uri += escape; | |
96 | } | |
97 | else | |
98 | { | |
99 | uri += uri_param[n]; | |
100 | } | |
101 | } | |
102 | ||
103 | return uri; | |
104 | } | |
105 | ||
106 | std::string escape_uri(quickbook::string_view uri_param) | |
107 | { | |
108 | std::string uri(uri_param.begin(), uri_param.end()); | |
109 | return escape_uri_impl(uri_param, "-_.!~*'()?\\/"); | |
110 | } | |
111 | ||
112 | std::string partially_escape_uri(quickbook::string_view uri_param) | |
113 | { | |
114 | return escape_uri_impl(uri_param, "-_.!~*'()?\\/:&=#%+"); | |
115 | } | |
116 | }} |