]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/quickbook/src/utils.cpp
f2f155768ba29a614ff528409430b476775242ce
[ceph.git] / ceph / src / boost / tools / quickbook / src / utils.cpp
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
17 {
18 namespace detail
19 {
20 std::string encode_string(quickbook::string_view str)
21 {
22 std::string result;
23 result.reserve(str.size());
24
25 for (string_iterator it = str.begin(); it != str.end(); ++it) {
26 switch (*it) {
27 case '<':
28 result += "&lt;";
29 break;
30 case '>':
31 result += "&gt;";
32 break;
33 case '&':
34 result += "&amp;";
35 break;
36 case '"':
37 result += "&quot;";
38 break;
39 default:
40 result += *it;
41 break;
42 }
43 }
44
45 return result;
46 }
47
48 void print_char(char ch, std::ostream& out)
49 {
50 switch (ch) {
51 case '<':
52 out << "&lt;";
53 break;
54 case '>':
55 out << "&gt;";
56 break;
57 case '&':
58 out << "&amp;";
59 break;
60 case '"':
61 out << "&quot;";
62 break;
63 default:
64 out << ch;
65 break;
66 // note &apos; is not included. see the curse of apos:
67 // http://fishbowl.pastiche.org/2003/07/01/the_curse_of_apos
68 }
69 }
70
71 void print_string(quickbook::string_view str, std::ostream& out)
72 {
73 for (string_iterator cur = str.begin(); cur != str.end(); ++cur) {
74 print_char(*cur, out);
75 }
76 }
77
78 std::string make_identifier(quickbook::string_view text)
79 {
80 std::string id(text.begin(), text.end());
81 for (std::string::iterator i = id.begin(); i != id.end(); ++i) {
82 if (!std::isalnum(static_cast<unsigned char>(*i))) {
83 *i = '_';
84 }
85 else {
86 *i = static_cast<char>(
87 std::tolower(static_cast<unsigned char>(*i)));
88 }
89 }
90
91 return id;
92 }
93
94 static std::string escape_uri_impl(
95 quickbook::string_view uri_param, char const* mark)
96 {
97 // Extra capital characters for validating percent escapes.
98 static char const hex[] = "0123456789abcdefABCDEF";
99
100 std::string uri;
101 uri.reserve(uri_param.size());
102
103 for (std::string::size_type n = 0; n < uri_param.size(); ++n) {
104 if (static_cast<unsigned char>(uri_param[n]) > 127 ||
105 (!std::isalnum(static_cast<unsigned char>(uri_param[n])) &&
106 !std::strchr(mark, uri_param[n])) ||
107 (uri_param[n] == '%' &&
108 !(n + 2 < uri_param.size() &&
109 std::strchr(hex, uri_param[n + 1]) &&
110 std::strchr(hex, uri_param[n + 2])))) {
111 char escape[] = {'%', hex[uri_param[n] / 16],
112 hex[uri_param[n] % 16], '\0'};
113 uri += escape;
114 }
115 else {
116 uri += uri_param[n];
117 }
118 }
119
120 return uri;
121 }
122
123 std::string escape_uri(quickbook::string_view uri_param)
124 {
125 std::string uri(uri_param.begin(), uri_param.end());
126 return escape_uri_impl(uri_param, "-_.!~*'()?\\/");
127 }
128
129 std::string partially_escape_uri(quickbook::string_view uri_param)
130 {
131 return escape_uri_impl(uri_param, "-_.!~*'()?\\/:&=#%+");
132 }
133 }
134 }