1 //===-- StringExtractor.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/StringExtractor.h"
18 static inline int xdigit_to_sint(char ch) {
19 if (ch >= 'a' && ch <= 'f')
21 if (ch >= 'A' && ch <= 'F')
23 if (ch >= '0' && ch <= '9')
28 //----------------------------------------------------------------------
29 // StringExtractor constructor
30 //----------------------------------------------------------------------
31 StringExtractor::StringExtractor() : m_packet(), m_index(0) {}
33 StringExtractor::StringExtractor(llvm::StringRef packet_str)
34 : m_packet(), m_index(0) {
35 m_packet.assign(packet_str.begin(), packet_str.end());
38 StringExtractor::StringExtractor(const char *packet_cstr)
39 : m_packet(), m_index(0) {
41 m_packet.assign(packet_cstr);
44 //----------------------------------------------------------------------
45 // StringExtractor copy constructor
46 //----------------------------------------------------------------------
47 StringExtractor::StringExtractor(const StringExtractor &rhs)
48 : m_packet(rhs.m_packet), m_index(rhs.m_index) {}
50 //----------------------------------------------------------------------
51 // StringExtractor assignment operator
52 //----------------------------------------------------------------------
53 const StringExtractor &StringExtractor::operator=(const StringExtractor &rhs) {
55 m_packet = rhs.m_packet;
56 m_index = rhs.m_index;
61 //----------------------------------------------------------------------
63 //----------------------------------------------------------------------
64 StringExtractor::~StringExtractor() {}
66 char StringExtractor::GetChar(char fail_value) {
67 if (m_index < m_packet.size()) {
68 char ch = m_packet[m_index];
76 //----------------------------------------------------------------------
77 // If a pair of valid hex digits exist at the head of the StringExtractor they
78 // are decoded into an unsigned byte and returned by this function
80 // If there is not a pair of valid hex digits at the head of the
81 // StringExtractor, it is left unchanged and -1 is returned
82 //----------------------------------------------------------------------
83 int StringExtractor::DecodeHexU8() {
85 if (GetBytesLeft() < 2) {
88 const int hi_nibble = xdigit_to_sint(m_packet[m_index]);
89 const int lo_nibble = xdigit_to_sint(m_packet[m_index + 1]);
90 if (hi_nibble == -1 || lo_nibble == -1) {
94 return (uint8_t)((hi_nibble << 4) + lo_nibble);
97 //----------------------------------------------------------------------
98 // Extract an unsigned character from two hex ASCII chars in the packet string,
99 // or return fail_value on failure
100 //----------------------------------------------------------------------
101 uint8_t StringExtractor::GetHexU8(uint8_t fail_value, bool set_eof_on_fail) {
102 // On success, fail_value will be overwritten with the next character in the
104 GetHexU8Ex(fail_value, set_eof_on_fail);
108 bool StringExtractor::GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail) {
109 int byte = DecodeHexU8();
111 if (set_eof_on_fail || m_index >= m_packet.size())
112 m_index = UINT64_MAX;
113 // ch should not be changed in case of failure
120 uint32_t StringExtractor::GetU32(uint32_t fail_value, int base) {
121 if (m_index < m_packet.size()) {
123 const char *start = m_packet.c_str();
124 const char *cstr = start + m_index;
125 uint32_t result = static_cast<uint32_t>(::strtoul(cstr, &end, base));
127 if (end && end != cstr) {
128 m_index = end - start;
135 int32_t StringExtractor::GetS32(int32_t fail_value, int base) {
136 if (m_index < m_packet.size()) {
138 const char *start = m_packet.c_str();
139 const char *cstr = start + m_index;
140 int32_t result = static_cast<int32_t>(::strtol(cstr, &end, base));
142 if (end && end != cstr) {
143 m_index = end - start;
150 uint64_t StringExtractor::GetU64(uint64_t fail_value, int base) {
151 if (m_index < m_packet.size()) {
153 const char *start = m_packet.c_str();
154 const char *cstr = start + m_index;
155 uint64_t result = ::strtoull(cstr, &end, base);
157 if (end && end != cstr) {
158 m_index = end - start;
165 int64_t StringExtractor::GetS64(int64_t fail_value, int base) {
166 if (m_index < m_packet.size()) {
168 const char *start = m_packet.c_str();
169 const char *cstr = start + m_index;
170 int64_t result = ::strtoll(cstr, &end, base);
172 if (end && end != cstr) {
173 m_index = end - start;
180 uint32_t StringExtractor::GetHexMaxU32(bool little_endian,
181 uint32_t fail_value) {
183 uint32_t nibble_count = 0;
187 uint32_t shift_amount = 0;
188 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
189 // Make sure we don't exceed the size of a uint32_t...
190 if (nibble_count >= (sizeof(uint32_t) * 2)) {
191 m_index = UINT64_MAX;
196 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
198 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
199 nibble_lo = xdigit_to_sint(m_packet[m_index]);
201 result |= ((uint32_t)nibble_hi << (shift_amount + 4));
202 result |= ((uint32_t)nibble_lo << shift_amount);
206 result |= ((uint32_t)nibble_hi << shift_amount);
212 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
213 // Make sure we don't exceed the size of a uint32_t...
214 if (nibble_count >= (sizeof(uint32_t) * 2)) {
215 m_index = UINT64_MAX;
219 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
231 uint64_t StringExtractor::GetHexMaxU64(bool little_endian,
232 uint64_t fail_value) {
234 uint32_t nibble_count = 0;
238 uint32_t shift_amount = 0;
239 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
240 // Make sure we don't exceed the size of a uint64_t...
241 if (nibble_count >= (sizeof(uint64_t) * 2)) {
242 m_index = UINT64_MAX;
247 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
249 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
250 nibble_lo = xdigit_to_sint(m_packet[m_index]);
252 result |= ((uint64_t)nibble_hi << (shift_amount + 4));
253 result |= ((uint64_t)nibble_lo << shift_amount);
257 result |= ((uint64_t)nibble_hi << shift_amount);
263 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
264 // Make sure we don't exceed the size of a uint64_t...
265 if (nibble_count >= (sizeof(uint64_t) * 2)) {
266 m_index = UINT64_MAX;
270 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
282 bool StringExtractor::ConsumeFront(const llvm::StringRef &str) {
283 llvm::StringRef S = GetStringRef();
284 if (!S.startswith(str))
287 m_index += str.size();
291 size_t StringExtractor::GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
292 uint8_t fail_fill_value) {
293 size_t bytes_extracted = 0;
294 while (!dest.empty() && GetBytesLeft() > 0) {
295 dest[0] = GetHexU8(fail_fill_value);
299 dest = dest.drop_front();
303 ::memset(dest.data(), fail_fill_value, dest.size());
305 return bytes_extracted;
308 //----------------------------------------------------------------------
309 // Decodes all valid hex encoded bytes at the head of the StringExtractor,
310 // limited by dst_len.
312 // Returns the number of bytes successfully decoded
313 //----------------------------------------------------------------------
314 size_t StringExtractor::GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest) {
315 size_t bytes_extracted = 0;
316 while (!dest.empty()) {
317 int decode = DecodeHexU8();
320 dest[0] = (uint8_t)decode;
321 dest = dest.drop_front();
324 return bytes_extracted;
327 // Consume ASCII hex nibble character pairs until we have decoded byte_size
330 uint64_t StringExtractor::GetHexWithFixedSize(uint32_t byte_size,
332 uint64_t fail_value) {
333 if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2) {
338 uint32_t shift_amount;
339 for (i = 0, shift_amount = 0; i < byte_size && IsGood();
340 ++i, shift_amount += 8) {
341 result |= ((uint64_t)GetHexU8() << shift_amount);
345 for (i = 0; i < byte_size && IsGood(); ++i) {
347 result |= GetHexU8();
351 m_index = UINT64_MAX;
355 size_t StringExtractor::GetHexByteString(std::string &str) {
357 str.reserve(GetBytesLeft() / 2);
359 while ((ch = GetHexU8()) != '\0')
364 size_t StringExtractor::GetHexByteStringFixedLength(std::string &str,
365 uint32_t nibble_length) {
368 uint32_t nibble_count = 0;
369 for (const char *pch = Peek();
370 (nibble_count < nibble_length) && (pch != nullptr);
371 str.append(1, GetHexU8(0, false)), pch = Peek(), nibble_count += 2) {
377 size_t StringExtractor::GetHexByteStringTerminatedBy(std::string &str,
381 while ((ch = GetHexU8(0, false)) != '\0')
383 if (Peek() && *Peek() == terminator)
390 bool StringExtractor::GetNameColonValue(llvm::StringRef &name,
391 llvm::StringRef &value) {
392 // Read something in the form of NNNN:VVVV; where NNNN is any character that
393 // is not a colon, followed by a ':' character, then a value (one or more ';'
394 // chars), followed by a ';'
395 if (m_index >= m_packet.size())
398 llvm::StringRef view(m_packet);
402 llvm::StringRef a, b, c, d;
403 view = view.substr(m_index);
404 std::tie(a, b) = view.split(':');
405 if (a.empty() || b.empty())
407 std::tie(c, d) = b.split(';');
408 if (b == c && d.empty())
414 m_index = m_packet.size();
416 size_t bytes_consumed = d.data() - view.data();
417 m_index += bytes_consumed;
422 void StringExtractor::SkipSpaces() {
423 const size_t n = m_packet.size();
424 while (m_index < n && isspace(m_packet[m_index]))