summaryrefslogtreecommitdiffstats
path: root/lldb/source/Utility/StringExtractor.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2016-08-29 19:58:14 +0000
committerZachary Turner <zturner@google.com>2016-08-29 19:58:14 +0000
commit54695a339f5d4f336865a564ab539ff1b5743223 (patch)
tree8613fa198a50f1f645644ecf602ec33a2159c606 /lldb/source/Utility/StringExtractor.cpp
parent8b4a30584a8e34893d3ded7923479bfc89b45f40 (diff)
downloadbcm5719-llvm-54695a339f5d4f336865a564ab539ff1b5743223.tar.gz
bcm5719-llvm-54695a339f5d4f336865a564ab539ff1b5743223.zip
Convert GetNameColonValue to return StringRefs.
StringExtractor::GetNameColonValue() looks for a substring of the form "<name>:<value>" and returns <name> and <value> to the caller. This results in two unnecessary string copies, since the name and value are not translated in any way and simply returned as-is. By converting this to return StringRefs we can get rid of hundreds of string copies. llvm-svn: 280000
Diffstat (limited to 'lldb/source/Utility/StringExtractor.cpp')
-rw-r--r--lldb/source/Utility/StringExtractor.cpp46
1 files changed, 30 insertions, 16 deletions
diff --git a/lldb/source/Utility/StringExtractor.cpp b/lldb/source/Utility/StringExtractor.cpp
index e25acabfaf8..744e7d482cb 100644
--- a/lldb/source/Utility/StringExtractor.cpp
+++ b/lldb/source/Utility/StringExtractor.cpp
@@ -13,6 +13,7 @@
#include <stdlib.h>
// C++ Includes
+#include <tuple>
// Other libraries and framework includes
// Project includes
@@ -37,6 +38,10 @@ StringExtractor::StringExtractor() :
{
}
+StringExtractor::StringExtractor(llvm::StringRef packet_str) : m_packet(), m_index(0)
+{
+ m_packet.assign(packet_str.begin(), packet_str.end());
+}
StringExtractor::StringExtractor(const char *packet_cstr) :
m_packet(),
@@ -468,28 +473,37 @@ StringExtractor::GetHexByteStringTerminatedBy (std::string &str,
}
bool
-StringExtractor::GetNameColonValue (std::string &name, std::string &value)
+StringExtractor::GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value)
{
// Read something in the form of NNNN:VVVV; where NNNN is any character
// that is not a colon, followed by a ':' character, then a value (one or
// more ';' chars), followed by a ';'
- if (m_index < m_packet.size())
+ if (m_index >= m_packet.size())
+ return fail();
+
+ llvm::StringRef view(m_packet);
+ if (view.empty())
+ return fail();
+
+ llvm::StringRef a, b, c, d;
+ view = view.substr(m_index);
+ std::tie(a, b) = view.split(':');
+ if (a.empty() || b.empty())
+ return fail();
+ std::tie(c, d) = b.split(';');
+ if (b == c && d.empty())
+ return fail();
+
+ name = a;
+ value = c;
+ if (d.empty())
+ m_index = m_packet.size();
+ else
{
- const size_t colon_idx = m_packet.find (':', m_index);
- if (colon_idx != std::string::npos)
- {
- const size_t semicolon_idx = m_packet.find (';', colon_idx);
- if (semicolon_idx != std::string::npos)
- {
- name.assign (m_packet, m_index, colon_idx - m_index);
- value.assign (m_packet, colon_idx + 1, semicolon_idx - (colon_idx + 1));
- m_index = semicolon_idx + 1;
- return true;
- }
- }
+ size_t bytes_consumed = d.data() - view.data();
+ m_index += bytes_consumed;
}
- m_index = UINT64_MAX;
- return false;
+ return true;
}
void
OpenPOWER on IntegriCloud