Procházet zdrojové kódy

Added STL string to bx::StringView conversion.

Branimir Karadžić před 7 roky
rodič
revize
9987d5cc5a
3 změnil soubory, kde provedl 33 přidání a 6 odebrání
  1. 17 6
      include/bx/inline/string.inl
  2. 8 0
      include/bx/string.h
  3. 8 0
      tests/string_test.cpp

+ 17 - 6
include/bx/inline/string.inl

@@ -19,14 +19,13 @@ namespace bx
 		char temp[2048];
 
 		char* out = temp;
-		int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
-		if ( (int32_t)sizeof(temp) < len)
+		int32_t len = vsnprintf(out, sizeof(temp), _format, _argList);
+		if (int32_t(sizeof(temp) ) < len)
 		{
-			out = (char*)alloca(len+1);
-			len = bx::vsnprintf(out, len, _format, _argList);
+			out = (char*)alloca(len);
+			len = vsnprintf(out, len, _format, _argList);
 		}
-		out[len] = '\0';
-		_out.append(out);
+		_out.append(out, out+len);
 	}
 
 	template <typename Ty>
@@ -86,6 +85,12 @@ namespace bx
 		set(_ptr, _term);
 	}
 
+	template<typename Ty>
+	inline StringView::StringView(const Ty& _container)
+	{
+		set(_container);
+	}
+
 	inline void StringView::set(const char* _ptr, int32_t _len)
 	{
 		clear();
@@ -106,6 +111,12 @@ namespace bx
 		set(_ptr, int32_t(_term-_ptr) );
 	}
 
+	template<typename Ty>
+	inline void StringView::set(const Ty& _container)
+	{
+		set(_container.data(), _container.length() );
+	}
+
 	inline void StringView::set(const StringView& _str)
 	{
 		set(_str.m_ptr, _str.m_len);

+ 8 - 0
include/bx/string.h

@@ -41,6 +41,10 @@ namespace bx
 		///
 		StringView(const char* _ptr, const char* _term);
 
+		///
+		template<typename Ty>
+		explicit StringView(const Ty& _container);
+
 		///
 		void set(const char* _ptr, int32_t _len = INT32_MAX);
 
@@ -50,6 +54,10 @@ namespace bx
 		///
 		void set(const StringView& _str);
 
+		///
+		template<typename Ty>
+		void set(const Ty& _container);
+
 		///
 		void clear();
 

+ 8 - 0
tests/string_test.cpp

@@ -8,9 +8,17 @@
 #include <bx/string.h>
 #include <bx/handlealloc.h>
 #include <bx/sort.h>
+#include <string>
 
 bx::AllocatorI* g_allocator;
 
+TEST_CASE("stringPrintfTy", "")
+{
+	std::string test;
+	bx::stringPrintf(test, "printf into std::string.");
+	REQUIRE(0 == bx::strCmp(bx::StringView(test), "printf into std::string.") );
+}
+
 TEST_CASE("chars", "")
 {
 	for (char ch = 'A'; ch <= 'Z'; ++ch)