@@ -175,7 +175,34 @@ class Bytes : public ObjectRef {
175175 * \return int zero if both char sequences compare equal. negative if this
176176 * appear before other, positive otherwise.
177177 */
178- static int memncmp (const char * lhs, const char * rhs, size_t lhs_count, size_t rhs_count);
178+ static int memncmp (const char * lhs, const char * rhs, size_t lhs_count, size_t rhs_count) {
179+ if (lhs == rhs && lhs_count == rhs_count) return 0 ;
180+
181+ for (size_t i = 0 ; i < lhs_count && i < rhs_count; ++i) {
182+ if (lhs[i] < rhs[i]) return -1 ;
183+ if (lhs[i] > rhs[i]) return 1 ;
184+ }
185+ if (lhs_count < rhs_count) {
186+ return -1 ;
187+ } else if (lhs_count > rhs_count) {
188+ return 1 ;
189+ } else {
190+ return 0 ;
191+ }
192+ }
193+ /* !
194+ * \brief Compare two char sequence for equality
195+ *
196+ * \param lhs Pointers to the char array to compare
197+ * \param rhs Pointers to the char array to compare
198+ * \param lhs_count Length of the char array to compare
199+ * \param rhs_count Length of the char array to compare
200+ *
201+ * \return true if the two char sequences are equal, false otherwise.
202+ */
203+ static bool memequal (const char * lhs, const char * rhs, size_t lhs_count, size_t rhs_count) {
204+ return lhs_count == rhs_count && (lhs == rhs || std::memcmp (lhs, rhs, lhs_count) == 0 );
205+ }
179206
180207 private:
181208 friend class String ;
@@ -311,7 +338,18 @@ class String : public ObjectRef {
311338 * before other, positive otherwise.
312339 */
313340 int compare (const char * other) const {
314- return Bytes::memncmp (data (), other, size (), std::strlen (other));
341+ const char * this_data = data ();
342+ size_t this_size = size ();
343+ for (size_t i = 0 ; i < this_size; ++i) {
344+ // other is shorter than this
345+ if (other[i] == ' \0 ' ) return 1 ;
346+ if (this_data[i] < other[i]) return -1 ;
347+ if (this_data[i] > other[i]) return 1 ;
348+ }
349+ // other equals this
350+ if (other[this_size] == ' \0 ' ) return 0 ;
351+ // other longer than this
352+ return -1 ;
315353 }
316354
317355 /* !
@@ -616,11 +654,17 @@ inline bool operator>=(const String& lhs, const char* rhs) { return lhs.compare(
616654inline bool operator >=(const char * lhs, const String& rhs) { return rhs.compare (lhs) <= 0 ; }
617655
618656// Overload == operator
619- inline bool operator ==(const String& lhs, const std::string& rhs) { return lhs.compare (rhs) == 0 ; }
657+ inline bool operator ==(const String& lhs, const std::string& rhs) {
658+ return Bytes::memequal (lhs.data (), rhs.data (), lhs.size (), rhs.size ());
659+ }
620660
621- inline bool operator ==(const std::string& lhs, const String& rhs) { return rhs.compare (lhs) == 0 ; }
661+ inline bool operator ==(const std::string& lhs, const String& rhs) {
662+ return Bytes::memequal (lhs.data (), rhs.data (), lhs.size (), rhs.size ());
663+ }
622664
623- inline bool operator ==(const String& lhs, const String& rhs) { return lhs.compare (rhs) == 0 ; }
665+ inline bool operator ==(const String& lhs, const String& rhs) {
666+ return Bytes::memequal (lhs.data (), rhs.data (), lhs.size (), rhs.size ());
667+ }
624668
625669inline bool operator ==(const String& lhs, const char * rhs) { return lhs.compare (rhs) == 0 ; }
626670
@@ -641,22 +685,6 @@ inline std::ostream& operator<<(std::ostream& out, const String& input) {
641685 out.write (input.data (), input.size ());
642686 return out;
643687}
644-
645- inline int Bytes::memncmp (const char * lhs, const char * rhs, size_t lhs_count, size_t rhs_count) {
646- if (lhs == rhs && lhs_count == rhs_count) return 0 ;
647-
648- for (size_t i = 0 ; i < lhs_count && i < rhs_count; ++i) {
649- if (lhs[i] < rhs[i]) return -1 ;
650- if (lhs[i] > rhs[i]) return 1 ;
651- }
652- if (lhs_count < rhs_count) {
653- return -1 ;
654- } else if (lhs_count > rhs_count) {
655- return 1 ;
656- } else {
657- return 0 ;
658- }
659- }
660688} // namespace ffi
661689
662690// Expose to the tvm namespace for usability
0 commit comments