include/boost/url/detail/path.hpp

100.0% Lines (60/60) 100.0% List of 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 13284x path_prefix(
25 char const* p,
26 std::size_t n) noexcept
27 {
28 13284x switch(n)
29 {
30 2904x case 0:
31 2904x return 0;
32
33 1389x case 1:
34 1389x if(p[0] == '/')
35 1168x return 1;
36 221x return 0;
37
38 543x case 2:
39 543x if(p[0] == '/')
40 368x return 1;
41 175x if( p[0] == '.' &&
42 88x p[1] == '/')
43 59x return 2;
44 116x return 0;
45
46 8448x default:
47 8448x if(p[0] == '/')
48 {
49 6488x if( p[1] == '.' &&
50 562x p[2] == '/')
51 354x return 3;
52 6134x return 1;
53 }
54 1960x if( p[0] == '.' &&
55 641x p[1] == '/')
56 351x return 2;
57 1609x break;
58 }
59 1609x return 0;
60 }
61
62 // VFALCO DEPRECATED
63 inline
64 std::size_t
65 13284x path_prefix(
66 core::string_view s) noexcept
67 {
68 13284x return path_prefix(
69 13284x 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 20024x path_segments(
77 core::string_view s,
78 std::size_t nseg) noexcept
79 {
80 20024x switch(s.size())
81 {
82 3386x case 0:
83 3386x BOOST_ASSERT(nseg == 0);
84 3386x return 0;
85
86 3160x case 1:
87 3160x BOOST_ASSERT(nseg == 1);
88 3160x if(s[0] == '/')
89 1394x return 0;
90 1766x return 1;
91
92 1571x case 2:
93 1571x if(s[0] == '/')
94 159x return nseg;
95 1465x if( s[0] == '.' &&
96 53x s[1] == '/')
97 {
98 14x BOOST_ASSERT(nseg > 1);
99 14x return nseg - 1;
100 }
101 1398x return nseg;
102
103 11907x default:
104 11907x if(s[0] == '/')
105 {
106 4937x if( s[1] == '.' &&
107 73x s[2] == '/')
108 {
109 51x BOOST_ASSERT(nseg > 1);
110 51x return nseg - 1;
111 }
112 4813x return nseg;
113 }
114 7204x if( s[0] == '.' &&
115 161x s[1] == '/')
116 {
117 52x BOOST_ASSERT(nseg > 1);
118 52x return nseg - 1;
119 }
120 6991x break;
121 }
122 6991x 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