这个是很久之前写的,去年总结了一下,将其单独提取出来,作为一个开源库放到了GitHub上,然而CPPFormat之类的名字都已经被抢注了,结果只好注册了一个这么蛋疼的名字:CPPFormatLibrary,以下简称FL。
首先介绍一下这是个什么东西。我们知道,在C++当中要进行格式化字符串,通常采用的是C库函数sprintf或者C++的stringstream,然而两者都有自己的问题,比如C库函数的类型安全问题,sprintf当参数不足,或者参数类型与格式化字符不符是都会发生错误,导致崩溃;而stringstream的效率又明显不行。除此之外,我么还知道boost库有format可以用,然而它效率不高。另外还有国外的大神写过fastformat库,地址:http://fastformat.sourceforge.net/ 。它的问题在于过于庞大,集成不方便,会引入太多你并不需要的东西;同时,它也并未将性能发挥到极限。我写这个FL库最初的灵感也来自于fastformat,也特别感谢它。
FL与fastformat一样,都致力于解决前面所描述的C++字符串格式化的各种问题,最终采用的方案是.net方案,关于.net的格式化字符串,可以看这篇文章:http://www.cnblogs.com/zyh-nhy/archive/2007/10/11/921240.html 与fastformat想比,FL移除了一些并没有什么用的功能,并进行了功能增强,比如fastformat并不支持{0,5}这样的更细的描述,而FL支持。当然fastformat与FL都没有完全覆盖.net的format,也就是说并不完全等同于.net的格式化,部分功能并未得到支持。
下面是FL中的测试代码,这个代码展示了最基本的格式化功能:
// Test
#include "Format.hpp" #include "Format/ProgressTimer.hpp" #define TEST_PERFORMANCE_IN_TOOLS 0 using namespace FormatLibrary; #include <iostream>
#include <vector>
using namespace std; void TestProfile()
{
const int TEST_COUNT = ; {
Profile::ProgressTimer Timer("FL"); for (int i = ; i < TEST_COUNT; ++i)
{
string str;
StandardLibrary::FormatTo(str, "{0}--#--{1,8}--#--{2}", , -40.2f, " String ");
StandardLibrary::FormatTo(str, "{0}--#--{1,8}--#--{1}", , -40.2f);
StandardLibrary::FormatTo(str, "{0}--#--{1,8}--#--{3}", , -40.2f, std::string("xxx"));
}
} #if !FL_COMPILER_MSVC
#define sprintf_s sprintf
#endif #if !TEST_PERFORMANCE_IN_TOOLS
{
Profile::ProgressTimer Timer("CL"); for (int i = ; i < TEST_COUNT; ++i)
{
string str;
char szBuf[];
sprintf_s(szBuf, "%d--#--%8.2f--#--%s", , -40.2f, " String ");
str = szBuf;
sprintf_s(szBuf, "%d--#--%8.2f--#--%f", , -40.2f, 0.0f);
str = szBuf;
sprintf_s(szBuf, "%d--#--%8.2f--#--%%f", , -40.2f);
str = szBuf;
}
}
#endif
} #if FL_PLATFORM_HAS_CPP11 && (FL_COMPILER_MSVC||FL_PLATFORM_MACOS)
#include <thread> void TestProfileMultiThread()
{
std::thread t0( TestProfile );
std::thread t1( TestProfile );
std::thread t2( TestProfile ); t0.join();
t1.join();
t2.join();
}
#endif int main()
{
StandardLibrary::STLGlobalPatternStorageA Storage;
Utility::TAutoString<char> TestStr; const char* pszTest = "{0},xxxd{1:d2}={2,3:d2} !! {{}} {0,-5:d8}";
Storage.LookupPatterns(pszTest, strlen(pszTest)); std::string str;
StandardLibrary::FormatTo(str, "test{0}", ); StandardLibrary::FormatTo(str, "{0}", char('c'), short()); #if FL_COMPILER_MSVC
StandardLibrary::FormatTo(str, "0x{0:x}", , DWORD());
#endif std::wstring wstr;
StandardLibrary::FormatTo(wstr, L"Test{1}, {2:f4}, {0}, {0,4}", L" X ", , -10.005f); cout << str << endl;
wcout << wstr << endl; TestProfile(); #if FL_PLATFORM_HAS_CPP11 && (FL_COMPILER_MSVC||FL_PLATFORM_MACOS)
TestProfileMultiThread();
#endif return ;
}
Windows Visual Studio 2013 Release下的输出:
0x64
Test20, -10.0050, X , X
0x64
Test20, -10.0050, X , X
FLElapse:0.0762746
CLElapse:0.269722
FLElapse:0.0756153
FLElapse:0.0766446
FLElapse:0.0762051
CLElapse:0.285714
CLElapse:0.288648
CLElapse:0.289193
Mac Xcode Release:
Test20, -10.0050, X , X
FLElapse:0.0901681
CLElapse:0.19329
FLElapse:0.147378
FLElapse:0.150375
FLElapse:0.153342
CLElapse:0.303508
CLElapse:0.308418
CLElapse:0.307407
这只是一个简单的测试,结果并不能覆盖所有情况,然而可以明确的是,FL在提供了:编译期类型安全检查,不定参数,多线程支持,可重入,可乱序等功能的前提下,并不会比C库函数sprintf慢。因此可以认为在使用C++的情况下,始终应该使用FL代替传统的各种格式化字符串的方式以获取更好更安全的代码。
FL被设计成Header Only的库,这样用起来就更方便了,只需要简单的包含Format.hpp头文件即可获取到所有的功能。同时针对C++ 11做了专门的优化,使得在支持C++ 11的编译器上可以取得更快的运行速度,这是其它库无法比拟的。
如果你使用的是STL的字符串string或者wstring,那么FL已经内置了对应的支持,所有的适配器都已经默认提供。假如你不是使用的STL,比如UnrealEngine中使用的FString,那么你也可以非常容易的集成FL到自己的项目中。UnrealEngine的FString在集成了FL之后,会多出一个静态的FormatTo函数和一个Format成员函数,它们可以用于使用.net风格产生一个字符串或者直接格式化自己。在项目中也可以找到UnrealEngine的集成代码,非常简单。
FL已经成功使用Visual Studio,XCode和Codeblocks with gcc进行编译,并支持windows,macos,linux,ios等平台。已经成功用于多个项目。
项目地址:https://github.com/sczybt/CPPFormatLibrary
使用git Clone地址:https://github.com/sczybt/CPPFormatLibrary.git
你也可以直接Download zip
另外,一个代码库不可能没有bug,如果你发现了问题,可以将测试用例发给我,我会修复之。欢迎大家来使用。
接下来我会针对这个库提供更多的说明和指导文档。
2015.10.15
CPPFormatLibary提升效率的优化原理
关于优化策略的相关讨论:http://www.cnblogs.com/bodong/p/4800340.html