[Scummvm-cvs-logs] SF.net SVN: scummvm:[42743] scummvm/trunk
wjpalenstijn at users.sourceforge.net
wjpalenstijn at users.sourceforge.net
Sat Jul 25 12:25:57 CEST 2009
Revision: 42743
http://scummvm.svn.sourceforge.net/scummvm/?rev=42743&view=rev
Author: wjpalenstijn
Date: 2009-07-25 10:25:57 +0000 (Sat, 25 Jul 2009)
Log Message:
-----------
Add Common::String::printf to format a string
Modified Paths:
--------------
scummvm/trunk/common/str.cpp
scummvm/trunk/common/str.h
scummvm/trunk/test/common/str.h
Modified: scummvm/trunk/common/str.cpp
===================================================================
--- scummvm/trunk/common/str.cpp 2009-07-25 09:36:16 UTC (rev 42742)
+++ scummvm/trunk/common/str.cpp 2009-07-25 10:25:57 UTC (rev 42743)
@@ -28,6 +28,8 @@
#include "common/memorypool.h"
+#include <stdarg.h>
+
#if !defined(__SYMBIAN32__)
#include <new>
#endif
@@ -435,6 +437,34 @@
return hashit(c_str());
}
+// static
+String String::printf(const char *fmt, ...)
+{
+ String output;
+ assert(output.isStorageIntern());
+
+ va_list va;
+ va_start(va, fmt);
+ int len = vsnprintf(output._str, _builtinCapacity, fmt, va);
+ va_end(va);
+
+ if (len < (int)_builtinCapacity) {
+ // vsnprintf succeeded
+ output._size = len;
+ } else {
+ // vsnprintf didn't have enough space, so grow buffer
+ output.ensureCapacity(len, false);
+ va_start(va, fmt);
+ int len2 = vsnprintf(output._str, len+1, fmt, va);
+ va_end(va);
+ assert(len == len2);
+ output._size = len2;
+ }
+
+ return output;
+}
+
+
#pragma mark -
bool String::operator ==(const String &x) const {
Modified: scummvm/trunk/common/str.h
===================================================================
--- scummvm/trunk/common/str.h 2009-07-25 09:36:16 UTC (rev 42742)
+++ scummvm/trunk/common/str.h 2009-07-25 10:25:57 UTC (rev 42743)
@@ -218,6 +218,11 @@
uint hash() const;
+ /**
+ * Printf-like function. Returns a formatted String.
+ */
+ static Common::String printf(const char *fmt, ...) GCC_PRINTF(1,2);
+
public:
typedef char * iterator;
typedef const char * const_iterator;
Modified: scummvm/trunk/test/common/str.h
===================================================================
--- scummvm/trunk/test/common/str.h 2009-07-25 09:36:16 UTC (rev 42742)
+++ scummvm/trunk/test/common/str.h 2009-07-25 10:25:57 UTC (rev 42743)
@@ -285,4 +285,12 @@
TS_ASSERT(!Common::matchString("monkey.s99", "monkey.s*1"));
TS_ASSERT(Common::matchString("monkey.s101", "monkey.s*1"));
}
+
+ void test_string_printf() {
+ TS_ASSERT( Common::String::printf("") == "" );
+ TS_ASSERT( Common::String::printf("%s", "test") == "test" );
+ TS_ASSERT( Common::String::printf("%s.s%.02d", "monkey", 1) == "monkey.s01" );
+ TS_ASSERT( Common::String::printf("%s%X", "test", 1234) == "test4D2" );
+ TS_ASSERT( Common::String::printf("Some %s to make this string longer than the default built-in %s %d", "text", "capacity", 123456) == "Some text to make this string longer than the default built-in capacity 123456" );
+ }
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Scummvm-git-logs
mailing list