LLDB  mainline
StringExtractor.h
Go to the documentation of this file.
1 //===-- StringExtractor.h ---------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef utility_StringExtractor_h_
10 #define utility_StringExtractor_h_
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <string>
18 
20 public:
21  enum { BigEndian = 0, LittleEndian = 1 };
22  // Constructors and Destructors
24  StringExtractor(llvm::StringRef packet_str);
25  StringExtractor(const char *packet_cstr);
26  StringExtractor(const StringExtractor &rhs);
27  virtual ~StringExtractor();
28 
29  // Operators
30  const StringExtractor &operator=(const StringExtractor &rhs);
31 
32  void Reset(llvm::StringRef str) {
33  m_packet = str;
34  m_index = 0;
35  }
36 
37  // Returns true if the file position is still valid for the data contained in
38  // this string extractor object.
39  bool IsGood() const { return m_index != UINT64_MAX; }
40 
41  uint64_t GetFilePos() const { return m_index; }
42 
43  void SetFilePos(uint32_t idx) { m_index = idx; }
44 
45  void Clear() {
46  m_packet.clear();
47  m_index = 0;
48  }
49 
50  void SkipSpaces();
51 
52  std::string &GetStringRef() { return m_packet; }
53 
54  const std::string &GetStringRef() const { return m_packet; }
55 
56  bool Empty() { return m_packet.empty(); }
57 
58  size_t GetBytesLeft() {
59  if (m_index < m_packet.size())
60  return m_packet.size() - m_index;
61  return 0;
62  }
63 
64  char GetChar(char fail_value = '\0');
65 
66  char PeekChar(char fail_value = '\0') {
67  const char *cstr = Peek();
68  if (cstr)
69  return cstr[0];
70  return fail_value;
71  }
72 
73  int DecodeHexU8();
74 
75  uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true);
76 
77  bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true);
78 
79  bool GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value);
80 
81  int32_t GetS32(int32_t fail_value, int base = 0);
82 
83  uint32_t GetU32(uint32_t fail_value, int base = 0);
84 
85  int64_t GetS64(int64_t fail_value, int base = 0);
86 
87  uint64_t GetU64(uint64_t fail_value, int base = 0);
88 
89  uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value);
90 
91  uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value);
92 
93  size_t GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
94  uint8_t fail_fill_value);
95 
96  size_t GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest);
97 
98  uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian,
99  uint64_t fail_value);
100 
101  size_t GetHexByteString(std::string &str);
102 
103  size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length);
104 
105  size_t GetHexByteStringTerminatedBy(std::string &str, char terminator);
106 
107  bool ConsumeFront(const llvm::StringRef &str);
108 
109  const char *Peek() {
110  if (m_index < m_packet.size())
111  return m_packet.c_str() + m_index;
112  return nullptr;
113  }
114 
115 protected:
116  bool fail() {
118  return false;
119  }
120  // For StringExtractor only
121  std::string m_packet; // The string in which to extract data.
122  uint64_t m_index; // When extracting data from a packet, this index
123  // will march along as things get extracted. If set to
124  // UINT64_MAX the end of the packet data was reached
125  // when decoding information
126 };
127 
128 #endif // utility_StringExtractor_h_
uint8_t GetHexU8(uint8_t fail_value=0, bool set_eof_on_fail=true)
uint64_t GetFilePos() const
bool ConsumeFront(const llvm::StringRef &str)
int32_t GetS32(int32_t fail_value, int base=0)
std::string m_packet
char PeekChar(char fail_value='\0')
uint64_t GetU64(uint64_t fail_value, int base=0)
size_t GetHexByteString(std::string &str)
uint32_t GetU32(uint32_t fail_value, int base=0)
uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian, uint64_t fail_value)
void Reset(llvm::StringRef str)
bool IsGood() const
void SetFilePos(uint32_t idx)
uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value)
bool GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value)
size_t GetHexBytes(llvm::MutableArrayRef< uint8_t > dest, uint8_t fail_fill_value)
const StringExtractor & operator=(const StringExtractor &rhs)
size_t GetHexBytesAvail(llvm::MutableArrayRef< uint8_t > dest)
size_t GetBytesLeft()
int64_t GetS64(int64_t fail_value, int base=0)
const std::string & GetStringRef() const
virtual ~StringExtractor()
const char * Peek()
uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value)
std::string & GetStringRef()
size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length)
#define UINT64_MAX
Definition: lldb-defines.h:35
char GetChar(char fail_value='\0')
bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail=true)
size_t GetHexByteStringTerminatedBy(std::string &str, char terminator)