C ++ string formatting Library: CPPFormatLibrary

Source: Internet
Author: User
Tags windows visual

C ++ string formatting Library: CPPFormatLibrary
This was written a long time ago. I summarized it last year and extracted it separately. As an open-source library, it was put on GitHub. However, CPPFormat and other names have already been noticed, as a result, we registered the CPPFormatLibrary, FL for short. First, we will introduce what this is. We know that to format strings in C ++, the C library function sprintf or stringstream of C ++ is usually used. However, both of them have their own problems, for example, the type security problem of the C library function. When sprintf has insufficient parameters or the parameter type does not match the formatting characters, errors will occur, resulting in a crash. stringstream is obviously inefficient. In addition, I also know that the boost library has a format that can be used, but its efficiency is not high. In addition, there are foreign god written fastformat library, address: http://fastformat.sourceforge.net /. The problem with it is that it is too large and inconvenient to integrate. It will introduce too many things you don't need. At the same time, it does not limit the performance. I was inspired by fastformat when I wrote this FL library, and I would like to thank it very much. Like fastformat, FL is committed to solving the problems of C ++ string formatting described earlier. The final solution is.. net solution, about. net format string, you can see this article: http://www.cnblogs.com/zyh-nhy/archive/2007/10/11/921240.html and fastformat to compare, FL removed some and useless functions, and enhance the function, for example, fastformat does not support more detailed descriptions such as {0, 5}, while FL does. Of course, both fastformat and FL do not completely overwrite the. net format. That is to say, they are not completely equivalent to. net formatting, and some functions are not supported. Below is the test code in FL, which shows the most basic Formatting Function:

 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_MSVC31 #define sprintf_s sprintf32 #endif33 34 #if !TEST_PERFORMANCE_IN_TOOLS35     {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 #endif51 }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 #endif67 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_MSVC82     StandardLibrary::FormatTo(str, "0x{0:x}", 100, DWORD(100));83 #endif84 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 #endif96 97     return 0;98 }

 

Windows Visual Studio 2013 Release output:
0x64Test20, -10.0050,  X ,  X0x64Test20, -10.0050,  X ,  X1920 FLElapse:0.07627461920 CLElapse:0.2697221636 FLElapse:0.07561537732 FLElapse:0.07664467956 FLElapse:0.07620517956 CLElapse:0.2857141636 CLElapse:0.2886487732 CLElapse:0.289193

 

Mac Xcode Release:
99Test20, -10.0050,  X ,  X 18446744073709551615 FLElapse:0.090168118446744073709551615 CLElapse:0.1932918446744073709551615 FLElapse:0.14737818446744073709551615 FLElapse:0.15037518446744073709551615 FLElapse:0.15334218446744073709551615 CLElapse:0.30350818446744073709551615 CLElapse:0.30841818446744073709551615 CLElapse:0.307407

 

This is just a simple test, and the results do not cover all the situations. However, it can be clear that FL provides: type security checks during compilation, indefinite parameters, multi-thread support, reentrant, it is not slower than the C library function sprintf when it can be out of order or other functions. Therefore, we can think that when C ++ is used, FL should always be used instead of the traditional methods of formatting strings to obtain better and safer code. FL is designed as a Header-Only library, which makes it easier to use. You Only need to include the Format. hpp Header file to obtain all the functions. At the same time, we have made special optimizations for C ++ 11, so that the compiler supporting C ++ 11 can achieve faster running speed, which is unmatched by other libraries. If you are using an STL string or wstring, FL has built-in support and all adapters are provided by default. If you are not using STL, such as FString in UnrealEngine, you can easily integrate FL into your project. After the FString of UnrealEngine is integrated with FL, a static FormatTo function and a Format member function are added. They can be used to generate a string using the. net style or directly Format themselves. You can also find the UnrealEngine integration code in the project, which is very simple. FL has been successfully compiled using Visual Studio, XCode, Codeblocks with gcc, and supports windows, macos, linux, ios, and other platforms. It has been successfully used for multiple projects. You can also directly Download the zip file. In addition, a code library cannot have no bugs. If you find any problems, you can send the test case to me and I will fix them. Welcome to use it. Next, I will provide more instructions and guidance documents for this library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.