Revision: 7058
http://winmerge.svn.sourceforge.net/winmerge/?rev=7058&view=rev
Author: kimmov
Date: 2009-12-26 23:11:18 +0000 (Sat, 26 Dec 2009)
Log Message:
-----------
Add printf()-style formatting function for String.
Code originally from Paul Senzee.
Modified Paths:
--------------
trunk/Docs/Users/Contributors.txt
trunk/Src/Common/UnicodeString.cpp
trunk/Src/Common/UnicodeString.h
Modified: trunk/Docs/Users/Contributors.txt
===================================================================
--- trunk/Docs/Users/Contributors.txt 2009-12-26 20:51:16 UTC (rev 7057)
+++ trunk/Docs/Users/Contributors.txt 2009-12-26 23:11:18 UTC (rev 7058)
@@ -189,6 +189,7 @@
* Cristi Posea (CSizingControlBar)
* Ferdinand Prantl (CrystalEditor syntax rules)
* Keith Rule (CMemDC - memory DC)
+* Paul Senzee (String formatting)
* Henry Spencer (CRegExp)
* Andrei Stcherbatchenko (CrystalEditor)
Modified: trunk/Src/Common/UnicodeString.cpp
===================================================================
--- trunk/Src/Common/UnicodeString.cpp 2009-12-26 20:51:16 UTC (rev 7057)
+++ trunk/Src/Common/UnicodeString.cpp 2009-12-26 23:11:18 UTC (rev 7058)
@@ -23,9 +23,15 @@
// ID line follows -- this is updated by SVN
// $Id$
+// String formatting code originally from Paul Senzee:
+// http://www.senzee5.com/2006/05/c-formatting-stdstring.html
+
#include <tchar.h>
+#include <stdarg.h>
#include "UnicodeString.h"
+static String format_arg_list(const TCHAR *fmt, va_list args);
+
/**
* @brief Convert a string to lower case string.
* @param [in] str String to convert to lower case.
@@ -148,3 +154,37 @@
result.erase(it + 1, result.end());
return result;
}
+
+static String format_arg_list(const TCHAR *fmt, va_list args)
+{
+ if (!fmt)
+ return _T("");
+ int result = -1;
+ int length = 256;
+ TCHAR *buffer = 0;
+ while (result == -1)
+ {
+ if (buffer)
+ delete [] buffer;
+ buffer = new TCHAR[length + 1];
+ memset(buffer, 0, (length + 1) * sizeof(TCHAR));
+ result = _vsntprintf(buffer, length, fmt, args);
+ length *= 2;
+ }
+ String s(buffer);
+ delete [] buffer;
+ return s;
+}
+
+/**
+ * @brief printf()-style formatting for STL string.
+ * Use this function to format String:s in printf() style.
+ */
+String string_format(const TCHAR *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ String s = format_arg_list(fmt, args);
+ va_end(args);
+ return s;
+}
Modified: trunk/Src/Common/UnicodeString.h
===================================================================
--- trunk/Src/Common/UnicodeString.h 2009-12-26 20:51:16 UTC (rev 7057)
+++ trunk/Src/Common/UnicodeString.h 2009-12-26 23:11:18 UTC (rev 7058)
@@ -49,4 +49,7 @@
String string_trim_ws_begin(const String & str);
String string_trim_ws_end(const String & str);
+// Formatting
+String string_format(const TCHAR *fmt, ...);
+
#endif // _UNICODE_STRING_
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|