Line data Source code
1 : //
2 : // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/boostorg/url
8 : //
9 :
10 : #ifndef BOOST_URL_DETAIL_PATH_HPP
11 : #define BOOST_URL_DETAIL_PATH_HPP
12 :
13 : #include <boost/core/detail/string_view.hpp>
14 :
15 : namespace boost {
16 : namespace urls {
17 : namespace detail {
18 :
19 : // Return the number of characters at
20 : // the front of the path that are reserved
21 : inline
22 : std::size_t
23 4367 : path_prefix(
24 : char const* p,
25 : std::size_t n) noexcept
26 : {
27 4367 : switch(n)
28 : {
29 311 : case 0:
30 311 : return 0;
31 :
32 240 : case 1:
33 240 : if(p[0] == '/')
34 155 : return 1;
35 85 : return 0;
36 :
37 259 : case 2:
38 259 : if(p[0] == '/')
39 158 : return 1;
40 101 : if( p[0] == '.' &&
41 86 : p[1] == '/')
42 57 : return 2;
43 44 : return 0;
44 :
45 3557 : default:
46 3557 : if(p[0] == '/')
47 : {
48 1905 : if( p[1] == '.' &&
49 433 : p[2] == '/')
50 234 : return 3;
51 1671 : return 1;
52 : }
53 1652 : if( p[0] == '.' &&
54 638 : p[1] == '/')
55 351 : return 2;
56 1301 : break;
57 : }
58 1301 : return 0;
59 : }
60 :
61 : // VFALCO DEPRECATED
62 : inline
63 : std::size_t
64 4367 : path_prefix(
65 : core::string_view s) noexcept
66 : {
67 4367 : return path_prefix(
68 4367 : s.data(), s.size());
69 : }
70 :
71 : // returns the number of adjusted
72 : // segments based on the malleable prefix.
73 : inline
74 : std::size_t
75 158 : path_segments(
76 : core::string_view s,
77 : std::size_t nseg) noexcept
78 : {
79 158 : switch(s.size())
80 : {
81 3 : case 0:
82 3 : BOOST_ASSERT(nseg == 0);
83 3 : return 0;
84 :
85 11 : case 1:
86 11 : BOOST_ASSERT(nseg == 1);
87 11 : if(s[0] == '/')
88 7 : return 0;
89 4 : return 1;
90 :
91 6 : case 2:
92 6 : if(s[0] == '/')
93 1 : return nseg;
94 10 : if( s[0] == '.' &&
95 5 : s[1] == '/')
96 : {
97 5 : BOOST_ASSERT(nseg > 1);
98 5 : return nseg - 1;
99 : }
100 0 : return nseg;
101 :
102 138 : default:
103 138 : if(s[0] == '/')
104 : {
105 35 : if( s[1] == '.' &&
106 1 : s[2] == '/')
107 : {
108 1 : BOOST_ASSERT(nseg > 1);
109 1 : return nseg - 1;
110 : }
111 33 : return nseg;
112 : }
113 107 : if( s[0] == '.' &&
114 3 : s[1] == '/')
115 : {
116 2 : BOOST_ASSERT(nseg > 1);
117 2 : return nseg - 1;
118 : }
119 102 : break;
120 : }
121 102 : return nseg;
122 : }
123 :
124 : // Trim reserved characters from
125 : // the front of the path.
126 : inline
127 : core::string_view
128 : clean_path(
129 : core::string_view s) noexcept
130 : {
131 : s.remove_prefix(
132 : path_prefix(s));
133 : return s;
134 : }
135 :
136 : } // detail
137 : } // urls
138 : } // boost
139 :
140 : #endif
|