C + + string format library: cppformatlibrary

Source: Internet
Author: User

This is a long time ago, summed up a bit last year, to separate it out, as an open source library on GitHub, but Cppformat and other names have been squatting, the results had to register a so the name of the pain: Cppformatlibrary, hereinafter referred to as FL.

Let's start by introducing what this is. We know that in C + + to format the string, usually using C library function sprintf or C + + StringStream, but both have their own problems, such as the C library function type security problem, sprintf when the parameters are insufficient, Or if the parameter type does not match the formatted character, there will be an error, resulting in a crash, and the efficiency of the stringstream is obviously not. In addition, I also know that the Boost library has format to use, but it is inefficient. In addition, there are foreign gods have written Fastformat library, address: http://fastformat.sourceforge.net/. The problem is that it's too big, it's inconvenient to integrate, it introduces too much of what you don't need, and it doesn't put performance to its limits. I wrote this FL library originally inspired by fastformat, and especially thanks to it.

FL, like Fastformat, is committed to solving the various problems of the C + + string formatting described earlier, and the eventual adoption of the. NET scenario, the formatted string for. NET, can be viewed in this article: Http://www.cnblogs.com/zyh-nhy /archive/2007/10/11/921240.html and Fastformat, FL removed some of the features that were not used, and enhanced functionality, such as Fastformat does not support a finer description of {0,5}, while FL supports it. Of course, Fastformat and FL do not completely cover. NET format, which means that it is not entirely equivalent to. NET formatting, and some features are not supported.

The following is the test code in FL, which shows the most basic formatting features:

1 //Test2#include"format.hpp"3 4#include"format/progresstimer.hpp"5 6 #defineTest_performance_in_tools 07 8 using namespaceformatlibrary;9 Ten#include <iostream> One#include <vector> A using namespacestd; -  - voidTestprofile () the { -     Const intTest_count =100000; -  -     { +Profile::P Rogresstimer Timer ("FL"); -  +          for(inti =0; i < Test_count; ++i) A         { at             stringstr; -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")); -         } -     } in  - #if! Fl_compiler_msvc to #definesprintf_s sprintf + #endif -  the #if! Test_performance_in_tools *     { $Profile::P Rogresstimer Timer ("CL");Panax Notoginseng  -          for(inti =0; i < Test_count; ++i) the         { +             stringstr; A             Charszbuf[ -]; thesprintf_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; -         } the     } - #endifWuyi } the  - #ifFl_platform_has_cpp11 && (fl_compiler_msvc| | Fl_platform_macos) Wu#include <thread> -  About voidTestprofilemultithread () $ { - std::thread t0 (testprofile); - std::thread T1 (testprofile); - std::thread T2 (testprofile); A  + T0.join (); the T1.join (); - T2.join (); $ } the #endif the  the intMain () the { - Standardlibrary::stlglobalpatternstoragea Storage; inutility::tautostring<Char>Teststr; the  the     Const Char* Psztest ="{0},XXXD{1:D2}={2,3:D2}!! {{}} {0,-5:d8}"; About storage.lookuppatterns (Psztest, strlen (psztest)); the  theSTD::stringstr; theStandardlibrary::formatto (str,"test{0}",Ten); +  -Standardlibrary::formatto (str,"{0}",Char('C'), Short(2)); the Bayi #ifFl_compiler_msvc theStandardlibrary::formatto (str,"0x{0:x}", -, DWORD ( -)); the #endif -  - std::wstring wstr; theStandardlibrary::formatto (WSTR, L"Test{1}, {2:f4}, {0}, {0,4}", L"X", -, -10.005f); the  thecout << str <<Endl; theWcout << wstr <<Endl; -  the testprofile (); the      the #ifFl_platform_has_cpp11 && (fl_compiler_msvc| | Fl_platform_macos)94 Testprofilemultithread (); the #endif the  the     return 0;98}

Output under Windows Visual Studio release:

0x64Test20,-10.0050, x, x0x64Test20,-10.0050, x, x1920x1080Flelapse:0.07627461920x1080Clelapse:0.2697221636Flelapse:0.07561537732Flelapse:0.07664467956Flelapse:0.07620517956Clelapse:0.2857141636Clelapse:0.2886487732Clelapse:0.289193

Mac Xcode Release:

 AboutTest20,-10.0050, x, x18446744073709551615Flelapse:0.090168118446744073709551615Clelapse:0.1932918446744073709551615Flelapse:0.14737818446744073709551615Flelapse:0.15037518446744073709551615Flelapse:0.15334218446744073709551615Clelapse:0.30350818446744073709551615Clelapse:0.30841818446744073709551615Clelapse:0.307407

This is just a simple test, and the results do not cover all cases, but it is clear that FL provides: compile-time type security checks, indefinite parameters, multithreading support, reentrant, can be disorderly and other functions under the premise, and will not be slower than the C library function sprintf. Therefore, you can assume that in the case of C + +, you should always use FL instead of the traditional way of formatting strings to get better and more secure code.

FL is designed as the header only library, so it is more convenient to use, just need to include a simple format.hpp header file to get all the functions. The special optimizations for C + + 11 make it possible to run faster on compilers that support C + + 11, which is unmatched by other libraries.

If you are using STL string or Wstring, FL has built-in support, and all adapters are already provided by default. If you are not using STL, such as the fstring used in Unrealengine, then you can easily integrate FL into your project. After integrating FL, Unrealengine's fstring will have a static Formatto function and a format member function that can be used to produce a string or directly format itself using the. NET style. The Unrealengine integration code can also be found in the project, which is very simple.

FL has successfully compiled with visual Studio,xcode and codeblocks with GCC, and supports platforms such as Windows,macos,linux,ios. has been successfully used for multiple projects.

Project Address: Https://github.com/sczybt/CPPFormatLibrary

Use git clone address: https://github.com/sczybt/CPPFormatLibrary.git

You can also directly download zip

In addition, a code base can not be without bugs, if you find the problem, you could send me the test case, I will fix it. Welcome everyone to use.

Next I will provide more instructions and guidance for this library.

C + + string format library: cppformatlibrary

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.