Browse Source

core: extract str_has_{prefix,suffix}

Daniele Bartolini 6 years ago
parent
commit
30585e2c87
3 changed files with 31 additions and 17 deletions
  1. 8 14
      src/core/strings/dynamic_string.h
  2. 17 0
      src/core/strings/string.h
  3. 6 3
      src/core/unit_tests.cpp

+ 8 - 14
src/core/strings/dynamic_string.h

@@ -61,11 +61,11 @@ struct DynamicString
 	/// Removes leading and trailing white-space characters from the string.
 	void trim();
 
-	/// Returns whether the string starts with @a str.
-	bool has_prefix(const char* str) const;
+	/// Returns whether the string starts with @a prefix.
+	bool has_prefix(const char* prefix) const;
 
-	/// Returns wheterh the string ends with @a str.
-	bool has_suffix(const char* str) const;
+	/// Returns wheterh the string ends with @a suffix.
+	bool has_suffix(const char* suffix) const;
 
 	/// Returns the StringId32 of the string.
 	StringId32 to_string_id() const;
@@ -194,20 +194,14 @@ inline void DynamicString::trim()
 	rtrim();
 }
 
-inline bool DynamicString::has_prefix(const char* str) const
+inline bool DynamicString::has_prefix(const char* prefix) const
 {
-	CE_ENSURE(NULL != str);
-	const u32 ml = strlen32(c_str());
-	const u32 sl = strlen32(str);
-	return sl <= ml && strncmp(&_data[0], str, sl) == 0;
+	return str_has_prefix(c_str(), prefix);
 }
 
-inline bool DynamicString::has_suffix(const char* str) const
+inline bool DynamicString::has_suffix(const char* suffix) const
 {
-	CE_ENSURE(NULL != str);
-	const u32 ml = strlen32(c_str());
-	const u32 sl = strlen32(str);
-	return sl <= ml && strncmp(&_data[ml-sl], str, sl) == 0;
+	return str_has_suffix(c_str(), suffix);
 }
 
 inline StringId32 DynamicString::to_string_id() const

+ 17 - 0
src/core/strings/string.h

@@ -5,6 +5,7 @@
 
 #pragma once
 
+#include "core/error/error.h"
 #include "core/platform.h"
 #include "core/types.h"
 #include <ctype.h> // isspace
@@ -110,4 +111,20 @@ inline int wildcmp(const char *wild, const char *str)
 	return !*wild;
 }
 
+inline bool str_has_prefix(const char* str, const char* prefix)
+{
+	CE_ENSURE(NULL != str);
+	CE_ENSURE(NULL != prefix);
+	return strncmp(&str[0], prefix, strlen32(prefix)) == 0;
+}
+
+inline bool str_has_suffix(const char* str, const char* suffix)
+{
+	CE_ENSURE(NULL != str);
+	CE_ENSURE(NULL != suffix);
+	const u32 ml = strlen32(str);
+	const u32 sl = strlen32(suffix);
+	return sl <= ml && strncmp(&str[ml-sl], suffix, sl) == 0;
+}
+
 } // namespace crown

+ 6 - 3
src/core/unit_tests.cpp

@@ -957,10 +957,13 @@ static void test_dynamic_string()
 		TempAllocator1024 ta;
 		DynamicString str(ta);
 		str.set("Hello everyone!", 15);
-		ENSURE(str.has_prefix("Hello"));
-		ENSURE(!str.has_prefix("hello"));
-		ENSURE(str.has_suffix("one!"));
+
+		ENSURE( str.has_prefix("Hello"));
+		ENSURE(!str.has_prefix("Helloo"));
+
+		ENSURE( str.has_suffix("one!"));
 		ENSURE(!str.has_suffix("one"));
+
 		ENSURE(!str.has_prefix("Hello everyone!!!"));
 		ENSURE(!str.has_suffix("Hello everyone!!!"));
 	}