Home | History | Annotate | Line # | Download | only in format
      1  1.1  jmmv // Copyright 2010 Google Inc.
      2  1.1  jmmv // All rights reserved.
      3  1.1  jmmv //
      4  1.1  jmmv // Redistribution and use in source and binary forms, with or without
      5  1.1  jmmv // modification, are permitted provided that the following conditions are
      6  1.1  jmmv // met:
      7  1.1  jmmv //
      8  1.1  jmmv // * Redistributions of source code must retain the above copyright
      9  1.1  jmmv //   notice, this list of conditions and the following disclaimer.
     10  1.1  jmmv // * Redistributions in binary form must reproduce the above copyright
     11  1.1  jmmv //   notice, this list of conditions and the following disclaimer in the
     12  1.1  jmmv //   documentation and/or other materials provided with the distribution.
     13  1.1  jmmv // * Neither the name of Google Inc. nor the names of its contributors
     14  1.1  jmmv //   may be used to endorse or promote products derived from this software
     15  1.1  jmmv //   without specific prior written permission.
     16  1.1  jmmv //
     17  1.1  jmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18  1.1  jmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19  1.1  jmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20  1.1  jmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21  1.1  jmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22  1.1  jmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23  1.1  jmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  jmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  jmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  jmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  1.1  jmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  jmmv 
     29  1.1  jmmv /// \file utils/format/formatter.hpp
     30  1.1  jmmv /// Provides the definition of the utils::format::formatter class.
     31  1.1  jmmv ///
     32  1.1  jmmv /// The utils::format::formatter class is a poor man's replacement for the
     33  1.1  jmmv /// Boost.Format library, as it is much simpler and has less dependencies.
     34  1.1  jmmv ///
     35  1.1  jmmv /// Be aware that the formatting supported by this module is NOT compatible
     36  1.1  jmmv /// with printf(3) nor with Boost.Format.  The general syntax for a
     37  1.1  jmmv /// placeholder in a formatting string is:
     38  1.1  jmmv ///
     39  1.1  jmmv ///     %[0][width][.precision]s
     40  1.1  jmmv ///
     41  1.1  jmmv /// In particular, note that the only valid formatting specifier is %s: the
     42  1.1  jmmv /// library deduces what to print based on the type of the variable passed
     43  1.1  jmmv /// in, not based on what the format string says.  Also, note that the only
     44  1.1  jmmv /// valid padding character is 0.
     45  1.1  jmmv 
     46  1.1  jmmv #if !defined(UTILS_FORMAT_FORMATTER_HPP)
     47  1.1  jmmv #define UTILS_FORMAT_FORMATTER_HPP
     48  1.1  jmmv 
     49  1.1  jmmv #include <sstream>
     50  1.1  jmmv #include <string>
     51  1.1  jmmv 
     52  1.1  jmmv namespace utils {
     53  1.1  jmmv namespace format {
     54  1.1  jmmv 
     55  1.1  jmmv 
     56  1.1  jmmv /// Mechanism to format strings similar to printf.
     57  1.1  jmmv ///
     58  1.1  jmmv /// A formatter always maintains the original format string but also holds a
     59  1.1  jmmv /// partial expansion.  The partial expansion is immutable in the context of a
     60  1.1  jmmv /// formatter instance, but calls to operator% return new formatter objects with
     61  1.1  jmmv /// one less formatting placeholder.
     62  1.1  jmmv ///
     63  1.1  jmmv /// In general, one can format a string in the following manner:
     64  1.1  jmmv ///
     65  1.1  jmmv /// \code
     66  1.1  jmmv /// const std::string s = (formatter("%s %s") % "foo" % 5).str();
     67  1.1  jmmv /// \endcode
     68  1.1  jmmv ///
     69  1.1  jmmv /// which, following the explanation above, would correspond to:
     70  1.1  jmmv ///
     71  1.1  jmmv /// \code
     72  1.1  jmmv /// const formatter f1("%s %s");
     73  1.1  jmmv /// const formatter f2 = f1 % "foo";
     74  1.1  jmmv /// const formatter f3 = f2 % 5;
     75  1.1  jmmv /// const std::string s = f3.str();
     76  1.1  jmmv /// \endcode
     77  1.1  jmmv class formatter {
     78  1.1  jmmv     /// The original format string provided by the user.
     79  1.1  jmmv     std::string _format;
     80  1.1  jmmv 
     81  1.1  jmmv     /// The current "expansion" of the format string.
     82  1.1  jmmv     ///
     83  1.1  jmmv     /// This field gets updated on every call to operator%() to have one less
     84  1.1  jmmv     /// formatting placeholder.
     85  1.1  jmmv     std::string _expansion;
     86  1.1  jmmv 
     87  1.1  jmmv     /// The position of _expansion from which to scan for placeholders.
     88  1.1  jmmv     std::string::size_type _last_pos;
     89  1.1  jmmv 
     90  1.1  jmmv     /// The position of the first placeholder in the current expansion.
     91  1.1  jmmv     std::string::size_type _placeholder_pos;
     92  1.1  jmmv 
     93  1.1  jmmv     /// The first placeholder in the current expansion.
     94  1.1  jmmv     std::string _placeholder;
     95  1.1  jmmv 
     96  1.1  jmmv     /// Stream used to format any possible argument supplied by operator%().
     97  1.1  jmmv     std::ostringstream* _oss;
     98  1.1  jmmv 
     99  1.1  jmmv     formatter replace(const std::string&) const;
    100  1.1  jmmv 
    101  1.1  jmmv     void init(void);
    102  1.1  jmmv     formatter(const std::string&, const std::string&,
    103  1.1  jmmv               const std::string::size_type);
    104  1.1  jmmv 
    105  1.1  jmmv public:
    106  1.1  jmmv     explicit formatter(const std::string&);
    107  1.1  jmmv     ~formatter(void);
    108  1.1  jmmv 
    109  1.1  jmmv     const std::string& str(void) const;
    110  1.1  jmmv     operator const std::string&(void) const;
    111  1.1  jmmv 
    112  1.1  jmmv     template< typename Type > formatter operator%(const Type&) const;
    113  1.1  jmmv     formatter operator%(const bool&) const;
    114  1.1  jmmv };
    115  1.1  jmmv 
    116  1.1  jmmv 
    117  1.1  jmmv }  // namespace format
    118  1.1  jmmv }  // namespace utils
    119  1.1  jmmv 
    120  1.1  jmmv 
    121  1.1  jmmv #endif  // !defined(UTILS_FORMAT_FORMATTER_HPP)
    122