1 //===-- StringExtractor.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef utility_StringExtractor_h_
10 #define utility_StringExtractor_h_
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
19 class StringExtractor {
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();
28 void Reset(llvm::StringRef str) {
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; }
37 uint64_t GetFilePos() const { return m_index; }
39 void SetFilePos(uint32_t idx) { m_index = idx; }
48 std::string &GetStringRef() { return m_packet; }
50 const std::string &GetStringRef() const { return m_packet; }
52 bool Empty() { return m_packet.empty(); }
54 size_t GetBytesLeft() {
55 if (m_index < m_packet.size())
56 return m_packet.size() - m_index;
60 char GetChar(char fail_value = '\0');
62 char PeekChar(char fail_value = '\0') {
63 const char *cstr = Peek();
71 uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true);
73 bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true);
75 bool GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value);
77 int32_t GetS32(int32_t fail_value, int base = 0);
79 uint32_t GetU32(uint32_t fail_value, int base = 0);
81 int64_t GetS64(int64_t fail_value, int base = 0);
83 uint64_t GetU64(uint64_t fail_value, int base = 0);
85 uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value);
87 uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value);
89 size_t GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
90 uint8_t fail_fill_value);
92 size_t GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest);
94 uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian,
97 size_t GetHexByteString(std::string &str);
99 size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length);
101 size_t GetHexByteStringTerminatedBy(std::string &str, char terminator);
103 bool ConsumeFront(const llvm::StringRef &str);
106 if (m_index < m_packet.size())
107 return m_packet.c_str() + m_index;
113 m_index = UINT64_MAX;
116 // For StringExtractor only
117 std::string m_packet; // The string in which to extract data.
118 uint64_t m_index; // When extracting data from a packet, this index
119 // will march along as things get extracted. If set to
120 // UINT64_MAX the end of the packet data was reached
121 // when decoding information
124 #endif // utility_StringExtractor_h_