E-MailRelay
gformat.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gformat.h
19///
20
21#ifndef G_FORMAT_H
22#define G_FORMAT_H
23
24#include "gdef.h"
25#include "gstringarray.h"
26#include <string>
27#include <ostream>
28#include <sstream>
29
30namespace G
31{
32 class format ;
33}
34
35//| \class G::format
36/// A simple version of boost::format for formatting strings in
37/// an i18n-friendly way.
38///
39/// Eg:
40/// \code
41/// using G::format ; // or boost::format
42/// std::cout << format("a %2% %1% d") % "c" % "b" << "\n" ;
43/// \endcode
44///
46{
47public:
48 explicit format( const std::string & fmt ) ;
49 ///< Constructor.
50
51 explicit format( const char * fmt ) ;
52 ///< Constructor.
53
54 format & parse( const std::string & fmt ) ;
55 ///< Resets the object with the given format string.
56
57 format & parse( const char * fmt ) ;
58 ///< Resets the object with the given format string.
59
60 std::string str() const ;
61 ///< Returns the string.
62
63 std::size_t size() const ;
64 ///< Returns the string size.
65
66 template <typename T> format & operator%( const T & ) ;
67 ///< Applies a substitution value.
68
69private:
70 void apply( const std::string & ) ;
71 static bool isdigit( char ) noexcept ;
72
73private:
74 std::string m_fmt ;
75 std::size_t m_i {0U} ;
76 StringArray m_values ;
77} ;
78
79namespace G
80{
81 std::ostream & operator<<( std::ostream & stream , const format & f ) ;
82 inline std::string str( const format & f )
83 {
84 return f.str() ;
85 }
86 template <typename T> format & format::operator%( const T & item )
87 {
88 std::ostringstream ss ;
89 ss << item ;
90 apply( ss.str() ) ;
91 return *this ;
92 }
93}
94
95#endif
A simple version of boost::format for formatting strings in an i18n-friendly way.
Definition: gformat.h:46
std::size_t size() const
Returns the string size.
Definition: gformat.cpp:93
format & parse(const std::string &fmt)
Resets the object with the given format string.
Definition: gformat.cpp:38
std::string str() const
Returns the string.
Definition: gformat.cpp:63
format(const std::string &fmt)
Constructor.
Definition: gformat.cpp:26
format & operator%(const T &)
Applies a substitution value.
Definition: gformat.h:86
Low-level classes.
Definition: garg.h:36
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30