Daniele Bartolini пре 9 година
родитељ
комит
49209ec82b
2 измењених фајлова са 14 додато и 10 уклоњено
  1. 9 6
      src/core/strings/string_id.cpp
  2. 5 4
      src/core/strings/string_id.h

+ 9 - 6
src/core/strings/string_id.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/taylor001/crown/blob/master/LICENSE
  */
 
+#include "dynamic_string.h"
 #include "error.h"
 #include "murmur.h"
 #include "string_id.h"
@@ -27,10 +28,11 @@ void StringId32::hash(const char* str, u32 len)
 	_id = murmur32(str, len, 0);
 }
 
-const char* StringId32::to_string(char* buf)
+void StringId32::to_string(DynamicString& s)
 {
-	snprintf(buf, STRING_LENGTH, "%.8x", _id);
-	return buf;
+	char buf[8+1];
+	snprintf(buf, sizeof(buf), "%.8x", _id);
+	s.set(buf, sizeof(buf)-1);
 }
 
 StringId64::StringId64(const char* str)
@@ -49,10 +51,11 @@ void StringId64::hash(const char* str, u32 len)
 	_id = murmur64(str, len, 0);
 }
 
-const char* StringId64::to_string(char* buf)
+void StringId64::to_string(DynamicString& s)
 {
-	snprintf(buf, STRING_LENGTH, "%.16" PRIx64, _id);
-	return buf;
+	char buf[16+1];
+	snprintf(buf, sizeof(buf), "%.16" PRIx64, _id);
+	s.set(buf, sizeof(buf)-1);
 }
 
 } // namespace crown

+ 5 - 4
src/core/strings/string_id.h

@@ -5,6 +5,7 @@
 
 #pragma once
 
+#include "string_types.h"
 #include "types.h"
 
 namespace crown
@@ -22,9 +23,9 @@ struct StringId32
 	explicit StringId32(const char* str, u32 len);
 
 	void hash(const char* str, u32 len);
-	const char* to_string(char* buf);
 
-	static const u32 STRING_LENGTH = 32;
+	/// Fills @a s with the string representation of the id.
+	void to_string(DynamicString& s);
 };
 
 /// Hashed string.
@@ -40,9 +41,9 @@ struct StringId64
 	explicit StringId64(const char* str, u32 len);
 
 	void hash(const char* str, u32 len);
-	const char* to_string(char* buf);
 
-	static const u32 STRING_LENGTH = 32;
+	/// Fills @a s with the string representation of the id.
+	void to_string(DynamicString& s);
 };
 
 /// @addtogroup String