[lld][WebAssembly] Perform data relocations during start function
[llvm-project.git] / lldb / source / Utility / UriParser.cpp
blobcfb9009898d2007b131829a262a3bf967365ce49
1 //===-- UriParser.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/UriParser.h"
10 #include "llvm/Support/raw_ostream.h"
12 #include <string>
14 #include <cstdint>
15 #include <tuple>
17 using namespace lldb_private;
19 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &OS,
20 const URI &U) {
21 OS << U.scheme << "://[" << U.hostname << ']';
22 if (U.port)
23 OS << ':' << U.port.getValue();
24 return OS << U.path;
27 llvm::Optional<URI> URI::Parse(llvm::StringRef uri) {
28 URI ret;
30 const llvm::StringRef kSchemeSep("://");
31 auto pos = uri.find(kSchemeSep);
32 if (pos == std::string::npos)
33 return llvm::None;
35 // Extract path.
36 ret.scheme = uri.substr(0, pos);
37 auto host_pos = pos + kSchemeSep.size();
38 auto path_pos = uri.find('/', host_pos);
39 if (path_pos != std::string::npos)
40 ret.path = uri.substr(path_pos);
41 else
42 ret.path = "/";
44 auto host_port = uri.substr(
45 host_pos,
46 ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos);
48 // Extract hostname
49 if (!host_port.empty() && host_port[0] == '[') {
50 // hostname is enclosed with square brackets.
51 pos = host_port.rfind(']');
52 if (pos == std::string::npos)
53 return llvm::None;
55 ret.hostname = host_port.substr(1, pos - 1);
56 host_port = host_port.drop_front(pos + 1);
57 if (!host_port.empty() && !host_port.consume_front(":"))
58 return llvm::None;
59 } else {
60 std::tie(ret.hostname, host_port) = host_port.split(':');
63 // Extract port
64 if (!host_port.empty()) {
65 uint16_t port_value = 0;
66 if (host_port.getAsInteger(0, port_value))
67 return llvm::None;
68 ret.port = port_value;
69 } else
70 ret.port = llvm::None;
72 return ret;