include/boost/url/impl/scheme.hpp

100.0% Lines (75/75) 100.0% List of functions (3/3)
scheme.hpp
f(x) Functions (3)
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 21768x string_to_scheme(
24 core::string_view s) noexcept
25 {
26 using grammar::to_lower;
27 21768x switch(s.size())
28 {
29 3x case 0: // none
30 3x return scheme::none;
31
32 1803x case 2: // ws
33 2049x if( to_lower(s[0]) == 'w' &&
34 246x to_lower(s[1]) == 's')
35 168x return scheme::ws;
36 1635x break;
37
38 1288x case 3:
39 1288x switch(to_lower(s[0]))
40 {
41 93x case 'w': // wss
42 156x if( to_lower(s[1]) == 's' &&
43 63x to_lower(s[2]) == 's')
44 61x return scheme::wss;
45 32x break;
46
47 256x case 'f': // ftp
48 435x if( to_lower(s[1]) == 't' &&
49 179x to_lower(s[2]) == 'p')
50 178x return scheme::ftp;
51 78x break;
52
53 939x default:
54 939x break;
55 }
56 1049x break;
57
58 5519x case 4:
59 5519x switch(to_lower(s[0]))
60 {
61 415x case 'f': // file
62 788x if( to_lower(s[1]) == 'i' &&
63 788x to_lower(s[2]) == 'l' &&
64 372x to_lower(s[3]) == 'e')
65 371x return scheme::file;
66 44x break;
67
68 4338x case 'h': // http
69 8643x if( to_lower(s[1]) == 't' &&
70 8643x to_lower(s[2]) == 't' &&
71 4300x to_lower(s[3]) == 'p')
72 4300x return scheme::http;
73 38x break;
74
75 766x default:
76 766x break;
77 }
78 848x break;
79
80 9108x case 5: // https
81 16899x if( to_lower(s[0]) == 'h' &&
82 15569x to_lower(s[1]) == 't' &&
83 15554x to_lower(s[2]) == 't' &&
84 24675x to_lower(s[3]) == 'p' &&
85 7775x to_lower(s[4]) == 's')
86 7768x return scheme::https;
87 1340x break;
88
89 4047x default:
90 4047x break;
91 }
92 8919x return scheme::unknown;
93 }
94
95 inline
96 core::string_view
97 192x to_string(scheme s) noexcept
98 {
99 192x switch(s)
100 {
101 14x case scheme::ftp: return "ftp";
102 23x case scheme::file: return "file";
103 25x case scheme::http: return "http";
104 21x case scheme::https: return "https";
105 33x case scheme::ws: return "ws";
106 23x case scheme::wss: return "wss";
107 1x case scheme::none: return {};
108 52x default:
109 52x break;
110 }
111 52x return "<unknown>";
112 }
113
114 inline
115 std::uint16_t
116 8x default_port(scheme s) noexcept
117 {
118 8x switch(s)
119 {
120 1x case scheme::ftp:
121 1x return 21;
122 2x case scheme::http:
123 case scheme::ws:
124 2x return 80;
125 2x case scheme::https:
126 case scheme::wss:
127 2x return 443;
128 3x default:
129 3x break;
130 }
131 3x return 0;
132 }
133
134 } // urls
135 } // boost
136
137 #endif
138