include/boost/url/detail/path.hpp

100.0% Lines (60 / 60) 100.0% Functions (3 / 3)
path.hpp
f(x) Functions (3)
Line TLA Hits 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/url/detail/config.hpp>
14 #include <boost/core/detail/string_view.hpp>
15
16 namespace boost {
17 namespace urls {
18 namespace detail {
19
20 // Return the number of characters at
21 // the front of the path that are reserved
22 inline
23 std::size_t
24 16977x path_prefix(
25 char const* p,
26 std::size_t n) noexcept
27 {
28 16977x switch(n)
29 {
30 3041x case 0:
31 3041x return 0;
32
33 1606x case 1:
34 1606x if(p[0] == '/')
35 1253x return 1;
36 353x return 0;
37
38 922x case 2:
39 922x if(p[0] == '/')
40 547x return 1;
41 375x if( p[0] == '.' &&
42 212x p[1] == '/')
43 77x return 2;
44 298x return 0;
45
46 11408x default:
47 11408x if(p[0] == '/')
48 {
49 7856x if( p[1] == '.' &&
50 984x p[2] == '/')
51 525x return 3;
52 7331x return 1;
53 }
54 3552x if( p[0] == '.' &&
55 1280x p[1] == '/')
56 728x return 2;
57 2824x break;
58 }
59 2824x return 0;
60 }
61
62 // VFALCO DEPRECATED
63 inline
64 std::size_t
65 16977x path_prefix(
66 core::string_view s) noexcept
67 {
68 16977x return path_prefix(
69 16977x s.data(), s.size());
70 }
71
72 // returns the number of adjusted
73 // segments based on the malleable prefix.
74 BOOST_URL_CXX14_CONSTEXPR_OR_INLINE
75 std::size_t
76 21363x path_segments(
77 core::string_view s,
78 std::size_t nseg) noexcept
79 {
80 21363x switch(s.size())
81 {
82 3525x case 0:
83 3525x BOOST_ASSERT(nseg == 0);
84 3525x return 0;
85
86 3356x case 1:
87 3356x BOOST_ASSERT(nseg == 1);
88 3356x if(s[0] == '/')
89 1570x return 0;
90 1786x return 1;
91
92 1664x case 2:
93 1664x if(s[0] == '/')
94 237x return nseg;
95 1490x if( s[0] == '.' &&
96 63x s[1] == '/')
97 {
98 15x BOOST_ASSERT(nseg > 1);
99 15x return nseg - 1;
100 }
101 1412x return nseg;
102
103 12818x default:
104 12818x if(s[0] == '/')
105 {
106 5739x if( s[1] == '.' &&
107 186x s[2] == '/')
108 {
109 98x BOOST_ASSERT(nseg > 1);
110 98x return nseg - 1;
111 }
112 5455x return nseg;
113 }
114 7499x if( s[0] == '.' &&
115 234x s[1] == '/')
116 {
117 110x BOOST_ASSERT(nseg > 1);
118 110x return nseg - 1;
119 }
120 7155x break;
121 }
122 7155x return nseg;
123 }
124
125 // Trim reserved characters from
126 // the front of the path.
127 inline
128 core::string_view
129 clean_path(
130 core::string_view s) noexcept
131 {
132 s.remove_prefix(
133 path_prefix(s));
134 return s;
135 }
136
137 } // detail
138 } // urls
139 } // boost
140
141 #endif
142