1 // Test
2 #include "Format.hpp"
3
4 #include "Format/ProgressTimer.hpp"
5
6 #define TEST_PERFORMANCE_IN_TOOLS 0
7
8 using namespace FormatLibrary;
9
10 #include <iostream>
11 #include <vector>
12 using namespace std;
13
14 void TestProfile()
15 {
16 const int TEST_COUNT = 100000;
17
18 {
19 Profile::ProgressTimer Timer("FL");
20
21 for (int i = 0; i < TEST_COUNT; ++i)
22 {
23 string str;
24 StandardLibrary::FormatTo(str, "{0}--#--{1,8}--#--{2}", 100, -40.2f, " String ");
25 StandardLibrary::FormatTo(str, "{0}--#--{1,8}--#--{1}", 100, -40.2f);
26 StandardLibrary::FormatTo(str, "{0}--#--{1,8}--#--{3}", 100, -40.2f, std::string("xxx"));
27 }
28 }
29
30 #if !FL_COMPILER_MSVC
31 #define sprintf_s sprintf
32 #endif
33
34 #if !TEST_PERFORMANCE_IN_TOOLS
35 {
36 Profile::ProgressTimer Timer("CL");
37
38 for (int i = 0; i < TEST_COUNT; ++i)
39 {
40 string str;
41 char szBuf[64];
42 sprintf_s(szBuf, "%d--#--%8.2f--#--%s", 100, -40.2f, " String ");
43 str = szBuf;
44 sprintf_s(szBuf, "%d--#--%8.2f--#--%f", 100, -40.2f, 0.0f);
45 str = szBuf;
46 sprintf_s(szBuf, "%d--#--%8.2f--#--%%f", 100, -40.2f);
47 str = szBuf;
48 }
49 }
50 #endif
51 }
52
53 #if FL_PLATFORM_HAS_CPP11 && (FL_COMPILER_MSVC||FL_PLATFORM_MACOS)
54 #include <thread>
55
56 void TestProfileMultiThread()
57 {
58 std::thread t0( TestProfile );
59 std::thread t1( TestProfile );
60 std::thread t2( TestProfile );
61
62 t0.join();
63 t1.join();
64 t2.join();
65 }
66 #endif
67
68 int main()
69 {
70 StandardLibrary::STLGlobalPatternStorageA Storage;
71 Utility::TAutoString<char> TestStr;
72
73 const char* pszTest = "{0},xxxd{1:d2}={2,3:d2} !! {{}} {0,-5:d8}";
74 Storage.LookupPatterns(pszTest, strlen(pszTest));
75
76 std::string str;
77 StandardLibrary::FormatTo(str, "test{0}", 10);
78
79 StandardLibrary::FormatTo(str, "{0}", char('c'), short(2));
80
81 #if FL_COMPILER_MSVC
82 StandardLibrary::FormatTo(str, "0x{0:x}", 100, DWORD(100));
83 #endif
84
85 std::wstring wstr;
86 StandardLibrary::FormatTo(wstr, L"Test{1}, {2:f4}, {0}, {0,4}", L" X ", 20, -10.005f);
87
88 cout << str << endl;
89 wcout << wstr << endl;
90
91 TestProfile();
92
93 #if FL_PLATFORM_HAS_CPP11 && (FL_COMPILER_MSVC||FL_PLATFORM_MACOS)
94 TestProfileMultiThread();
95 #endif
96
97 return 0;
98 }
Windows Visual Studio 2013 Release下的輸出:
0x64 Test20, -10.0050, X , X 0x64 Test20, -10.0050, X , X 1920 FLElapse:0.0762746 1920 CLElapse:0.269722 1636 FLElapse:0.0756153 7732 FLElapse:0.0766446 7956 FLElapse:0.0762051 7956 CLElapse:0.285714 1636 CLElapse:0.288648 7732 CLElapse:0.289193
Mac Xcode Release:
99 Test20, -10.0050, X , X 18446744073709551615 FLElapse:0.0901681 18446744073709551615 CLElapse:0.19329 18446744073709551615 FLElapse:0.147378 18446744073709551615 FLElapse:0.150375 18446744073709551615 FLElapse:0.153342 18446744073709551615 CLElapse:0.303508 18446744073709551615 CLElapse:0.308418 18446744073709551615 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等平台。已經成功用於多個項目。 你也可以直接Download zip 另外,一個代碼庫不可能沒有bug,如果你發現了問題,可以將測試用例發給我,我會修復之。歡迎大家來使用。 接下來我會針對這個庫提供更多的說明和指導文檔。