Path: blob/main/contrib/kyua/utils/format/formatter.hpp
48199 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/format/formatter.hpp29/// Provides the definition of the utils::format::formatter class.30///31/// The utils::format::formatter class is a poor man's replacement for the32/// Boost.Format library, as it is much simpler and has less dependencies.33///34/// Be aware that the formatting supported by this module is NOT compatible35/// with printf(3) nor with Boost.Format. The general syntax for a36/// placeholder in a formatting string is:37///38/// %[0][width][.precision]s39///40/// In particular, note that the only valid formatting specifier is %s: the41/// library deduces what to print based on the type of the variable passed42/// in, not based on what the format string says. Also, note that the only43/// valid padding character is 0.4445#if !defined(UTILS_FORMAT_FORMATTER_HPP)46#define UTILS_FORMAT_FORMATTER_HPP4748#include "utils/format/formatter_fwd.hpp"4950#include <sstream>51#include <string>5253namespace utils {54namespace format {555657/// Mechanism to format strings similar to printf.58///59/// A formatter always maintains the original format string but also holds a60/// partial expansion. The partial expansion is immutable in the context of a61/// formatter instance, but calls to operator% return new formatter objects with62/// one less formatting placeholder.63///64/// In general, one can format a string in the following manner:65///66/// \code67/// const std::string s = (formatter("%s %s") % "foo" % 5).str();68/// \endcode69///70/// which, following the explanation above, would correspond to:71///72/// \code73/// const formatter f1("%s %s");74/// const formatter f2 = f1 % "foo";75/// const formatter f3 = f2 % 5;76/// const std::string s = f3.str();77/// \endcode78class formatter {79/// The original format string provided by the user.80std::string _format;8182/// The current "expansion" of the format string.83///84/// This field gets updated on every call to operator%() to have one less85/// formatting placeholder.86std::string _expansion;8788/// The position of _expansion from which to scan for placeholders.89std::string::size_type _last_pos;9091/// The position of the first placeholder in the current expansion.92std::string::size_type _placeholder_pos;9394/// The first placeholder in the current expansion.95std::string _placeholder;9697/// Stream used to format any possible argument supplied by operator%().98std::ostringstream* _oss;99100formatter replace(const std::string&) const;101102void init(void);103formatter(const std::string&, const std::string&,104const std::string::size_type);105106public:107explicit formatter(const std::string&);108~formatter(void);109110const std::string& str(void) const;111operator const std::string&(void) const;112113template< typename Type > formatter operator%(const Type&) const;114formatter operator%(const bool&) const;115};116117118} // namespace format119} // namespace utils120121122#endif // !defined(UTILS_FORMAT_FORMATTER_HPP)123124125