Browse Source

StripWhitespace from string view

(cherry picked from commit 1d21206198ea3ea32d225ff8f33e842fd95bdc48)
Michael Ragazzon 6 years ago
parent
commit
e17b866e84
2 changed files with 15 additions and 4 deletions
  1. 7 0
      Include/RmlUi/Core/StringUtilities.h
  2. 8 4
      Source/Core/StringUtilities.cpp

+ 7 - 0
Include/RmlUi/Core/StringUtilities.h

@@ -100,6 +100,9 @@ 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(const String& string);
 	RMLUICORE_API String StripWhitespace(const String& string);
 
 
+	/// Strip whitespace characters from the beginning and end of a string.
+	RMLUICORE_API String StripWhitespace(StringView string);
+
 	/// Operator for STL containers using strings.
 	/// Operator for STL containers using strings.
 	struct RMLUICORE_API StringComparei
 	struct RMLUICORE_API StringComparei
 	{
 	{
@@ -167,6 +170,10 @@ public:
 
 
 	inline size_t size() const { return p_end - p_begin; }
 	inline size_t size() const { return p_end - p_begin; }
 
 
+	explicit inline operator String() const {
+		return String(p_begin, p_end);
+	}
+
 private:
 private:
 	const char* p_begin;
 	const char* p_begin;
 	const char* p_end;
 	const char* p_end;

+ 8 - 4
Source/Core/StringUtilities.cpp

@@ -246,16 +246,20 @@ void StringUtilities::JoinString(String& string, const StringList& string_list,
 	}
 	}
 }
 }
 
 
-// Strip whitespace characters from the beginning and end of a string.
 String StringUtilities::StripWhitespace(const String& string)
 String StringUtilities::StripWhitespace(const String& string)
 {
 {
-	const char* start = string.c_str();
-	const char* end = start + string.size();
+	return StripWhitespace(StringView(string));
+}
+
+RMLUICORE_API String StringUtilities::StripWhitespace(StringView string)
+{
+	const char* start = string.begin();
+	const char* end = string.end();
 
 
 	while (start < end && IsWhitespace(*start))
 	while (start < end && IsWhitespace(*start))
 		start++;
 		start++;
 
 
-	while (end > start && IsWhitespace(*(end - 1)))
+	while (end > start&& IsWhitespace(*(end - 1)))
 		end--;
 		end--;
 
 
 	if (start < end)
 	if (start < end)