Browse Source

Avoid string allocations in AlphCompare

Bernhard Liebl 7 years ago
parent
commit
b47ce5e5d6
3 changed files with 42 additions and 59 deletions
  1. 16 1
      core/string_db.h
  2. 3 58
      core/ustring.cpp
  3. 23 0
      core/ustring.h

+ 16 - 1
core/string_db.h

@@ -138,7 +138,22 @@ public:
 
 		_FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
 
-			return l.operator String() < r.operator String();
+			const char *l_cname = l._data ? l._data->cname : "";
+			const char *r_cname = r._data ? r._data->cname : "";
+
+			if (l_cname) {
+
+				if (r_cname)
+					return is_str_less(l_cname, r_cname);
+				else
+					return is_str_less(l_cname, r._data->name.ptr());
+			} else {
+
+				if (r_cname)
+					return is_str_less(l._data->name.ptr(), r_cname);
+				else
+					return is_str_less(l._data->name.ptr(), r._data->name.ptr());
+			}
 		}
 	};
 

+ 3 - 58
core/ustring.cpp

@@ -64,26 +64,7 @@ bool CharString::operator<(const CharString &p_right) const {
 		return p_right.length() != 0;
 	}
 
-	const char *this_str = get_data();
-	const char *that_str = p_right.get_data();
-	while (true) {
-
-		if (*that_str == 0 && *this_str == 0)
-			return false; //this can't be equal, sadly
-		else if (*this_str == 0)
-			return true; //if this is empty, and the other one is not, then we're less.. I think?
-		else if (*that_str == 0)
-			return false; //otherwise the other one is smaller..
-		else if (*this_str < *that_str) //more than
-			return true;
-		else if (*this_str > *that_str) //less than
-			return false;
-
-		this_str++;
-		that_str++;
-	}
-
-	return false; //should never reach here anyway
+	return is_str_less(get_data(), p_right.get_data());
 }
 
 const char *CharString::get_data() const {
@@ -372,25 +353,7 @@ bool String::operator<(const CharType *p_str) const {
 	if (empty())
 		return true;
 
-	const CharType *this_str = c_str();
-	while (true) {
-
-		if (*p_str == 0 && *this_str == 0)
-			return false; //this can't be equal, sadly
-		else if (*this_str == 0)
-			return true; //if this is empty, and the other one is not, then we're less.. I think?
-		else if (*p_str == 0)
-			return false; //otherwise the other one is smaller..
-		else if (*this_str < *p_str) //more than
-			return true;
-		else if (*this_str > *p_str) //less than
-			return false;
-
-		this_str++;
-		p_str++;
-	}
-
-	return false; //should never reach here anyway
+	return is_str_less(c_str(), p_str);
 }
 
 bool String::operator<=(String p_str) const {
@@ -405,25 +368,7 @@ bool String::operator<(const char *p_str) const {
 	if (empty())
 		return true;
 
-	const CharType *this_str = c_str();
-	while (true) {
-
-		if (*p_str == 0 && *this_str == 0)
-			return false; //this can't be equal, sadly
-		else if (*this_str == 0)
-			return true; //if this is empty, and the other one is not, then we're less.. I think?
-		else if (*p_str == 0)
-			return false; //otherwise the other one is smaller..
-		else if (*this_str < *p_str) //more than
-			return true;
-		else if (*this_str > *p_str) //less than
-			return false;
-
-		this_str++;
-		p_str++;
-	}
-
-	return false; //should never reach here anyway
+	return is_str_less(c_str(), p_str);
 }
 
 bool String::operator<(String p_str) const {

+ 23 - 0
core/ustring.h

@@ -272,6 +272,29 @@ struct NaturalNoCaseComparator {
 	}
 };
 
+template <typename L, typename R>
+_FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
+
+	while (true) {
+
+		if (*l_ptr == 0 && *r_ptr == 0)
+			return false;
+		else if (*l_ptr == 0)
+			return true;
+		else if (*r_ptr == 0)
+			return false;
+		else if (*l_ptr < *r_ptr)
+			return true;
+		else if (*l_ptr > *r_ptr)
+			return false;
+
+		l_ptr++;
+		r_ptr++;
+	}
+
+	CRASH_COND(true); // unreachable
+}
+
 /* end of namespace */
 
 //tool translate