Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:58

0001 //===-- StringExtractor.h ---------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_STRINGEXTRACTOR_H
0010 #define LLDB_UTILITY_STRINGEXTRACTOR_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/StringRef.h"
0014 
0015 #include <cstddef>
0016 #include <cstdint>
0017 #include <string>
0018 
0019 class StringExtractor {
0020 public:
0021   enum { BigEndian = 0, LittleEndian = 1 };
0022   // Constructors and Destructors
0023   StringExtractor();
0024   StringExtractor(llvm::StringRef packet_str);
0025   StringExtractor(const char *packet_cstr);
0026   virtual ~StringExtractor();
0027 
0028   void Reset(llvm::StringRef str) {
0029     m_packet = std::string(str);
0030     m_index = 0;
0031   }
0032 
0033   // Returns true if the file position is still valid for the data contained in
0034   // this string extractor object.
0035   bool IsGood() const { return m_index != UINT64_MAX; }
0036 
0037   uint64_t GetFilePos() const { return m_index; }
0038 
0039   void SetFilePos(uint32_t idx) { m_index = idx; }
0040 
0041   void Clear() {
0042     m_packet.clear();
0043     m_index = 0;
0044   }
0045 
0046   void SkipSpaces();
0047 
0048   llvm::StringRef GetStringRef() const { return m_packet; }
0049 
0050   bool Empty() { return m_packet.empty(); }
0051 
0052   size_t GetBytesLeft() {
0053     if (m_index < m_packet.size())
0054       return m_packet.size() - m_index;
0055     return 0;
0056   }
0057 
0058   char GetChar(char fail_value = '\0');
0059 
0060   char PeekChar(char fail_value = '\0') {
0061     const char *cstr = Peek();
0062     if (cstr)
0063       return cstr[0];
0064     return fail_value;
0065   }
0066 
0067   int DecodeHexU8();
0068 
0069   uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true);
0070 
0071   bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true);
0072 
0073   bool GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value);
0074 
0075   int32_t GetS32(int32_t fail_value, int base = 0);
0076 
0077   uint32_t GetU32(uint32_t fail_value, int base = 0);
0078 
0079   int64_t GetS64(int64_t fail_value, int base = 0);
0080 
0081   uint64_t GetU64(uint64_t fail_value, int base = 0);
0082 
0083   uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value);
0084 
0085   uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value);
0086 
0087   size_t GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
0088                      uint8_t fail_fill_value);
0089 
0090   size_t GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest);
0091 
0092   size_t GetHexByteString(std::string &str);
0093 
0094   size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length);
0095 
0096   size_t GetHexByteStringTerminatedBy(std::string &str, char terminator);
0097 
0098   bool ConsumeFront(const llvm::StringRef &str);
0099 
0100   const char *Peek() {
0101     if (m_index < m_packet.size())
0102       return m_packet.c_str() + m_index;
0103     return nullptr;
0104   }
0105 
0106 protected:
0107   bool fail() {
0108     m_index = UINT64_MAX;
0109     return false;
0110   }
0111 
0112   /// The string in which to extract data.
0113   std::string m_packet;
0114 
0115   /// When extracting data from a packet, this index will march along as things
0116   /// get extracted. If set to UINT64_MAX the end of the packet data was
0117   /// reached when decoding information.
0118   uint64_t m_index = 0;
0119 };
0120 
0121 #endif // LLDB_UTILITY_STRINGEXTRACTOR_H