Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:26

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/url
0008 //
0009 
0010 #ifndef BOOST_URL_DETAIL_PATH_HPP
0011 #define BOOST_URL_DETAIL_PATH_HPP
0012 
0013 #include <boost/core/detail/string_view.hpp>
0014 
0015 namespace boost {
0016 namespace urls {
0017 namespace detail {
0018 
0019 // Return the number of characters at
0020 // the front of the path that are reserved
0021 inline
0022 std::size_t
0023 path_prefix(
0024     char const* p,
0025     std::size_t n) noexcept
0026 {
0027     switch(n)
0028     {
0029     case 0:
0030         return 0;
0031 
0032     case 1:
0033         if(p[0] == '/')
0034             return 1;
0035         return 0;
0036 
0037     case 2:
0038         if(p[0] == '/')
0039             return 1;
0040         if( p[0] == '.' &&
0041             p[1] == '/')
0042             return 2;
0043         return 0;
0044 
0045     default:
0046         if(p[0] == '/')
0047         {
0048             if( p[1] == '.' &&
0049                 p[2] == '/')
0050                 return 3;
0051             return 1;
0052         }
0053         if( p[0] == '.' &&
0054             p[1] == '/')
0055             return 2;
0056         break;
0057     }
0058     return 0;
0059 }
0060 
0061 // VFALCO DEPRECATED
0062 inline
0063 std::size_t
0064 path_prefix(
0065     core::string_view s) noexcept
0066 {
0067     return path_prefix(
0068         s.data(), s.size());
0069 }
0070 
0071 // returns the number of adjusted
0072 // segments based on the malleable prefix.
0073 inline
0074 std::size_t
0075 path_segments(
0076     core::string_view s,
0077     std::size_t nseg) noexcept
0078 {
0079     switch(s.size())
0080     {
0081     case 0:
0082         BOOST_ASSERT(nseg == 0);
0083         return 0;
0084 
0085     case 1:
0086         BOOST_ASSERT(nseg == 1);
0087         if(s[0] == '/')
0088             return 0;
0089         return 1;
0090 
0091     case 2:
0092         if(s[0] == '/')
0093             return nseg;
0094         if( s[0] == '.' &&
0095             s[1] == '/')
0096         {
0097             BOOST_ASSERT(nseg > 1);
0098             return nseg - 1;
0099         }
0100         return nseg;
0101 
0102     default:
0103         if(s[0] == '/')
0104         {
0105             if( s[1] == '.' &&
0106                 s[2] == '/')
0107             {
0108                 BOOST_ASSERT(nseg > 1);
0109                 return nseg - 1;
0110             }
0111             return nseg;
0112         }
0113         if( s[0] == '.' &&
0114             s[1] == '/')
0115         {
0116             BOOST_ASSERT(nseg > 1);
0117             return nseg - 1;
0118         }
0119         break;
0120     }
0121     return nseg;
0122 }
0123 
0124 // Trim reserved characters from
0125 // the front of the path.
0126 inline
0127 core::string_view
0128 clean_path(
0129     core::string_view s) noexcept
0130 {
0131     s.remove_prefix(
0132         path_prefix(s));
0133     return s;
0134 }
0135 
0136 } // detail
0137 } // urls
0138 } // boost
0139 
0140 #endif