================
@@ -37,10 +40,55 @@ using Id = uint64_t;
 /// the current session.
 static constexpr Id kCalculateSeq = UINT64_MAX;
 
+/// A wrapper around a 'std::string' to ensure the contents are valid utf8
+/// during serialization.
+class String {
+public:
+  String(std::string str) : m_str(str) {}
+  String(llvm::StringRef str) : m_str(str.str()) {}
+  String(const char *str) : m_str(str) {}
+  String() = default;
+
+  operator llvm::Twine() { return m_str; }
+  operator std::string &() { return m_str; }
+  operator std::string() const { return m_str; }
+  operator llvm::StringRef() const { return llvm::StringRef(m_str); }
+
+  void clear() { m_str.clear(); }
+  bool empty() const { return m_str.empty(); }
+  const char *c_str() const { return m_str.c_str(); }
+  const char *data() const { return m_str.data(); }
+  std::string str() const { return m_str; }
+  std::string &str() { return m_str; }
+
+private:
+  std::string m_str;
+};
+llvm::json::Value toJSON(const String &s);
+bool fromJSON(const llvm::json::Value &, String &, llvm::json::Path);
+inline bool operator==(const String &a, const String &b) {
+  return a.str() == b.str();
+}
+inline bool operator==(const String &a, const char *b) { return a.str() == b; }
+inline bool operator==(const char *a, const String &b) { return a == b.str(); }
+inline bool operator==(llvm::StringRef a, const String &b) {
+  return a.str() == b.str();
+}
+inline bool operator==(const String &a, llvm::StringRef b) {
+  return a.str() == b.str();
+}
+inline bool operator<(const String &a, const String &b) {
+  return a.str() < b.str();
+}
+inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const String &S) {
+  OS << S.str();
+  return OS;
+}
----------------
da-viper wrote:

Could you wrap any string arguments in the operators with llvm::StringRef to 
avoid copies for comparison.  and rename from a and b to lhs and res.

i.e `llvm::StringRef(lhs) == llvm::StringRef(rhs)`

https://github.com/llvm/llvm-project/pull/181261
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to