I'm gonna commit this right now though I'm not really sure whether Visual Studio will agree to build va_copy and others.legacy
@@ -40,7 +40,7 @@ liblol_a_SOURCES = \ | |||||
$(android_sources) \ | $(android_sources) \ | ||||
$(bullet_sources) \ | $(bullet_sources) \ | ||||
\ | \ | ||||
core/hash.cpp \ | |||||
core/hash.cpp core/string.cpp \ | |||||
\ | \ | ||||
thread/threadbase.h thread/thread.h \ | thread/threadbase.h thread/thread.h \ | ||||
\ | \ | ||||
@@ -0,0 +1,51 @@ | |||||
// | |||||
// Lol Engine | |||||
// | |||||
// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> | |||||
// 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 <cstdio> | |||||
#ifdef WIN32 | |||||
# define WIN32_LEAN_AND_MEAN | |||||
# include <windows.h> | |||||
#endif | |||||
#include <cstdarg> | |||||
#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 */ | |||||
@@ -70,7 +70,7 @@ public: | |||||
return ret += s; | 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! */ | /* Be careful, we have a trailing zero we don't want! */ | ||||
--m_count; | --m_count; | ||||
@@ -84,7 +84,7 @@ public: | |||||
return ret += c; | return ret += c; | ||||
} | } | ||||
inline String operator +=(char c) | |||||
inline String& operator +=(char c) | |||||
{ | { | ||||
((Super &)*this).Last() = c; | ((Super &)*this).Last() = c; | ||||
((Super &)*this).Push('\0'); | ((Super &)*this).Push('\0'); | ||||
@@ -107,6 +107,14 @@ public: | |||||
{ | { | ||||
return !(*this == s); | 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 */ | } /* namespace lol */ | ||||
@@ -235,6 +235,7 @@ | |||||
<ClCompile Include="bullet\LinearMath\btSerializer.cpp" /> | <ClCompile Include="bullet\LinearMath\btSerializer.cpp" /> | ||||
<ClCompile Include="camera.cpp" /> | <ClCompile Include="camera.cpp" /> | ||||
<ClCompile Include="core\hash.cpp" /> | <ClCompile Include="core\hash.cpp" /> | ||||
<ClCompile Include="core\string.cpp" /> | |||||
<ClCompile Include="debug\fps.cpp" /> | <ClCompile Include="debug\fps.cpp" /> | ||||
<ClCompile Include="debug\record.cpp" /> | <ClCompile Include="debug\record.cpp" /> | ||||
<ClCompile Include="debug\stats.cpp" /> | <ClCompile Include="debug\stats.cpp" /> | ||||
@@ -11,6 +11,9 @@ | |||||
<Filter Include="src\debug"> | <Filter Include="src\debug"> | ||||
<UniqueIdentifier>{e056731c-5484-434a-965e-801c199c0366}</UniqueIdentifier> | <UniqueIdentifier>{e056731c-5484-434a-965e-801c199c0366}</UniqueIdentifier> | ||||
</Filter> | </Filter> | ||||
<Filter Include="src\core"> | |||||
<UniqueIdentifier>{a63b1fd6-3747-4a9f-9bd7-3f942133ad6b}</UniqueIdentifier> | |||||
</Filter> | |||||
<Filter Include="src\platform"> | <Filter Include="src\platform"> | ||||
<UniqueIdentifier>{a11c55f8-8e10-4270-be24-38e8d4fcf589}</UniqueIdentifier> | <UniqueIdentifier>{a11c55f8-8e10-4270-be24-38e8d4fcf589}</UniqueIdentifier> | ||||
</Filter> | </Filter> | ||||
@@ -179,7 +182,10 @@ | |||||
<Filter>src\...</Filter> | <Filter>src\...</Filter> | ||||
</ClCompile> | </ClCompile> | ||||
<ClCompile Include="hash.cpp"> | <ClCompile Include="hash.cpp"> | ||||
<Filter>src\...</Filter> | |||||
<Filter>src\core</Filter> | |||||
</ClCompile> | |||||
<ClCompile Include="string.cpp"> | |||||
<Filter>src\core</Filter> | |||||
</ClCompile> | </ClCompile> | ||||
<ClCompile Include="layer.cpp"> | <ClCompile Include="layer.cpp"> | ||||
<Filter>src\...</Filter> | <Filter>src\...</Filter> | ||||
@@ -120,6 +120,14 @@ LOLUNIT_FIXTURE(StringTest) | |||||
LOLUNIT_ASSERT(s1 != s2); | LOLUNIT_ASSERT(s1 != s2); | ||||
LOLUNIT_ASSERT(!(s1 != s3)); | LOLUNIT_ASSERT(!(s1 != s3)); | ||||
} | } | ||||
LOLUNIT_TEST(StringPrintf) | |||||
{ | |||||
String s1 = "3a"; | |||||
String s2 = String::Printf("%d%x", 3, 10); | |||||
LOLUNIT_ASSERT(s1 == s2); | |||||
} | |||||
}; | }; | ||||
} /* namespace lol */ | } /* namespace lol */ | ||||