Convert UriParser to use StringRef.

llvm-svn: 287190
This commit is contained in:
Zachary Turner 2016-11-17 01:38:02 +00:00
parent 24bd317871
commit 245f7fdcfa
13 changed files with 77 additions and 56 deletions

View File

@ -73,6 +73,7 @@ public:
//------------------------------------------------------------------
static llvm::StringRef
DecodeUUIDBytesFromString(llvm::StringRef str, ValueType &uuid_bytes,
uint32_t &bytes_decoded,
uint32_t num_uuid_bytes = 16);
protected:

View File

@ -128,6 +128,7 @@ static inline int xdigit_to_int(char ch) {
llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
ValueType &uuid_bytes,
uint32_t &bytes_decoded,
uint32_t num_uuid_bytes) {
::memset(uuid_bytes, 0, sizeof(uuid_bytes));
size_t uuid_byte_idx = 0;
@ -157,6 +158,7 @@ llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
// Clear trailing bytes to 0.
for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
uuid_bytes[i] = 0;
bytes_decoded = uuid_byte_idx;
return p;
}
size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) {
@ -169,9 +171,9 @@ size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) {
// Skip leading whitespace characters
p = p.ltrim();
uint32_t bytes_decoded = 0;
llvm::StringRef rest =
UUID::DecodeUUIDBytesFromString(p, m_uuid, num_uuid_bytes);
size_t bytes_decoded = p.size() - rest.size();
UUID::DecodeUUIDBytesFromString(p, m_uuid, bytes_decoded, num_uuid_bytes);
// If we successfully decoded a UUID, return the amount of characters that
// were consumed

View File

@ -79,8 +79,9 @@ size_t OptionValueUUID::AutoComplete(CommandInterpreter &interpreter,
const size_t num_modules = target->GetImages().GetSize();
if (num_modules > 0) {
UUID::ValueType uuid_bytes;
llvm::StringRef rest = UUID::DecodeUUIDBytesFromString(s, uuid_bytes);
const size_t num_bytes_decoded = s.size() - rest.size();
size_t num_bytes_decoded = 0;
llvm::StringRef rest =
UUID::DecodeUUIDBytesFromString(s, uuid_bytes, num_bytes_decoded);
for (size_t i = 0; i < num_modules; ++i) {
ModuleSP module_sp(target->GetImages().GetModuleAtIndex(i));
if (module_sp) {

View File

@ -185,7 +185,7 @@ Error AdbClient::SetPortForwarding(const uint16_t local_port,
}
Error AdbClient::SetPortForwarding(const uint16_t local_port,
const char *remote_socket_name,
llvm::StringRef remote_socket_name,
const UnixSocketNamespace socket_namespace) {
char message[PATH_MAX];
const char *sock_namespace_str =
@ -193,7 +193,7 @@ Error AdbClient::SetPortForwarding(const uint16_t local_port,
? kSocketNamespaceAbstract
: kSocketNamespaceFileSystem;
snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port,
sock_namespace_str, remote_socket_name);
sock_namespace_str, remote_socket_name.str().c_str());
const auto error = SendDeviceMessage(message);
if (error.Fail())

View File

@ -96,7 +96,7 @@ public:
const uint16_t remote_port);
Error SetPortForwarding(const uint16_t local_port,
const char *remote_socket_name,
llvm::StringRef remote_socket_name,
const UnixSocketNamespace socket_namespace);
Error DeletePortForwarding(const uint16_t local_port);

View File

@ -168,7 +168,7 @@ Error PlatformAndroid::ConnectRemote(Args &args) {
m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
int port;
std::string scheme, host, path;
llvm::StringRef scheme, host, path;
const char *url = args.GetArgumentAtIndex(0);
if (!url)
return Error("URL is null.");

View File

@ -27,7 +27,7 @@ static const lldb::pid_t g_remote_platform_pid =
static Error ForwardPortWithAdb(
const uint16_t local_port, const uint16_t remote_port,
const char *remote_socket_name,
llvm::StringRef remote_socket_name,
const llvm::Optional<AdbClient::UnixSocketNamespace> &socket_namespace,
std::string &device_id) {
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
@ -50,7 +50,7 @@ static Error ForwardPortWithAdb(
if (log)
log->Printf("Forwarding remote socket \"%s\" to local TCP port %d",
remote_socket_name, local_port);
remote_socket_name.str().c_str(), local_port);
if (!socket_namespace)
return Error("Invalid socket namespace");
@ -114,7 +114,7 @@ Error PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
return Error("\"platform connect\" takes a single argument: <connect-url>");
int remote_port;
std::string scheme, host, path;
llvm::StringRef scheme, host, path;
const char *url = args.GetArgumentAtIndex(0);
if (!url)
return Error("URL is null.");
@ -132,7 +132,7 @@ Error PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
std::string connect_url;
auto error =
MakeConnectURL(g_remote_platform_pid, (remote_port < 0) ? 0 : remote_port,
path.c_str(), connect_url);
path, connect_url);
if (error.Fail())
return error;
@ -175,7 +175,7 @@ void PlatformAndroidRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) {
Error PlatformAndroidRemoteGDBServer::MakeConnectURL(
const lldb::pid_t pid, const uint16_t remote_port,
const char *remote_socket_name, std::string &connect_url) {
llvm::StringRef remote_socket_name, std::string &connect_url) {
static const int kAttempsNum = 5;
Error error;
@ -214,7 +214,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
int remote_port;
std::string scheme, host, path;
llvm::StringRef scheme, host, path;
if (!UriParser::Parse(connect_url, scheme, host, remote_port, path)) {
error.SetErrorStringWithFormat("Invalid URL: %s", connect_url);
return nullptr;
@ -222,7 +222,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
std::string new_connect_url;
error = MakeConnectURL(s_remote_gdbserver_fake_pid--,
(remote_port < 0) ? 0 : remote_port, path.c_str(),
(remote_port < 0) ? 0 : remote_port, path,
new_connect_url);
if (error.Fail())
return nullptr;

View File

@ -55,7 +55,7 @@ protected:
void DeleteForwardPort(lldb::pid_t pid);
Error MakeConnectURL(const lldb::pid_t pid, const uint16_t remote_port,
const char *remote_socket_name,
llvm::StringRef remote_socket_name,
std::string &connect_url);
private:

View File

@ -309,9 +309,12 @@ Error PlatformRemoteGDBServer::ConnectRemote(Args &args) {
const char *url = args.GetArgumentAtIndex(0);
if (!url)
return Error("URL is null.");
if (!UriParser::Parse(url, m_platform_scheme, m_platform_hostname, port,
path))
llvm::StringRef scheme, hostname, pathname;
if (!UriParser::Parse(url, scheme, hostname, port, pathname))
return Error("Invalid URL: %s", url);
m_platform_scheme = scheme;
m_platform_hostname = hostname;
path = pathname;
const ConnectionStatus status = m_gdb_client.Connect(url, &error);
if (status == eConnectionStatusSuccess) {

View File

@ -124,10 +124,10 @@ Error GDBRemoteCommunicationServerPlatform::LaunchGDBServer(
this, std::placeholders::_1),
false);
std::string platform_scheme;
std::string platform_ip;
llvm::StringRef platform_scheme;
llvm::StringRef platform_ip;
int platform_port;
std::string platform_path;
llvm::StringRef platform_path;
bool ok = UriParser::Parse(GetConnection()->GetURI(), platform_scheme,
platform_ip, platform_port, platform_path);
UNUSED_IF_ASSERT_DISABLED(ok);
@ -140,7 +140,7 @@ Error GDBRemoteCommunicationServerPlatform::LaunchGDBServer(
#endif
uint16_t *port_ptr = &port;
if (m_socket_protocol == Socket::ProtocolTcp)
url << platform_ip << ":" << port;
url << platform_ip.str() << ":" << port;
else {
socket_name = GetDomainSocketPath("gdbserver").GetPath();
url << socket_name;

View File

@ -23,18 +23,19 @@ using namespace lldb_private;
//----------------------------------------------------------------------
// UriParser::Parse
//----------------------------------------------------------------------
bool UriParser::Parse(const std::string &uri, std::string &scheme,
std::string &hostname, int &port, std::string &path) {
std::string tmp_scheme, tmp_hostname, tmp_port, tmp_path;
bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
llvm::StringRef &hostname, int &port,
llvm::StringRef &path) {
llvm::StringRef tmp_scheme, tmp_hostname, tmp_port, tmp_path;
static const char *kSchemeSep = "://";
const llvm::StringRef kSchemeSep("://");
auto pos = uri.find(kSchemeSep);
if (pos == std::string::npos)
return false;
// Extract path.
tmp_scheme = uri.substr(0, pos);
auto host_pos = pos + strlen(kSchemeSep);
auto host_pos = pos + kSchemeSep.size();
auto path_pos = uri.find('/', host_pos);
if (path_pos != std::string::npos)
tmp_path = uri.substr(path_pos);
@ -53,28 +54,19 @@ bool UriParser::Parse(const std::string &uri, std::string &scheme,
return false;
tmp_hostname = host_port.substr(1, pos - 1);
host_port.erase(0, pos + 1);
host_port = host_port.drop_front(pos + 1);
if (!host_port.empty() && !host_port.consume_front(":"))
return false;
} else {
pos = host_port.find(':');
tmp_hostname = host_port.substr(
0, (pos != std::string::npos) ? pos : host_port.size());
host_port.erase(0, (pos != std::string::npos) ? pos : host_port.size());
std::tie(tmp_hostname, host_port) = host_port.split(':');
}
// Extract port
tmp_port = host_port;
if (!tmp_port.empty()) {
if (tmp_port[0] != ':')
if (!host_port.empty()) {
uint16_t port_value = 0;
if (host_port.getAsInteger(0, port_value))
return false;
tmp_port = tmp_port.substr(1);
bool success = false;
auto port_tmp =
StringConvert::ToUInt32(tmp_port.c_str(), UINT32_MAX, 10, &success);
if (!success || port_tmp > 65535) {
// there are invalid characters in port_buf
return false;
}
port = port_tmp;
port = port_value;
} else
port = -1;

View File

@ -12,9 +12,10 @@
// C Includes
// C++ Includes
#include <string>
// Other libraries and framework includes
#include "llvm/ADT/StringRef.h"
// Project includes
class UriParser {
@ -27,8 +28,9 @@ public:
//
// if the url is invalid, function returns false and
// output parameters remain unchanged
static bool Parse(const std::string &uri, std::string &scheme,
std::string &hostname, int &port, std::string &path);
static bool Parse(llvm::StringRef uri, llvm::StringRef &scheme,
llvm::StringRef &hostname, int &port,
llvm::StringRef &path);
};
#endif // utility_UriParser_h_

View File

@ -30,16 +30,16 @@ public:
};
#define VALIDATE \
std::string scheme(kAsdf); \
std::string hostname(kAsdf); \
llvm::StringRef scheme(kAsdf); \
llvm::StringRef hostname(kAsdf); \
int port(1138); \
std::string path(kAsdf); \
llvm::StringRef path(kAsdf); \
EXPECT_EQ(testCase.m_result, \
UriParser::Parse(testCase.m_uri, scheme, hostname, port, path)); \
EXPECT_STREQ(testCase.m_scheme, scheme.c_str()); \
EXPECT_STREQ(testCase.m_hostname, hostname.c_str()); \
EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str()); \
EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str()); \
EXPECT_EQ(testCase.m_port, port); \
EXPECT_STREQ(testCase.m_path, path.c_str());
EXPECT_STREQ(testCase.m_path, path.str().c_str());
TEST_F(UriParserTest, Minimal) {
const UriTestCase testCase("x://y", "x", "y", -1, "/");
@ -48,7 +48,17 @@ TEST_F(UriParserTest, Minimal) {
TEST_F(UriParserTest, MinimalPort) {
const UriTestCase testCase("x://y:1", "x", "y", 1, "/");
VALIDATE
llvm::StringRef scheme(kAsdf);
llvm::StringRef hostname(kAsdf);
int port(1138);
llvm::StringRef path(kAsdf);
bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
EXPECT_EQ(testCase.m_result, result);
EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
EXPECT_EQ(testCase.m_port, port);
EXPECT_STREQ(testCase.m_path, path.str().c_str());
}
TEST_F(UriParserTest, MinimalPath) {
@ -69,13 +79,23 @@ TEST_F(UriParserTest, LongPath) {
TEST_F(UriParserTest, TypicalPortPath) {
const UriTestCase testCase("connect://192.168.100.132:5432/", "connect",
"192.168.100.132", 5432, "/");
VALIDATE
VALIDATE;
}
TEST_F(UriParserTest, BracketedHostnamePort) {
const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect",
"192.168.100.132", 5432, "/");
VALIDATE
llvm::StringRef scheme(kAsdf);
llvm::StringRef hostname(kAsdf);
int port(1138);
llvm::StringRef path(kAsdf);
bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
EXPECT_EQ(testCase.m_result, result);
EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
EXPECT_EQ(testCase.m_port, port);
EXPECT_STREQ(testCase.m_path, path.str().c_str());
}
TEST_F(UriParserTest, BracketedHostname) {