include/boost/url/impl/scheme.hpp

100.0% Lines (75/75) 100.0% Functions (3/3)
include/boost/url/impl/scheme.hpp
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/boostorg/url
9 //
10
11 #ifndef BOOST_URL_IMPL_SCHEME_HPP
12 #define BOOST_URL_IMPL_SCHEME_HPP
13
14 #include <boost/url/detail/config.hpp>
15
16 #include <boost/url/grammar/ci_string.hpp>
17
18 namespace boost {
19 namespace urls {
20
21 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
22 scheme
23 5321 string_to_scheme(
24 core::string_view s) noexcept
25 {
26 using grammar::to_lower;
27 5321 switch(s.size())
28 {
29 2 case 0: // none
30 2 return scheme::none;
31
32 312 case 2: // ws
33 453 if( to_lower(s[0]) == 'w' &&
34 141 to_lower(s[1]) == 's')
35 139 return scheme::ws;
36 173 break;
37
38 180 case 3:
39 180 switch(to_lower(s[0]))
40 {
41 51 case 'w': // wss
42 96 if( to_lower(s[1]) == 's' &&
43 45 to_lower(s[2]) == 's')
44 43 return scheme::wss;
45 8 break;
46
47 78 case 'f': // ftp
48 115 if( to_lower(s[1]) == 't' &&
49 37 to_lower(s[2]) == 'p')
50 36 return scheme::ftp;
51 42 break;
52
53 51 default:
54 51 break;
55 }
56 101 break;
57
58 2365 case 4:
59 2365 switch(to_lower(s[0]))
60 {
61 361 case 'f': // file
62 710 if( to_lower(s[1]) == 'i' &&
63 710 to_lower(s[2]) == 'l' &&
64 348 to_lower(s[3]) == 'e')
65 347 return scheme::file;
66 14 break;
67
68 1854 case 'h': // http
69 3706 if( to_lower(s[1]) == 't' &&
70 3706 to_lower(s[2]) == 't' &&
71 1851 to_lower(s[3]) == 'p')
72 1851 return scheme::http;
73 3 break;
74
75 150 default:
76 150 break;
77 }
78 167 break;
79
80 1465 case 5: // https
81 2023 if( to_lower(s[0]) == 'h' &&
82 1115 to_lower(s[1]) == 't' &&
83 1113 to_lower(s[2]) == 't' &&
84 2579 to_lower(s[3]) == 'p' &&
85 555 to_lower(s[4]) == 's')
86 548 return scheme::https;
87 917 break;
88
89 997 default:
90 997 break;
91 }
92 2355 return scheme::unknown;
93 }
94
95 inline
96 core::string_view
97 53 to_string(scheme s) noexcept
98 {
99 53 switch(s)
100 {
101 4 case scheme::ftp: return "ftp";
102 3 case scheme::file: return "file";
103 3 case scheme::http: return "http";
104 5 case scheme::https: return "https";
105 11 case scheme::ws: return "ws";
106 5 case scheme::wss: return "wss";
107 1 case scheme::none: return {};
108 21 default:
109 21 break;
110 }
111 21 return "<unknown>";
112 }
113
114 inline
115 std::uint16_t
116 8 default_port(scheme s) noexcept
117 {
118 8 switch(s)
119 {
120 1 case scheme::ftp:
121 1 return 21;
122 2 case scheme::http:
123 case scheme::ws:
124 2 return 80;
125 2 case scheme::https:
126 case scheme::wss:
127 2 return 443;
128 3 default:
129 3 break;
130 }
131 3 return 0;
132 }
133
134 } // urls
135 } // boost
136
137 #endif
138