tqchen commented on a change in pull request #4628: [Object] Add String container URL: https://github.com/apache/incubator-tvm/pull/4628#discussion_r384610283
########## File path: include/tvm/runtime/container.h ########## @@ -274,7 +298,246 @@ 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; + + static constexpr const uint32_t _type_index = TypeIndex::kDynamic; + static constexpr const char* _type_key = "runtime.String"; + TVM_DECLARE_FINAL_OBJECT_INFO(StringObj, Object); + + private: + /*! \brief String object which is moved from std::string container. */ + class FromStd; + + friend class String; +}; + +/*! + * \brief Reference to string objects. + * + * \code + * + * // Example to create runtime String reference object from std::string + * std::string s = "hello world"; + * + * // You can create the reference from existing std::string + * String ref{std::move(s)}; + * + * // You can rebind the reference to another string. + * ref = std::string{"hello world2"}; + * + * // You can use the reference as hash map key + * std::unordered<String, int32_t) m; + * m[ref] = 1; + * + * // You can compare the reference object with other string objects + * assert(ref == "hello world", true); + * + * // You can convert the reference to std::string again + * string s2 = (string)ref; + * + * \endcode + */ +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 Change the value the reference object points to. + * + * \param other The value for the new String + * + */ + inline String operator=(std::string other); + + /*! + * \brief Compare is equal to other std::string + * + * \param other The other string + * + * \return the comparison result + */ + bool operator==(const std::string& other) const { + return size() == other.size() && + other.compare(0, other.size(), get()->data, size()) == 0; + } + + /*! + * \brief Compare is not equal to other std::string + * + * \param other The other string + * + * \return the comparison result + */ + bool operator!=(const std::string& other) const { return !operator==(other); } + + /*! + * \brief Compare is equal to other char string + * + * \param other The other char string + * + * \return the comparison result + */ + bool operator==(const char* other) const { return !operator!=(other); } + + /*! + * \brief Compare is not equal to other char string + * + * \param other The other char string + * + * \return the comparison result + */ + bool operator!=(const char* other) const { + return size() != std::strlen(other) || compare(other, size()) != 0; + } + + /*! + * \brief Compares this to other for at most len chars + * + * \return zero if both char sequences compare equal. negative if this appear + * before other, positive otherwise. + */ + int compare(const String& other, size_t len) const { + return compare(other.data(), len); + } + + /*! + * \brief Compares this to other for at most len chars + * + * \return zero if both char sequences compare equal. negative if this appear + * before other, positive otherwise. + */ + int compare(const std::string& other, size_t len) const { + return compare(other.data(), len); + } + + /*! + * \brief Compares this to other for at most len chars + * + * \return zero if both char sequences compare equal. negative if this appear + * before other, positive otherwise. + */ + int compare(const char* other, size_t len) const { + if (other == get()->data) { + return 0; + } + return std::memcmp(get()->data, other, len); Review comment: I think we need more thoughts into this. I see a few problems: -The interface of compare is not consistent with std, and we should change it to make sure it is consistent http://www.cplusplus.com/reference/string/string/compare/ - While the current usage of compare (internally) in operator== and operator!= compares the length first. We cannot guarantee that the user will have the same behavior. Given that this is a public function, we should make sure consistent behavior with std. Which leads to the conclusion that perhaps we need an explicit for loop, some testcases are desirable ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services