Daniele Bartolini 10 лет назад
Родитель
Сommit
f3c92d796b
1 измененных файлов с 36 добавлено и 0 удалено
  1. 36 0
      src/core/strings/dynamic_string.h

+ 36 - 0
src/core/strings/dynamic_string.h

@@ -44,6 +44,15 @@ struct DynamicString
 	/// Returns whether the string is empty.
 	bool empty() const;
 
+	/// Removes leading white-space characters from the string.
+	void ltrim();
+
+	/// Removes trailing white-space characters from the string.
+	void rtrim();
+
+	/// Removes leading and trailing white-space characters from the string.
+	void trim();
+
 	/// Removes the leading string @a s.
 	/// @note
 	/// The string must start with @a s.
@@ -169,6 +178,33 @@ inline bool DynamicString::empty() const
 	return length() == 0;
 }
 
+inline void DynamicString::ltrim()
+{
+	const char* str = c_str();
+	const char* end = skip_spaces(str);
+
+	const u32 len = strlen32(end);
+
+	memmove(array::begin(_data), end, len);
+	array::resize(_data, len);
+}
+
+inline void DynamicString::rtrim()
+{
+	char* str = (char*)c_str();
+	char* end = str + strlen32(str) - 1;
+
+	while (end > str && isspace(*end)) --end;
+
+	*(end + 1) = '\0';
+}
+
+inline void DynamicString::trim()
+{
+	ltrim();
+	rtrim();
+}
+
 inline void DynamicString::strip_leading(const char* s)
 {
 	CE_ASSERT_NOT_NULL(s);