E-MailRelay

grange.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 grange.h
19///
20
21#ifndef G_RANGE_H
22#define G_RANGE_H
23
24#include "gdef.h"
25#include "gstringview.h"
26#include "gstr.h"
27#include "gexception.h"
28
29 namespace G
30{
31 namespace Range /// Utility functions for pair-of-integer ranges.
32 {
33 inline std::pair<int,int> range( std::string_view spec_part )
34 {
35 if( spec_part.empty() )
36 {
37 return { -1 , -1 } ;
38 }
39 else if( spec_part.find('-') == std::string::npos )
40 {
41 int value = static_cast<int>( Str::toUInt( spec_part ) ) ;
42 return { value , value } ;
43 }
44 else
45 {
46 return {
47 static_cast<int>(Str::toUInt(Str::headView(spec_part,"-",false))) ,
48 static_cast<int>(Str::toUInt(Str::tailView(spec_part,"-",true)))
49 } ;
50 }
51 }
52 inline std::pair<int,int> range( int a , int b ) { return { a , b } ; }
53 inline std::pair<int,int> from( int n ) { return { n , -1 } ; }
54 inline std::pair<int,int> none() { return { -1 , -1 } ; }
55 inline std::pair<int,int> all() { return { 0 , -1 } ; }
56 inline std::string str( std::pair<int,int> range , int big = 9999 )
57 {
58 return Str::fromInt(range.first).append(1U,'-').append(Str::fromInt(range.second<0?big:range.second)) ;
59 }
60 inline bool within( std::pair<int,int> range , int n )
61 {
62 return n >= 0 && n >= range.first && ( range.second < 0 || n <= range.second ) ;
63 }
64 inline void check( std::string_view spec )
65 {
66 if( !spec.empty() )
67 {
68 auto r = range( spec ) ; // throws on error -- see G::Str::toUInt()
69 if( r.first != -1 && r.second < r.first ) // eg. "1000-900"
70 throw Exception( "not a valid numeric range" ) ;
71 }
72 }
73 }
74}
75
76#endif
G::Exception
A general-purpose exception class derived from std::exception and containing an error message.
Definition: gexception.h:64
G::Str::fromInt
static std::string fromInt(int i)
Converts int 'i' to a string.
Definition: gstr.h:594
G::Str::tailView
static std::string_view tailView(std::string_view in, std::size_t pos, std::string_view default_={}) noexcept
Like tail() but returning a view into the input string.
Definition: gstr.cpp:1337
G::Str::toUInt
static unsigned int toUInt(std::string_view s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:648
G::Str::headView
static std::string_view headView(std::string_view in, std::size_t pos, std::string_view default_={}) noexcept
Like head() but returning a view into the input string.
Definition: gstr.cpp:1308
gexception.h
gstr.h
gstringview.h
G
Low-level classes.
Definition: garg.h:36