Audacity 3.2.0
UriParser.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: UriParser.cpp
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#include "UriParser.h"
8
9UriFields ParseUri(std::string_view uri) noexcept
10{
11 UriFields result;
12 auto schemeEnd = uri.find("://");
13 if (schemeEnd != std::string_view::npos)
14 {
15 result.Scheme = uri.substr(0, schemeEnd);
16 uri.remove_prefix(schemeEnd + 3);
17 }
18
19 auto fragmentStart = uri.find('#');
20 if (fragmentStart != std::string_view::npos)
21 {
22 result.Fragment = uri.substr(fragmentStart + 1);
23 uri.remove_suffix(uri.size() - fragmentStart);
24 }
25
26 auto queryStart = uri.find('?');
27 if (queryStart != std::string_view::npos)
28 {
29 result.Query = uri.substr(queryStart + 1);
30 uri.remove_suffix(uri.size() - queryStart);
31 }
32
33 auto pathStart = uri.find('/');
34 if (pathStart != std::string_view::npos)
35 {
36 result.Path = uri.substr(pathStart + 1);
37 uri.remove_suffix(uri.size() - pathStart);
38 }
39
40 auto userInfoEnd = uri.find('@');
41 if (userInfoEnd != std::string_view::npos)
42 {
43 result.UserInfo = uri.substr(0, userInfoEnd);
44 uri.remove_prefix(userInfoEnd + 1);
45 }
46
47 auto portStart = uri.find(':');
48 if (portStart != std::string_view::npos)
49 {
50 result.Port = uri.substr(portStart + 1);
51 uri.remove_suffix(uri.size() - portStart);
52 }
53
54 result.Host = uri;
55
56 return result;
57}
58
59QueryFields ParseUriQuery(std::string_view query, std::string_view delimiter) noexcept
60{
61 QueryFields result;
62
63 while (!query.empty())
64 {
65 const auto queryItemEnd = query.find(delimiter);
66 const auto queryItem = query.substr(0, queryItemEnd);
67
68 query.remove_prefix(
69 queryItemEnd != std::string_view::npos ? queryItemEnd + 1 :
70 query.size());
71
72 const auto queryItemValueStart = queryItem.find('=');
73
74 const auto queryItemValue = queryItem.substr(
75 queryItemValueStart != std::string_view::npos ?
76 queryItemValueStart + 1 :
77 queryItem.size());
78
79 const auto queryItemKey = queryItem.substr(
80 0, queryItemValueStart != std::string_view::npos ?
81 queryItemValueStart :
82 queryItem.size());
83
84 result.emplace(queryItemKey, queryItemValue);
85 }
86
87 return result;
88}
QueryFields ParseUriQuery(std::string_view query, std::string_view delimiter) noexcept
Parses URI query and returns QueryFields structure with parsed fields.
Definition: UriParser.cpp:59
UriFields ParseUri(std::string_view uri) noexcept
Definition: UriParser.cpp:9
std::unordered_map< std::string_view, std::string_view > QueryFields
Definition: UriParser.h:27
constexpr size_t npos(-1)
std::string_view UserInfo
Definition: UriParser.h:14
std::string_view Port
Definition: UriParser.h:16
std::string_view Fragment
Definition: UriParser.h:19
std::string_view Host
Definition: UriParser.h:15
std::string_view Query
Definition: UriParser.h:18
std::string_view Scheme
Definition: UriParser.h:13
std::string_view Path
Definition: UriParser.h:17