Browse Source

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.
legacy
Sam Hocevar sam 12 years ago
parent
commit
949128d325
6 changed files with 78 additions and 4 deletions
  1. +1
    -1
      src/Makefile.am
  2. +51
    -0
      src/core/string.cpp
  3. +10
    -2
      src/lol/core/string.h
  4. +1
    -0
      src/lolcore.vcxproj
  5. +7
    -1
      src/lolcore.vcxproj.filters
  6. +8
    -0
      test/unit/string.cpp

+ 1
- 1
src/Makefile.am View File

@@ -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 \
\


+ 51
- 0
src/core/string.cpp View File

@@ -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 */


+ 10
- 2
src/lol/core/string.h View File

@@ -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 */


+ 1
- 0
src/lolcore.vcxproj View File

@@ -235,6 +235,7 @@
<ClCompile Include="bullet\LinearMath\btSerializer.cpp" />
<ClCompile Include="camera.cpp" />
<ClCompile Include="core\hash.cpp" />
<ClCompile Include="core\string.cpp" />
<ClCompile Include="debug\fps.cpp" />
<ClCompile Include="debug\record.cpp" />
<ClCompile Include="debug\stats.cpp" />


+ 7
- 1
src/lolcore.vcxproj.filters View File

@@ -11,6 +11,9 @@
<Filter Include="src\debug">
<UniqueIdentifier>{e056731c-5484-434a-965e-801c199c0366}</UniqueIdentifier>
</Filter>
<Filter Include="src\core">
<UniqueIdentifier>{a63b1fd6-3747-4a9f-9bd7-3f942133ad6b}</UniqueIdentifier>
</Filter>
<Filter Include="src\platform">
<UniqueIdentifier>{a11c55f8-8e10-4270-be24-38e8d4fcf589}</UniqueIdentifier>
</Filter>
@@ -179,7 +182,10 @@
<Filter>src\...</Filter>
</ClCompile>
<ClCompile Include="hash.cpp">
<Filter>src\...</Filter>
<Filter>src\core</Filter>
</ClCompile>
<ClCompile Include="string.cpp">
<Filter>src\core</Filter>
</ClCompile>
<ClCompile Include="layer.cpp">
<Filter>src\...</Filter>


+ 8
- 0
test/unit/string.cpp View File

@@ -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 */


Loading…
Cancel
Save