]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Utility/UriParser.cpp
MFV r314910: 7843 get_clones_stat() is suboptimal for lots of clones
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Utility / UriParser.cpp
1 //===-- UriParser.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Utility/UriParser.h"
11
12 // C Includes
13
14 // C++ Includes
15 #include <cstring>
16
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Host/StringConvert.h"
20
21 using namespace lldb_private;
22
23 //----------------------------------------------------------------------
24 // UriParser::Parse
25 //----------------------------------------------------------------------
26 bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
27                       llvm::StringRef &hostname, int &port,
28                       llvm::StringRef &path) {
29   llvm::StringRef tmp_scheme, tmp_hostname, tmp_port, tmp_path;
30
31   const llvm::StringRef kSchemeSep("://");
32   auto pos = uri.find(kSchemeSep);
33   if (pos == std::string::npos)
34     return false;
35
36   // Extract path.
37   tmp_scheme = uri.substr(0, pos);
38   auto host_pos = pos + kSchemeSep.size();
39   auto path_pos = uri.find('/', host_pos);
40   if (path_pos != std::string::npos)
41     tmp_path = uri.substr(path_pos);
42   else
43     tmp_path = "/";
44
45   auto host_port = uri.substr(
46       host_pos,
47       ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos);
48
49   // Extract hostname
50   if (host_port[0] == '[') {
51     // hostname is enclosed with square brackets.
52     pos = host_port.find(']');
53     if (pos == std::string::npos)
54       return false;
55
56     tmp_hostname = host_port.substr(1, pos - 1);
57     host_port = host_port.drop_front(pos + 1);
58     if (!host_port.empty() && !host_port.consume_front(":"))
59       return false;
60   } else {
61     std::tie(tmp_hostname, host_port) = host_port.split(':');
62   }
63
64   // Extract port
65   if (!host_port.empty()) {
66     uint16_t port_value = 0;
67     if (host_port.getAsInteger(0, port_value))
68       return false;
69     port = port_value;
70   } else
71     port = -1;
72
73   scheme = tmp_scheme;
74   hostname = tmp_hostname;
75   path = tmp_path;
76   return true;
77 }