]>
git.proxmox.com Git - ceph.git/blob - 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/
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 =============================================================================*/
16 namespace quickbook
{ namespace detail
18 std::string
encode_string(quickbook::string_view str
)
21 result
.reserve(str
.size());
23 for (string_iterator it
= str
.begin();
24 it
!= str
.end(); ++it
)
28 case '<': result
+= "<"; break;
29 case '>': result
+= ">"; break;
30 case '&': result
+= "&"; break;
31 case '"': result
+= """; break;
32 default: result
+= *it
; break;
39 void print_char(char ch
, std::ostream
& out
)
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
53 void print_string(quickbook::string_view str
, std::ostream
& out
)
55 for (string_iterator cur
= str
.begin();
56 cur
!= str
.end(); ++cur
)
58 print_char(*cur
, out
);
62 std::string
make_identifier(quickbook::string_view text
)
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
))) {
70 *i
= static_cast<char>(std::tolower(static_cast<unsigned char>(*i
)));
77 static std::string
escape_uri_impl(quickbook::string_view uri_param
, char const* mark
)
79 // Extra capital characters for validating percent escapes.
80 static char const hex
[] = "0123456789abcdefABCDEF";
83 uri
.reserve(uri_param
.size());
85 for (std::string::size_type n
= 0; n
< uri_param
.size(); ++n
)
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]))))
94 char escape
[] = { '%', hex
[uri_param
[n
] / 16], hex
[uri_param
[n
] % 16], '\0' };
106 std::string
escape_uri(quickbook::string_view uri_param
)
108 std::string
uri(uri_param
.begin(), uri_param
.end());
109 return escape_uri_impl(uri_param
, "-_.!~*'()?\\/");
112 std::string
partially_escape_uri(quickbook::string_view uri_param
)
114 return escape_uri_impl(uri_param
, "-_.!~*'()?\\/:&=#%+");