Boost string processing function -- format

Source: Internet
Author: User

Use boost: format to format strings

In string processing, formatting is indispensable. In C ++, the traditional Formatting Function is sprintf of C language, but it is a big problem that is not safe. Therefore, stringstream is introduced in stl for secure formatting, but stringstream is far less intuitive than sprintf. For example, the following code is used:

Char text [] = "hello ";
Bool is_all_lower = boost: algorithm: all (text, is_lower ());

Char output [128];
Sprintf (output, "<% s> % s in the lower case", text, (is_all_lower? "Is": "is not "));

If the last two format functions are written using stringstream, the readability is far inferior to that of sprintf.

Stringstream output;
Output <"<" <text <">"
<(Is_all_lower )? "Is": "is not ")
<"In the lower case ";

Boost introduces a function that provides string. format similar to string. format in. net to format a string. the following format is used:

Boost: format fmt = boost: format ("<% s> % s in the lower case") % text % (is_all_lower? "Is": "is not ");
String output = fmt. str ();

The preceding example demonstrates a C-style formatted string. boost. format also provides a format string similar to. net:

Boost: format fmt = boost: format ("<% 1%> % 2% in the lower case") % text % (is_all_lower? "Is": "is not ");
Cout <fmt <endl;

This method makes it easier to see the position of a parameter in the formatted string. We recommend this method. However, its starting coordinate is 1 rather than 0, and friends who are used to. net's string. format need to pay attention to it.

Formatting Control

The format syntax is [N $] [flags] [width] [. precision] type-char. It also provides the C language and. net style.

// Traditional C Language Style
Cout <boost: format ("\ n % s"

"% 1 t decimal = [% d] \ n"

"% 1 t formatted decimal = [% 5d] \ n"

"% 1 t formatted in decimal format, prefill '0' = [% 05d] \ n"

"% 1 t hexadecimal = [% x] \ n"

"% 1 t octal = [% o] \ n"

"% 1 t floating point = [% f] \ n"

"% 1 t formatted floating point = [% 3.3f] \ n"

"% 1 t scientific COUNT = [% e] \ n"

) % "Example: \ n" % 15% 15% 15% 15% 15% 15.01% 15.01% <endl;

//. Net Style
Cout <boost: format ("% 1%"

"% 1 t decimal = [% 2 $ d] \ n"

"% 1 t formatted decimal = [% 2 $ 5d] \ n"

"% 1 t formatted in decimal format, prefill '0' = [% 2 $ 05d] \ n"

"% 1 t hexadecimal = [% 2 $ x] \ n"

"% 1 t octal = [% 2 $ o] \ n"

"% 1 t floating point = [% 3 $ f] \ n"

"% 1 t formatted floating point = [% 3 $ 3.3f] \ n"

"% 1 t scientific COUNT = [% 3 $ e] \ n"

) % "Example: \ n" % 15% 15.01 <endl;

Exception Handling

Since the boost. format function is used to replace sprintf, it is natural to have the exception processing function, rather than being shown to you like sprintf. The boost. format method is to throw an exception. In the following two cases, an exception is thrown:

  1. The format string is invalid.
  2. Illegal format binding

The following code demonstrates the two scenarios:

Try
{
Boost: format ("<% 3 ");
}
Catch (std: exception & err)
{
Cout <err. what () <endl;
}

Boost: format fmt = boost: format ("<% 3%> % 2% in the lower case") % text % (is_all_lower? "Is": "is not ");
Try
{
Cout <fmt <endl;
}
Catch (std: exception & err)
{
Cout <err. what () <endl;
}

Encapsulation

Boost. format is implemented by an object rather than a function, which causes a lot of trouble in usage and Exception Handling. However, the Variable Parameter template syntax of c ++ 11 can easily encapsulate it into a Variable Parameter Function:

String string_fromat (const
Char * format ,...)

Three overload versions need to be defined:

Template <class
TFirst>
Void string_format (boost: format & fmt, TFirst & first)
{
Fmt % first;
}

Template <class
TFirst, class... TOther>
Void string_format (boost: format & fmt, TFirst & first, TOther &... other)
{
Fmt % first;
String_format (fmt, other ...);
}

Template <class
TFirst, class... TOther>
String string_format (const
Char * format, TFirst & first, TOther &... other)
{
Boost: format fmt (format );
String_format (fmt, first, other ...);
Return fmt. str ();
}

Now we can use it like this:

Auto output = string_format ("<% 1%> % 2% in the lower case", text, (is_all_lower? "Is": "is not "));

All exceptions are thrown in this function. Although the efficiency is relatively low, it is more comfortable to use.

PS: Variable Parameter templates are not supported yet in vc 2012. However, Microsoft has released a CTP version compiler that allows you to use this feature in vc 2012, it should be officially supported in the next update or sp1. Gcc can be perfectly supported. This feature can also be used in windows through MinGW.

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.