tqchen commented on a change in pull request #4628: [Object] Add String
container
URL: https://github.com/apache/incubator-tvm/pull/4628#discussion_r365605959
##########
File path: include/tvm/runtime/container.h
##########
@@ -274,6 +276,137 @@ class ADT : public ObjectRef {
TVM_DEFINE_OBJECT_REF_METHODS(ADT, ObjectRef, ADTObj);
};
+/*! \brief An object representing string. It's POD type. */
+class StringObj : public Object {
+ public:
+ /*! \brief The length of the string object. */
+ uint32_t size;
+
+ /*! \brief The pointer to string data. */
+ const char* data;
+
+ private:
+ /*! \brief String object which is moved from std::string container. */
+ class FromStd;
+
+ friend class String;
+};
+
+/*! \brief reference to string objects. */
+class String : public ObjectRef {
+ public:
+ /*!
+ * \brief Construct a new String object
+ *
+ * \param other The moved/copied std::string object
+ *
+ * \note If user passes const reference, it will trigger copy. If it's
rvalue,
+ * it will be moved into other.
+ */
+ inline explicit String(std::string other);
+
+ /*!
+ * \brief Compare is equal to other std::string
+ *
+ * \param other The other string
+ */
+ inline bool operator==(std::string other) const;
+
+ /*!
+ * \brief Compare is not equal to other std::string
+ *
+ * \param other The other string
+ */
+ inline bool operator!=(std::string other) const;
+
+ /*!
+ * \brief Compare is equal to other char string
+ *
+ * \param other The other char string
+ */
+ inline bool operator==(const char* other) const;
+
+ /*!
+ * \brief Compare is not equal to other char string
+ *
+ * \param other The other char string
+ */
+ inline bool operator!=(const char* other) const;
+
+ /*!
+ * \brief Returns a pointer to the char array in the string.
+ *
+ * \return const char*
+ */
+ inline const char* c_str() const;
+
+ /*!
+ * \brief Return the length of the string
+ *
+ * \return size_t string length
+ */
+ inline size_t size() const;
+
+ /*!
+ * \brief Return the data pointer
+ *
+ * \return const char* data pointer
+ */
+ inline const char* data() const;
+
+ /*!
+ * \brief Convert String to an std::sting object
+ *
+ * \return std::string
+ */
+ inline operator std::string() const;
+
+ TVM_DEFINE_OBJECT_REF_METHODS(String, ObjectRef, StringObj);
+};
+
+/*! \brief An object representing string moved from std::string. */
+class StringObj::FromStd : public StringObj {
+ public:
+ /*! \brief Container that holds the memory. */
+ std::string data_container;
+};
+
+inline String::String(std::string other) {
+ auto ptr = make_object<StringObj::FromStd>();
+ ptr->data_container.swap(other);
+ ptr->size = ptr->data_container.size();
+ ptr->data = ptr->data_container.data();
+ data_ = std::move(ptr);
+}
+
+inline bool String::operator==(std::string other) const {
+ return !operator!=(other);
+}
+
+inline bool String::operator!=(std::string other) const {
+ return operator->()->size != other.size() ||
+ std::strncmp(operator->()->data, other.data(), other.size()) != 0;
+}
+
+inline bool String::operator==(const char* other) const {
+ return !operator!=(other);
+}
+
+inline bool String::operator!=(const char* other) const {
+ return operator->()->size != std::strlen(other) ||
+ std::strncmp(operator->()->data, other, operator->()->size) != 0;
Review comment:
We don;t have to do strlen. strncmp should be suffice here
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services