junrushao1994 commented on a change in pull request #5806:
URL: https://github.com/apache/incubator-tvm/pull/5806#discussion_r439874434
##########
File path: include/tvm/runtime/container.h
##########
@@ -1410,10 +1344,114 @@ inline String& String::operator=(std::string other) {
inline String& String::operator=(const char* other) { return
operator=(std::string(other)); }
-inline String operator+(const std::string lhs, const String& rhs) {
- return lhs + rhs.operator std::string();
+template <typename T, typename U,
+ typename = typename std::enable_if<std::is_same<T, String>::value ||
+ std::is_same<T,
std::string>::value>::type,
+ typename = typename std::enable_if<std::is_same<U, String>::value ||
+ (std::is_same<U,
std::string>::value &&
+ !std::is_same<T,
U>::value)>::type>
+inline String operator+(const T& lhs, const U& rhs) {
+ size_t lhs_size = lhs.size();
+ size_t rhs_size = rhs.size();
+ char* concat = new char[lhs_size + rhs_size];
+ std::memcpy(concat, lhs.data(), lhs_size);
+ std::memcpy(concat + lhs_size, rhs.data(), rhs_size);
+ auto ptr = make_object<StringObj>();
+ ptr->size = lhs_size + rhs_size;
+ ptr->data = concat;
+ return String(ptr);
+}
+
+inline String operator+(const char* lhs, const String& rhs) {
+ size_t lhs_size = std::strlen(lhs);
+ size_t rhs_size = rhs.size();
+ char* concat = new char[lhs_size + rhs_size];
+ std::memcpy(concat, lhs, lhs_size);
+ std::memcpy(concat + lhs_size, rhs.data(), rhs_size);
+ auto ptr = make_object<StringObj>();
+ ptr->size = lhs_size + rhs_size;
+ ptr->data = concat;
+ return String(ptr);
}
+inline String operator+(const String& lhs, const char* rhs) {
+ size_t lhs_size = lhs.size();
+ size_t rhs_size = std::strlen(rhs);
+ char* concat = new char[lhs_size + rhs_size];
+ std::memcpy(concat, lhs.data(), lhs_size);
+ std::memcpy(concat + lhs_size, rhs, rhs_size);
+ auto ptr = make_object<StringObj>();
+ ptr->size = lhs_size + rhs_size;
+ ptr->data = concat;
+ return String(ptr);
+}
+
+// Overload < operator
+inline bool operator<(const String& lhs, const std::string& rhs) { return
lhs.compare(rhs) < 0; }
+
+inline bool operator<(const std::string& lhs, const String& rhs) { return
rhs.compare(lhs) > 0; }
+
+inline bool operator<(const String& lhs, const String& rhs) { return
lhs.compare(rhs) < 0; }
+
+inline bool operator<(const String& lhs, const char* rhs) { return
lhs.compare(rhs) < 0; }
+
+inline bool operator<(const char* lhs, const String& rhs) { return
rhs.compare(lhs) > 0; }
+
+inline bool operator>(const String& lhs, const std::string& rhs) { return
lhs.compare(rhs) > 0; }
Review comment:
move this two lines downside
##########
File path: include/tvm/runtime/container.h
##########
@@ -1410,10 +1344,114 @@ inline String& String::operator=(std::string other) {
inline String& String::operator=(const char* other) { return
operator=(std::string(other)); }
-inline String operator+(const std::string lhs, const String& rhs) {
- return lhs + rhs.operator std::string();
+template <typename T, typename U,
+ typename = typename std::enable_if<std::is_same<T, String>::value ||
+ std::is_same<T,
std::string>::value>::type,
+ typename = typename std::enable_if<std::is_same<U, String>::value ||
+ (std::is_same<U,
std::string>::value &&
+ !std::is_same<T,
U>::value)>::type>
+inline String operator+(const T& lhs, const U& rhs) {
+ size_t lhs_size = lhs.size();
+ size_t rhs_size = rhs.size();
+ char* concat = new char[lhs_size + rhs_size];
+ std::memcpy(concat, lhs.data(), lhs_size);
+ std::memcpy(concat + lhs_size, rhs.data(), rhs_size);
+ auto ptr = make_object<StringObj>();
+ ptr->size = lhs_size + rhs_size;
+ ptr->data = concat;
+ return String(ptr);
+}
+
+inline String operator+(const char* lhs, const String& rhs) {
+ size_t lhs_size = std::strlen(lhs);
+ size_t rhs_size = rhs.size();
+ char* concat = new char[lhs_size + rhs_size];
Review comment:
Shall we add a `\0` to the end of the string?
----------------------------------------------------------------
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]