E-MailRelay
gstringlist.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 gstringlist.h
19///
20
21#ifndef G_STRING_LIST_H
22#define G_STRING_LIST_H
23
24#include "gdef.h"
25#include "gstr.h"
26#include "gstringarray.h"
27#include "gstringview.h"
28#include "goptional.h"
29#include <string>
30
31namespace G
32{
33 namespace StringList /// Utility functions for lists of strings.
34 {
35 enum class Ignore
36 {
37 Case ,
38 Nothing
39 } ;
40
41 void keepMatch( StringArray & list , const StringArray & allow_list ,
42 Ignore = Ignore::Nothing ) ;
43 ///< Removes items in the list that do not match any entry
44 ///< in the allow list. Optionally uses a case-insensitive
45 ///< match.
46
47 void applyMatch( StringArray & list , const StringArray & allow_list ,
48 Ignore = Ignore::Nothing ) ;
49 ///< Removes items in the list that do not match any entry
50 ///< in the allow list and reorders the result to be the same
51 ///< as the allow list. Optionally uses a case-insensitive
52 ///< match.
53
54 void removeMatch( StringArray & list , const StringArray & deny_list ,
55 Ignore = Ignore::Nothing ) ;
56 ///< Removes items in the list that match an entry
57 ///< in the deny list. Optionally uses a case-insensitive
58 ///< match.
59
60 bool headMatch( const StringArray & list , std::string_view head ) ;
61 ///< Returns true if any string in the array has the given start
62 ///< (or 'head' is empty).
63
64 bool tailMatch( const StringArray & list , std::string_view ending ) ;
65 ///< Returns true if any string in the array has the given ending
66 ///< (or the given ending is empty).
67
68 std::string headMatchResidue( const StringArray & list , std::string_view head ) ;
69 ///< Returns the unmatched part of the first string in the array that has
70 ///< the given start. Returns the empty string if nothing matches or if
71 ///< the first match is an exact match for the whole string.
72
73 bool match( const StringArray & , const std::string & ) ;
74 ///< Returns true if any string in the array matches the given string.
75
76 bool imatch( const StringArray & , const std::string & ) ;
77 ///< Returns true if any string in the array matches the given string,
78 ///< ignoring case.
79
80 struct Filter /// Filters a list of strings with allow and deny lists.
81 {
82 Filter( StringArray & list , Ignore ignore = Ignore::Case ) :
83 m_list(list) ,
84 m_ignore(ignore)
85 {
86 }
87 Filter & allow( const std::optional<std::string> & a )
88 {
89 auto a_list = Str::splitIntoTokens( a.value_or(std::string()) , "," ) ;
90 if( a.has_value() && a_list.empty() )
91 m_list.clear() ;
92 else
93 StringList::keepMatch( m_list , a_list , m_ignore ) ;
94 return *this ;
95 }
96 Filter & deny( const std::string & d )
97 {
98 auto d_list = Str::splitIntoTokens( d , "," ) ;
99 StringList::removeMatch( m_list , d_list , m_ignore ) ;
100 return *this ;
101 }
102 ~Filter() = default ;
103 Filter( const Filter & ) = delete ;
104 Filter( Filter && ) = delete ;
105 Filter & operator=( const Filter & ) = delete ;
106 Filter & operator=( Filter && ) = delete ;
107 StringArray & m_list ;
108 Ignore m_ignore ;
109 } ;
110 }
111}
112
113#endif
static void splitIntoTokens(const std::string &in, StringArray &out, std::string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:1119
A class template like a simplified c++17 std::optional.
Definition: goptional.h:50
bool has_value() const noexcept
Returns true if a defined value.
Definition: goptional.h:109
T value_or(const T &) const
Returns the value or a default.
Definition: goptional.h:134
void applyMatch(StringArray &list, const StringArray &allow_list, Ignore=Ignore::Nothing)
Removes items in the list that do not match any entry in the allow list and reorders the result to be...
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
bool match(const StringArray &, const std::string &)
Returns true if any string in the array matches the given string.
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
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
bool imatch(const StringArray &, const std::string &)
Returns true if any string in the array matches the given string, ignoring case.
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
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
Low-level classes.
Definition: garg.h:36
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
Filters a list of strings with allow and deny lists.
Definition: gstringlist.h:81