StringTable.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===- StringTable.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 LLVM_DEBUGINFO_GSYM_STRINGTABLE_H
  9. #define LLVM_DEBUGINFO_GSYM_STRINGTABLE_H
  10. #include "llvm/ADT/Optional.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/DebugInfo/GSYM/Range.h"
  13. #include <stdint.h>
  14. namespace llvm {
  15. namespace gsym {
  16. /// String tables in GSYM files are required to start with an empty
  17. /// string at offset zero. Strings must be UTF8 NULL terminated strings.
  18. struct StringTable {
  19. StringRef Data;
  20. StringTable() : Data() {}
  21. StringTable(StringRef D) : Data(D) {}
  22. StringRef operator[](size_t Offset) const { return getString(Offset); }
  23. StringRef getString(uint32_t Offset) const {
  24. if (Offset < Data.size()) {
  25. auto End = Data.find('\0', Offset);
  26. return Data.substr(Offset, End - Offset);
  27. }
  28. return StringRef();
  29. }
  30. void clear() { Data = StringRef(); }
  31. };
  32. inline raw_ostream &operator<<(raw_ostream &OS, const StringTable &S) {
  33. OS << "String table:\n";
  34. uint32_t Offset = 0;
  35. const size_t Size = S.Data.size();
  36. while (Offset < Size) {
  37. StringRef Str = S.getString(Offset);
  38. OS << HEX32(Offset) << ": \"" << Str << "\"\n";
  39. Offset += Str.size() + 1;
  40. }
  41. return OS;
  42. }
  43. } // namespace gsym
  44. } // namespace llvm
  45. #endif // LLVM_DEBUGINFO_GSYM_STRINGTABLE_H