StringExtractor.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef LLDB_UTILITY_STRINGEXTRACTOR_H
  9. #define LLDB_UTILITY_STRINGEXTRACTOR_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include <cstddef>
  13. #include <cstdint>
  14. #include <string>
  15. class StringExtractor {
  16. public:
  17. enum { BigEndian = 0, LittleEndian = 1 };
  18. // Constructors and Destructors
  19. StringExtractor();
  20. StringExtractor(llvm::StringRef packet_str);
  21. StringExtractor(const char *packet_cstr);
  22. virtual ~StringExtractor();
  23. void Reset(llvm::StringRef str) {
  24. m_packet = std::string(str);
  25. m_index = 0;
  26. }
  27. // Returns true if the file position is still valid for the data contained in
  28. // this string extractor object.
  29. bool IsGood() const { return m_index != UINT64_MAX; }
  30. uint64_t GetFilePos() const { return m_index; }
  31. void SetFilePos(uint32_t idx) { m_index = idx; }
  32. void Clear() {
  33. m_packet.clear();
  34. m_index = 0;
  35. }
  36. void SkipSpaces();
  37. llvm::StringRef GetStringRef() const { return m_packet; }
  38. bool Empty() { return m_packet.empty(); }
  39. size_t GetBytesLeft() {
  40. if (m_index < m_packet.size())
  41. return m_packet.size() - m_index;
  42. return 0;
  43. }
  44. char GetChar(char fail_value = '\0');
  45. char PeekChar(char fail_value = '\0') {
  46. const char *cstr = Peek();
  47. if (cstr)
  48. return cstr[0];
  49. return fail_value;
  50. }
  51. int DecodeHexU8();
  52. uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true);
  53. bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true);
  54. bool GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value);
  55. int32_t GetS32(int32_t fail_value, int base = 0);
  56. uint32_t GetU32(uint32_t fail_value, int base = 0);
  57. int64_t GetS64(int64_t fail_value, int base = 0);
  58. uint64_t GetU64(uint64_t fail_value, int base = 0);
  59. uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value);
  60. uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value);
  61. size_t GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
  62. uint8_t fail_fill_value);
  63. size_t GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest);
  64. size_t GetHexByteString(std::string &str);
  65. size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length);
  66. size_t GetHexByteStringTerminatedBy(std::string &str, char terminator);
  67. bool ConsumeFront(const llvm::StringRef &str);
  68. const char *Peek() {
  69. if (m_index < m_packet.size())
  70. return m_packet.c_str() + m_index;
  71. return nullptr;
  72. }
  73. protected:
  74. bool fail() {
  75. m_index = UINT64_MAX;
  76. return false;
  77. }
  78. /// The string in which to extract data.
  79. std::string m_packet;
  80. /// When extracting data from a packet, this index will march along as things
  81. /// get extracted. If set to UINT64_MAX the end of the packet data was
  82. /// reached when decoding information.
  83. uint64_t m_index;
  84. };
  85. #endif // LLDB_UTILITY_STRINGEXTRACTOR_H