The Artima Developer Community
Sponsored Link

C++ Community News Forum
FastFormat 0.3.5 released

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Matthew Wilson

Posts: 145
Nickname: bigboy
Registered: Jun, 2004

FastFormat 0.3.5 released Posted: May 13, 2009 10:36 PM
Reply to this message Reply
Summary
FastFormat is an open-source C++ formatting library that provides localised text formatting, optionally using resource-based format strings. It's 100% type-safe, flexible, infinitely extensible, and highly efficient. As of 0.3.5 it has production quality support for all built-in types, and the flexibility to support any user-defined types.
Advertisement

FastFormat is an open source C++ formatting library, whose design parameters are 100% type-safety, efficiency, and flexibility. It is simple to use, highly-portable (platform and compiler-independent), and is infinitely extensible.

FastFormat supports output/formatting of statements of arbitrary complexity, consisting of heterogeneous types. It provides two APIs:

  • Format: provides replacement-based formatting, like Streams (printf()-family), Boost.Format()

      fastformat::fmt(result, "x={1}, y={0}", y, x);

    The Format API provides for specifying minimum width:

      fastformat::fmt(result, "x={0,10}", 123456); // at least 10 chars wide

    and/or maximum width:

      fastformat::fmt(result, "x={0,,5}", 123456); // no more than 5 chars wide

    and alignment:

      fastformat::fmt(result, "x={0,10,10,<}", 123456); // aligns left in a field exactly 10 wide

      fastformat::fmt(result, "x={0,10,10,>}", 123456); // aligns right in a field exactly 10 wide

      fastformat::fmt(result, "x={0,10, ,^}", 123456); // aligns centrally in a field at least 10 wide

  • Write: provides concatenation-based formatting, like IOStreams

      fastformat::write(result, "x=", x, ", y=" y);

FastFormat writes to output "sinks", which can be of arbitrary type. It implicitly supports any type that is structurally conformant with the standard library's string, and the library includes adaptors to allow writing to:

  • std::ostream
  • FILE*
  • STLSoft's auto_buffer
  • C-style string buffers and character buffers
  • ACE's ACE_String
  • ATL's CComBSTR
  • MFC's CString
  • std::stringstream
  • speech (currently Windows-only)
  • Windows' OutputDebugString()
  • Windows' MessageBox()
Adaptation to a new type requires the definition of a single function.

FastFormat is robust. Both APIs are 100% type-safe - something no other C/C++ widely-used formatting library can claim - and with the Write API it is impossible to compile defective code.

FastFormat is fast. The processing of each statement involves at most one memory allocation to hold the entire statement, and each statement element is measured and copied exactly once. As a consequence, the library is on a par with (the type-unsafe) C's Streams (printf()-family) of functions, faster than C++'s IOStreams (by 2-10x) and Loki.SafeFormat (by 1-5x), and considerably faster than Boost.Format (by 5-17x).

FastFormat supports I18N/L10N by using numbered arguments, enabling reordering of arguments by exchanging format strings. The library comes with a number of resource bundles, classes whose instances can load sets of localised resource strings for use as format strings.

FastFormat does not contain any compiler-specific or platform-specific constructs. It supports UNIX (including Linux and Mac OS-X), and Windows, and should work with any operating system. It is known to be compatible with Comeau (4.3.3+), GCC (3.4+), Intel (8+), Metrowerks (8+), Microsoft Visual C++ (6.0+), and should work with any reasonably modern C++ compiler.

FastFormat is completely free and includes source released under a BSD-style license. Commercial customisations and related consultancy are provided by Synesis Software)

FastFormat features in a series of articles in ACCU's peer-reviewed Overload magazine:

Examples:

  • Emulate Streams' ability to specify widths:

      printf("[%-10d, %10d]\n", 123, -456);

      fastformat::fmtln(std::cout, "[{0,10,,<}, {1,10}]", 123, -456);

    Both produce the output:

      [123       ,       -456]

  • Handle heterogeneous types:

      struct connection_t
      {
        std::string     connectionId;
        struct in_addr  remoteAddress;
        struct in_addr  localAddress;
        unsigned short  port;
        unsigned long   numBytesTransferred;
        struct tm       completionTime;
      };

      void log_connection(connection_t const& conn)
      {
        fastformat::fmtln(
          std::cout
        , "{0} {5} {1} {2} {3} {4}"
        , conn.connectionId
        , conn.remoteAddress
        , conn.localAddress
        , conn.port
        , conn.numBytesTransferred
        , conn.completionTime
      );
      }


    Produces output like:

      channel-1 May 03 03:50:41 2009 192.168.160.247 127.0.0.1 5651 102401

  • Emulate Boost.Format's absolute tabulations (with a little indirection):

      std::cout
          << boost::format("%1% %2%, %|20t|lives at %3%\n")
              % "Mr"
              % "Smith"
              % "the Smith residence"
          << std::endl;


      std::string scratch;

      fastformat::fmtln(
          std::cout
      ,   "{0,20,,<}lives at {1}"
      ,   fastformat::fmt(
              scratch
          ,   "{0} {1}, "
          ,   "Mr"
          ,   "Smith"
          )
      ,   "the Smith residence"
      );


    Both produce the output:

      Mr Smith,           lives at the Smith residence

Performance:

FastFormat's performance has been extensively tested, and in every case it has superior performance to the C++ standard library's IOStreams, Boost.Format and Loki.SafeFormat. In most cases it has equivalent performance to the (type-unsafe) C standard library's Streams (printf()-family). Test results are included on the project website (http://www.fastformat.org/performance.html), and in the first in the Overload article series; the third article in the series has a further set of four performance tests, based on real-world uses of FastFormat in commercial projects.

Features:

  • 100% type-safe
  • Presents a natural syntax
  • Infinitely extensible
  • Highly-efficient:
    • no intermediate memory allocations
    • no intermediate string copying
    • no repetition of any string-length calculations
    • at most one allocation required to produce the resultant string
  • Verified with Borland, Metrowerks' CodeWarrior, Digital Mars, GCC, Intel, and Visual C++ compilers
  • Verified on Linux, Mac OS-X, and Windows

Requirements:

    STLSoft libraries (1.9.1 beta 81 or later; available from http://stlsoft.org/downloads.html); 100% header-only open-source.

Home page: http://www.fastformat.org/
Download: http://sourceforge.net/project/showfiles.php?group_id=177382&package_id=204396

Topic: Synesis Software Releases The New recls 100% .NET Library Previous Topic   Next Topic Topic: ACCU Annual Conference in Oxford, 2nd - 5th April.


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us