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