Browse Source

Remove non-standard string comparison, and unnecessary template.

Michael Ragazzon 5 years ago
parent
commit
6d5975da52

+ 3 - 16
Include/RmlUi/Core/StringUtilities.h

@@ -29,8 +29,6 @@
 #ifndef RMLUICORESTRINGUTILITIES_H
 #ifndef RMLUICORESTRINGUTILITIES_H
 #define RMLUICORESTRINGUTILITIES_H
 #define RMLUICORESTRINGUTILITIES_H
 
 
-#include <algorithm>
-#include <stddef.h>
 #include "Header.h"
 #include "Header.h"
 #include "Types.h"
 #include "Types.h"
 
 
@@ -42,12 +40,6 @@ namespace Core {
 	@author Lloyd Weehuizen
 	@author Lloyd Weehuizen
  */
  */
 
 
-// Redefine Windows APIs as their STDC counterparts.
-#ifdef _MSC_VER
-	#define strcasecmp stricmp
-	#define strncasecmp strnicmp
-#endif
-
 class StringView;
 class StringView;
 
 
 /// Construct a string using sprintf-style syntax.
 /// Construct a string using sprintf-style syntax.
@@ -91,8 +83,7 @@ namespace StringUtilities
 	RMLUICORE_API String Replace(String subject, char search, char replace);
 	RMLUICORE_API String Replace(String subject, char search, char replace);
 
 
 	/// Checks if a given value is a whitespace character.
 	/// Checks if a given value is a whitespace character.
-	template < typename CharacterType >
-	inline bool IsWhitespace(CharacterType x)
+	inline bool IsWhitespace(const char x)
 	{
 	{
 		return (x == '\r' || x == '\n' || x == ' ' || x == '\t');
 		return (x == '\r' || x == '\n' || x == ' ' || x == '\t');
 	}
 	}
@@ -103,11 +94,8 @@ namespace StringUtilities
 	/// Strip whitespace characters from the beginning and end of a string.
 	/// Strip whitespace characters from the beginning and end of a string.
 	RMLUICORE_API String StripWhitespace(StringView string);
 	RMLUICORE_API String StripWhitespace(StringView string);
 
 
-	/// Operator for STL containers using strings.
-	struct RMLUICORE_API StringComparei
-	{
-		bool operator()(const String& lhs, const String& rhs) const;
-	};
+	/// Case insensitive string comparison. Returns true if they compare equal.
+	RMLUICORE_API bool StringCompareCaseInsensitive(StringView lhs, StringView rhs);
 
 
 	// Decode the first code point in a zero-terminated UTF-8 string.
 	// Decode the first code point in a zero-terminated UTF-8 string.
 	RMLUICORE_API Character ToCharacter(const char* p);
 	RMLUICORE_API Character ToCharacter(const char* p);
@@ -136,7 +124,6 @@ namespace StringUtilities
 		return p;
 		return p;
 	}
 	}
 
 
-
 	/// Converts a string in UTF-8 encoding to a u16string in UTF-16 encoding.
 	/// Converts a string in UTF-8 encoding to a u16string in UTF-16 encoding.
 	/// Reports a warning if some or all characters could not be converted.
 	/// Reports a warning if some or all characters could not be converted.
 	RMLUICORE_API U16String ToUTF16(const String& str);
 	RMLUICORE_API U16String ToUTF16(const String& str);

+ 2 - 2
Source/Core/PropertyParserNumber.cpp

@@ -106,8 +106,8 @@ bool PropertyParserNumber::ParseValue(Property& property, const String& value, c
 		if (value.size() < unit_suffix.second.size())
 		if (value.size() < unit_suffix.second.size())
 			continue;
 			continue;
 
 
-		size_t test_unit_pos = value.size() - unit_suffix.second.size();
-		if (strcasecmp(value.c_str() + test_unit_pos, unit_suffix.second.c_str()) == 0)
+		const size_t test_unit_pos = value.size() - unit_suffix.second.size();
+		if (StringUtilities::StringCompareCaseInsensitive(StringView(value, test_unit_pos), StringView(unit_suffix.second)))
 		{
 		{
 			unit_pos = test_unit_pos;
 			unit_pos = test_unit_pos;
 			property.unit = unit_suffix.first;
 			property.unit = unit_suffix.first;

+ 23 - 10
Source/Core/StringUtilities.cpp

@@ -28,8 +28,10 @@
 
 
 #include "../../Include/RmlUi/Core/StringUtilities.h"
 #include "../../Include/RmlUi/Core/StringUtilities.h"
 #include "../../Include/RmlUi/Core/Log.h"
 #include "../../Include/RmlUi/Core/Log.h"
+#include <algorithm>
 #include <stdio.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdarg.h>
+#include <string.h>
 
 
 namespace Rml {
 namespace Rml {
 namespace Core {
 namespace Core {
@@ -80,15 +82,15 @@ String CreateString(size_t max_size, const char* format, ...)
 	return result;
 	return result;
 }
 }
 
 
+static inline char CharToLower(char c) {
+	if (c >= 'A' && c <= 'Z')
+		c += char('a' - 'A');
+	return c;
+}
 
 
 String StringUtilities::ToLower(const String& string) {
 String StringUtilities::ToLower(const String& string) {
 	String str_lower = string;
 	String str_lower = string;
-	std::transform(str_lower.begin(), str_lower.end(), str_lower.begin(), [](char c) {
-		if (c >= 'A' && c <= 'Z')
-			c += char( 'a' - 'A' );
-		return c;
-		}
-	);
+	std::transform(str_lower.begin(), str_lower.end(), str_lower.begin(), &CharToLower);
 	return str_lower;
 	return str_lower;
 }
 }
 
 
@@ -268,12 +270,23 @@ RMLUICORE_API String StringUtilities::StripWhitespace(StringView string)
 	return String();
 	return String();
 }
 }
 
 
-// Operators for STL containers using strings.
-bool StringUtilities::StringComparei::operator()(const String& lhs, const String& rhs) const
+bool StringUtilities::StringCompareCaseInsensitive(const StringView lhs, const StringView rhs)
 {
 {
-	return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
-}
+	if (lhs.size() != rhs.size())
+		return false;
+
+	const char* left = lhs.begin();
+	const char* right = rhs.begin();
+	const char* const left_end = lhs.end();
 
 
+	for (; left != left_end; ++left, ++right)
+	{
+		if (CharToLower(*left) != CharToLower(*right))
+			return false;
+	}
+
+	return true;
+}
 
 
 Character StringUtilities::ToCharacter(const char* p)
 Character StringUtilities::ToCharacter(const char* p)
 {
 {

+ 7 - 4
Source/Core/XMLParseTools.cpp

@@ -28,10 +28,13 @@
 
 
 #include "XMLParseTools.h"
 #include "XMLParseTools.h"
 #include "../../Include/RmlUi/Core/StreamMemory.h"
 #include "../../Include/RmlUi/Core/StreamMemory.h"
-#include "Template.h"
+#include "../../Include/RmlUi/Core/ElementDocument.h"
+#include "../../Include/RmlUi/Core/StringUtilities.h"
+#include "../../Include/RmlUi/Core/Types.h"
 #include "TemplateCache.h"
 #include "TemplateCache.h"
-#include "../../Include/RmlUi/Core.h"
+#include "Template.h"
 #include <ctype.h>
 #include <ctype.h>
+#include <string.h>
 
 
 namespace Rml {
 namespace Rml {
 namespace Core {
 namespace Core {
@@ -40,7 +43,7 @@ namespace Core {
 // NOTE: tag *MUST* be in lowercase
 // NOTE: tag *MUST* be in lowercase
 const char* XMLParseTools::FindTag(const char* tag, const char* string, bool closing_tag)
 const char* XMLParseTools::FindTag(const char* tag, const char* string, bool closing_tag)
 {
 {
-	int length = (int)strlen(tag);
+	const size_t length = strlen(tag);
 	const char* ptr = string;
 	const char* ptr = string;
 	bool found_closing = false;
 	bool found_closing = false;
 
 
@@ -50,7 +53,7 @@ const char* XMLParseTools::FindTag(const char* tag, const char* string, bool clo
 		if (tolower((*ptr)) == tag[0])
 		if (tolower((*ptr)) == tag[0])
 		{
 		{
 			// If it does, check the whole word
 			// If it does, check the whole word
-			if (strncasecmp(ptr,tag,length) == 0)
+			if (StringUtilities::StringCompareCaseInsensitive(StringView(ptr, ptr + length), StringView(tag, tag + length)))
 			{
 			{
 				// Check for opening <, loop back in the string skipping white space and forward slashes if
 				// Check for opening <, loop back in the string skipping white space and forward slashes if
 				// we're looking for the closing tag
 				// we're looking for the closing tag