From 949128d32596dc6ea6ac7d2d91b992f988719cf2 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 21 Nov 2012 00:34:09 +0000 Subject: [PATCH] core: implement String::Printf() and start working on the unit tests. I'm gonna commit this right now though I'm not really sure whether Visual Studio will agree to build va_copy and others. --- src/Makefile.am | 2 +- src/core/string.cpp | 51 +++++++++++++++++++++++++++++++++++++ src/lol/core/string.h | 12 +++++++-- src/lolcore.vcxproj | 1 + src/lolcore.vcxproj.filters | 8 +++++- test/unit/string.cpp | 8 ++++++ 6 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/core/string.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 81313019..ca75df4f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -40,7 +40,7 @@ liblol_a_SOURCES = \ $(android_sources) \ $(bullet_sources) \ \ - core/hash.cpp \ + core/hash.cpp core/string.cpp \ \ thread/threadbase.h thread/thread.h \ \ diff --git a/src/core/string.cpp b/src/core/string.cpp new file mode 100644 index 00000000..0a5f654b --- /dev/null +++ b/src/core/string.cpp @@ -0,0 +1,51 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#ifdef WIN32 +# define WIN32_LEAN_AND_MEAN +# include +#endif + +#include + +#include "core.h" + +namespace lol +{ + +String String::Printf(char const *format, ...) +{ + String ret; + + va_list ap, aq; + va_start(ap, format); + va_copy(aq, ap); + + /* vsnprintf() tells us how many character we need, and we need to + * add one for the terminating null byte. */ + size_t needed = vsnprintf(NULL, 0, format, aq) + 1; + ((Super &)ret).Reserve(needed); + ret.m_count = needed; + vsnprintf(&ret[0], needed, format, ap); + + va_end(aq); + va_end(ap); + + return ret; +} + +} /* namespace lol */ + diff --git a/src/lol/core/string.h b/src/lol/core/string.h index 1cf7a3e8..098fc2aa 100644 --- a/src/lol/core/string.h +++ b/src/lol/core/string.h @@ -70,7 +70,7 @@ public: return ret += s; } - inline String operator +=(String const &s) + inline String& operator +=(String const &s) { /* Be careful, we have a trailing zero we don't want! */ --m_count; @@ -84,7 +84,7 @@ public: return ret += c; } - inline String operator +=(char c) + inline String& operator +=(char c) { ((Super &)*this).Last() = c; ((Super &)*this).Push('\0'); @@ -107,6 +107,14 @@ public: { return !(*this == s); } + +#ifdef __GNUC__ +# define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p))) +#else +# define LOL_FMT_ATTR(n, p) +#endif + static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2); +#undef LOL_FMT_ATTR }; } /* namespace lol */ diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index bd98d8b0..e2d2fe6f 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -235,6 +235,7 @@ + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 394a43ab..a6bfa00d 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -11,6 +11,9 @@ {e056731c-5484-434a-965e-801c199c0366} + + {a63b1fd6-3747-4a9f-9bd7-3f942133ad6b} + {a11c55f8-8e10-4270-be24-38e8d4fcf589} @@ -179,7 +182,10 @@ src\... - src\... + src\core + + + src\core src\... diff --git a/test/unit/string.cpp b/test/unit/string.cpp index 532351bc..465f89de 100644 --- a/test/unit/string.cpp +++ b/test/unit/string.cpp @@ -120,6 +120,14 @@ LOLUNIT_FIXTURE(StringTest) LOLUNIT_ASSERT(s1 != s2); LOLUNIT_ASSERT(!(s1 != s3)); } + + LOLUNIT_TEST(StringPrintf) + { + String s1 = "3a"; + String s2 = String::Printf("%d%x", 3, 10); + + LOLUNIT_ASSERT(s1 == s2); + } }; } /* namespace lol */