E-MailRelay

gstringlist.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 gstringlist.cpp
19///
20
21#include "gdef.h"
22#include "gstringlist.h"
23#include "gstr.h"
24#include "gassert.h"
25#include <algorithm>
26#include <functional>
27#include <string>
28
29 namespace G
30{
31 namespace StringListImp
32 {
33 bool inList( StringArray::const_iterator begin , StringArray::const_iterator end ,
34 const std::string & s , bool i ) ;
35 bool notInList( StringArray::const_iterator begin , StringArray::const_iterator end ,
36 const std::string & s , bool i ) ;
37 bool match( const std::string & a , const std::string & b , bool ignore_case ) ;
38 }
39}
40
41 bool G::StringListImp::inList( StringArray::const_iterator begin , StringArray::const_iterator end ,
42 const std::string & s , bool ignore )
43{
44 using namespace std::placeholders ;
45 return std::any_of( begin , end , std::bind(match,_1,std::cref(s),ignore) ) ;
46}
47
48 bool G::StringListImp::notInList( StringArray::const_iterator begin , StringArray::const_iterator end ,
49 const std::string & s , bool ignore_case )
50{
51 return !inList( begin , end , s , ignore_case ) ;
52}
53
54 bool G::StringListImp::match( const std::string & a , const std::string & b , bool ignore_case )
55{
56 return ignore_case ? sv_imatch(std::string_view(a),std::string_view(b)) : (a==b) ;
57}
58
59 void G::StringList::keepMatch( StringArray & list , const StringArray & match_list , Ignore ignore )
60{
61 using namespace std::placeholders ;
62 if( !match_list.empty() )
63 list.erase(
64 std::remove_if( list.begin() , list.end() ,
65 std::bind(StringListImp::notInList,match_list.begin(),match_list.end(),_1,ignore==Ignore::Case) ) ,
66 list.end() ) ;
67}
68
69 void G::StringList::removeMatch( StringArray & list , const StringArray & deny_list , Ignore ignore )
70{
71 using namespace std::placeholders ;
72 list.erase(
73 std::remove_if( list.begin() , list.end() ,
74 std::bind(StringListImp::inList,deny_list.begin(),deny_list.end(),_1,ignore==Ignore::Case) ) ,
75 list.end() ) ;
76}
77
78 bool G::StringList::headMatch( const StringArray & in , std::string_view head )
79{
80 return std::any_of( in.begin() , in.end() ,
81 [&head](const std::string &x){return Str::headMatch(x,head);} ) ;
82}
83
84#ifndef G_LIB_SMALL
85 bool G::StringList::tailMatch( const StringArray & in , std::string_view tail )
86{
87 return std::any_of( in.begin() , in.end() ,
88 [&tail](const std::string &x){return Str::tailMatch(x,tail);} ) ;
89}
90#endif
91
92std::string G::StringList::headMatchResidue( const StringArray & in , std::string_view head )
93{
94 const auto end = in.end() ;
95 for( auto p = in.begin() ; p != end ; ++p )
96 {
97 if( Str::headMatch( *p , head ) )
98 return (*p).substr( head.size() ) ;
99 }
100 return {} ;
101}
102
103 bool G::StringList::match( const StringArray & a , const std::string & b )
104{
105 return std::find( a.begin() , a.end() , b ) != a.end() ;
106}
107
108 bool G::StringList::imatch( const StringArray & a , const std::string & b )
109{
110 using namespace std::placeholders ;
111 std::string_view b_sv( b ) ;
112 return std::any_of( a.begin() , a.end() ,
113 [b_sv](const std::string & a_str){ return Str::imatch(a_str,b_sv) ; } ) ;
114}
115
gassert.h
gstr.h
gstringlist.h
G::StringList::keepMatch
void keepMatch(StringArray &list, const StringArray &allow_list, Ignore=Ignore::Nothing)
Removes items in the list that do not match any entry in the allow list.
Definition: gstringlist.cpp:59
G::StringList::match
bool match(const StringArray &, const std::string &)
Returns true if any string in the array matches the given string.
Definition: gstringlist.cpp:103
G::StringList::tailMatch
bool tailMatch(const StringArray &list, std::string_view ending)
Returns true if any string in the array has the given ending (or the given ending is empty).
Definition: gstringlist.cpp:85
G::StringList::headMatch
bool headMatch(const StringArray &list, std::string_view head)
Returns true if any string in the array has the given start (or 'head' is empty).
Definition: gstringlist.cpp:78
G::StringList::imatch
bool imatch(const StringArray &, const std::string &)
Returns true if any string in the array matches the given string, ignoring case.
Definition: gstringlist.cpp:108
G::StringList::removeMatch
void removeMatch(StringArray &list, const StringArray &deny_list, Ignore=Ignore::Nothing)
Removes items in the list that match an entry in the deny list.
Definition: gstringlist.cpp:69
G::StringList::headMatchResidue
std::string headMatchResidue(const StringArray &list, std::string_view head)
Returns the unmatched part of the first string in the array that has the given start.
Definition: gstringlist.cpp:92
G
Low-level classes.
Definition: garg.h:36
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30