E-MailRelay
gformat.cpp
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.cpp
19///
20
21#include "gdef.h"
22#include "gformat.h"
23#include "gstr.h"
24
25#ifndef G_LIB_SMALL
26G::format::format( const std::string & fmt ) :
27 m_fmt(fmt)
28{
29}
30#endif
31
32G::format::format( const char * fmt ) :
33 m_fmt(fmt)
34{
35}
36
37#ifndef G_LIB_SMALL
38G::format & G::format::parse( const std::string & fmt )
39{
40 m_fmt = fmt ;
41 m_i = 0U ;
42 m_values.clear() ;
43 return *this ;
44}
45#endif
46
47#ifndef G_LIB_SMALL
48G::format & G::format::parse( const char * fmt )
49{
50 m_fmt = fmt ;
51 m_i = 0U ;
52 m_values.clear() ;
53 return *this ;
54}
55#endif
56
57bool G::format::isdigit( char c ) noexcept
58{
59 // std::isdigit( static_cast<unsigned char>(c) )
60 return c >= '0' && c <= '9' ;
61}
62
63std::string G::format::str() const
64{
65 std::string s = m_fmt ;
66 const std::size_t npos = std::string::npos ;
67 for( std::size_t p = s.find('%') ; p != npos && (p+2U) < s.size() ; )
68 {
69 std::size_t q = s.find( '%' , p+1 ) ;
70 if( q != npos && q == (p+2U) && isdigit(s.at(p+1U)) ) // kiss 1..9 only
71 {
72 auto n = G::Str::toUInt( s.substr(p+1,1U) ) ;
73 if( n && n <= m_values.size() )
74 {
75 s.replace( p , 3U , m_values.at(n-1U) ) ;
76 p += m_values.at(n-1U).size() ;
77 }
78 else
79 {
80 s.erase( p , 3U ) ;
81 }
82 }
83 else
84 {
85 p++ ;
86 }
87 p = p < s.size() ? s.find('%',p) : npos ;
88 }
89 return s ;
90}
91
92#ifndef G_LIB_SMALL
93std::size_t G::format::size() const
94{
95 return str().size() ;
96}
97#endif
98
99void G::format::apply( const std::string & value )
100{
101 m_values.push_back( value ) ;
102}
103
104std::ostream & G::operator<<( std::ostream & stream , const format & f )
105{
106 return stream << f.str() ;
107}
108
static unsigned int toUInt(std::string_view s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:648
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