Bläddra i källkod

Add set() and empty() to DynamicString

Daniele Bartolini 10 år sedan
förälder
incheckning
a921f495b2
1 ändrade filer med 18 tillägg och 1 borttagningar
  1. 18 1
      src/core/strings/dynamic_string.h

+ 18 - 1
src/core/strings/dynamic_string.h

@@ -32,12 +32,18 @@ struct DynamicString
 	DynamicString& operator=(const char c);
 	DynamicString& operator=(const FixedString& s);
 
+	/// Sets the string to @a s.
+	void set(const char* s, u32 len);
+
 	/// Reserves space for at least @n characters.
 	void reserve(u32 n);
 
-	// Returns the length of the string.
+	/// Returns the length of the string.
 	u32 length() const;
 
+	/// Returns whether the string is empty.
+	bool empty() const;
+
 	/// Removes the leading string @a s.
 	/// @note
 	/// The string must start with @a s.
@@ -73,6 +79,12 @@ inline DynamicString::DynamicString(const char* s, Allocator& a)
 	array::push(_data, s, strlen32(s));
 }
 
+inline void DynamicString::set(const char* s, u32 len)
+{
+	array::resize(_data, len);
+	strncpy(array::begin(_data), s, len);
+}
+
 inline DynamicString& operator+=(DynamicString& a, const DynamicString& b)
 {
 	array::push(a._data, array::begin(b._data), array::size(b._data));
@@ -152,6 +164,11 @@ inline u32 DynamicString::length() const
 	return strlen32(this->c_str());
 }
 
+inline bool DynamicString::empty() const
+{
+	return length() == 0;
+}
+
 inline void DynamicString::strip_leading(const char* s)
 {
 	CE_ASSERT_NOT_NULL(s);