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