E-MailRelay

gformat.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 G::format::format( const std::string & fmt ) :
26 m_fmt(fmt)
27{
28}
29
30 G::format::format( const char * fmt ) :
31 m_fmt(fmt)
32{
33}
34
35 G::format & G::format::parse( const std::string & fmt )
36{
37 m_fmt = fmt ;
38 m_i = 0U ;
39 m_values.clear() ;
40 return *this ;
41}
42
43 G::format & G::format::parse( const char * fmt )
44{
45 m_fmt = fmt ;
46 m_i = 0U ;
47 m_values.clear() ;
48 return *this ;
49}
50
51 bool G::format::isdigit( char c )
52{
53 // std::isdigit( static_cast<unsigned char>(c) )
54 return c >= '0' && c <= '9' ;
55}
56
57std::string G::format::str() const
58{
59 std::string s = m_fmt ;
60 const std::size_t npos = std::string::npos ;
61 for( std::size_t p = s.find('%') ; p != npos && (p+2U) < s.size() ; )
62 {
63 std::size_t q = s.find( '%' , p+1 ) ;
64 if( q != npos && q == (p+2U) && isdigit(s.at(p+1U)) ) // kiss 1..9 only
65 {
66 auto n = G::Str::toUInt( s.substr(p+1,1U) ) ;
67 if( n && n <= m_values.size() )
68 {
69 s.replace( p , 3U , m_values.at(n-1U) ) ;
70 p += m_values.at(n-1U).size() ;
71 }
72 else
73 {
74 s.erase( p , 3U ) ;
75 }
76 }
77 else
78 {
79 p++ ;
80 }
81 p = p < s.size() ? s.find('%',p) : npos ;
82 }
83 return s ;
84}
85
86std::size_t G::format::size() const
87{
88 return str().size() ;
89}
90
91 void G::format::apply( const std::string & value )
92{
93 m_values.push_back( value ) ;
94}
95
96std::ostream & G::operator<<( std::ostream & stream , const format & f )
97{
98 return stream << f.str() ;
99}
100
G::Str::toUInt
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:604
G::format
A simple version of boost::format for formatting strings in an i18n-friendly way.
Definition: gformat.h:46
G::format::size
std::size_t size() const
Returns the string size.
Definition: gformat.cpp:86
G::format::parse
format & parse(const std::string &fmt)
Resets the object with the given format string.
Definition: gformat.cpp:35
G::format::str
std::string str() const
Returns the string.
Definition: gformat.cpp:57
G::format::format
format(const std::string &fmt)
Constructor.
Definition: gformat.cpp:25
gformat.h
gstr.h