Branimir Karadžić 7 anos atrás
pai
commit
43f530b4ec
4 arquivos alterados com 27 adições e 9 exclusões
  1. 4 1
      include/bx/readerwriter.h
  2. 6 7
      src/string.cpp
  3. 0 1
      tests/string_test.cpp
  4. 17 0
      tests/vsnprintf_test.cpp

+ 4 - 1
include/bx/readerwriter.h

@@ -263,7 +263,10 @@ namespace bx
 	int32_t write(WriterI* _writer, const StringView& _str, Error* _err = NULL);
 	int32_t write(WriterI* _writer, const StringView& _str, Error* _err = NULL);
 
 
 	///
 	///
-	int32_t write(WriterI* _writer, Error* _err, const char* _format, ...);
+	int32_t write(WriterI* _writer, const StringView& _format, va_list _argList, Error* _err);
+
+	///
+	int32_t write(WriterI* _writer, Error* _err, const StringView& _format, ...);
 
 
 	/// Write repeat the same value.
 	/// Write repeat the same value.
 	int32_t writeRep(WriterI* _writer, uint8_t _byte, int32_t _size, Error* _err = NULL);
 	int32_t writeRep(WriterI* _writer, uint8_t _byte, int32_t _size, Error* _err = NULL);

+ 6 - 7
src/string.cpp

@@ -891,9 +891,9 @@ namespace bx
 		}
 		}
 	} // anonymous namespace
 	} // anonymous namespace
 
 
-	int32_t write(WriterI* _writer, const char* _format, va_list _argList, Error* _err)
+	int32_t write(WriterI* _writer, const StringView& _format, va_list _argList, Error* _err)
 	{
 	{
-		MemoryReader reader(_format, uint32_t(strLen(_format) ) );
+		MemoryReader reader(_format.getPtr(), _format.getLength() );
 
 
 		int32_t size = 0;
 		int32_t size = 0;
 
 
@@ -1103,15 +1103,13 @@ namespace bx
 			}
 			}
 		}
 		}
 
 
-		size += write(_writer, '\0', _err);
-
 		return size;
 		return size;
 	}
 	}
 
 
-	int32_t write(WriterI* _writer, Error* _err, const char* _format, ...)
+	int32_t write(WriterI* _writer, Error* _err, const StringView& _format, ...)
 	{
 	{
 		va_list argList;
 		va_list argList;
-		va_start(argList, _format);
+		va_start(argList, &_format);
 		int32_t total = write(_writer, _format, argList, _err);
 		int32_t total = write(_writer, _format, argList, _err);
 		va_end(argList);
 		va_end(argList);
 		return total;
 		return total;
@@ -1132,6 +1130,7 @@ namespace bx
 
 
 			if (err.isOk() )
 			if (err.isOk() )
 			{
 			{
+				size += write(&writer, '\0', &err);
 				return size - 1 /* size without '\0' terminator */;
 				return size - 1 /* size without '\0' terminator */;
 			}
 			}
 		}
 		}
@@ -1143,7 +1142,7 @@ namespace bx
 		int32_t size = write(&sizer, _format, argListCopy, &err);
 		int32_t size = write(&sizer, _format, argListCopy, &err);
 		va_end(argListCopy);
 		va_end(argListCopy);
 
 
-		return size - 1 /* size without '\0' terminator */;
+		return size;
 	}
 	}
 
 
 	int32_t vsnprintf(char* _out, int32_t _max, const char* _format, va_list _argList)
 	int32_t vsnprintf(char* _out, int32_t _max, const char* _format, va_list _argList)

+ 0 - 1
tests/string_test.cpp

@@ -464,6 +464,5 @@ TEST_CASE("strFindBlock", "")
 	const bx::StringView test1(test0, 1);
 	const bx::StringView test1(test0, 1);
 
 
 	bx::StringView result = bx::strFindBlock(test1, '{', '}');
 	bx::StringView result = bx::strFindBlock(test1, '{', '}');
-	printf("%.*s", result.getLength(), result.getPtr() );
 	REQUIRE(19 == result.getLength() );
 	REQUIRE(19 == result.getLength() );
 }
 }

+ 17 - 0
tests/vsnprintf_test.cpp

@@ -5,6 +5,8 @@
 
 
 #include "test.h"
 #include "test.h"
 #include <bx/string.h>
 #include <bx/string.h>
+#include <bx/readerwriter.h>
+
 #include <limits>
 #include <limits>
 #include <inttypes.h>
 #include <inttypes.h>
 
 
@@ -173,3 +175,18 @@ TEST_CASE("vsnprintf", "")
 		, world.getLength(), world.getPtr()
 		, world.getLength(), world.getPtr()
 		) );
 		) );
 }
 }
+
+TEST_CASE("vsnprintf write")
+{
+	char tmp[64];
+	bx::StaticMemoryBlock mb(tmp, sizeof(tmp));
+	bx::MemoryWriter writer(&mb);
+
+	bx::Error err;
+	int32_t len = bx::write(&writer, &err, "%d", 1389);
+	REQUIRE(err.isOk());
+	REQUIRE(len == 4);
+
+	bx::StringView str(tmp, len);
+	REQUIRE(0 == bx::strCmp(str, "1389"));
+}