args-parser 6.3.3
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__UTILS_HPP__INCLUDED
32#define ARGS__UTILS_HPP__INCLUDED
33
34// Args include.
35#include "types.hpp"
36
37// C++ include.
38#include <algorithm>
39#include <type_traits>
40
41
42namespace Args {
43
44
45//
46// DISABLE_COPY
47//
48
50#define DISABLE_COPY( Class ) \
51 Class( const Class & ) = delete; \
52 Class & operator= ( const Class & ) = delete;
53
54
55//
56// UNUSED
57//
58
60#define UNUSED( Var ) (void)Var;
61
62
63namespace details {
64
65//
66// asConst
67//
68
70template< typename T >
71constexpr typename std::add_const< T >::type &
72asConst( T & t ) noexcept
73{
74 return t;
75}
76
77template < typename T >
78void asConst( const T && ) = delete;
79
80
81//
82// isArgument
83//
84
86static inline bool
87isArgument( const String & word )
88{
89 return ( word.find( SL( "--" ) ) == 0 );
90} // isArgument
91
92
93//
94// isFlag
95//
96
98static inline bool
99isFlag( const String & word )
100{
101 if( !isArgument( word ) )
102 {
103 if( word.find( SL( '-' ) ) == 0 )
104 return true;
105 }
106
107 return false;
108} // isFlag
109
110
111//
112// isCorrectFlag
113//
114
116static inline bool
117isCorrectFlag( const String & flag )
118{
119 if( flag.empty() || flag.length() > 1 )
120 return false;
121
122 static const String availableSymbols( SL( "0123456789"
123 "abcdefghijklmnopqrstuvwxyz"
124 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) );
125
126 return ( availableSymbols.find( flag ) != String::npos );
127} // isCorrectFlag
128
129
130//
131// isCorrectName
132//
133
135static inline bool
136isCorrectName( const String & name )
137{
138 if( name.empty() )
139 return false;
140
141 static const String availableSymbols( SL( "0123456789"
142 "abcdefghijklmnopqrstuvwxyz"
143 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_" ) );
144
145 for( const Char & c : asConst( name ) )
146 {
147 if( availableSymbols.find( c ) == String::npos )
148 return false;
149 }
150
151 return true;
152} // isCorrectName
153
154
155//
156// isMisspelledName
157//
158
160static inline bool
161isMisspelledName( const String & misspelled,
162 const String & correct )
163{
164 if( !misspelled.empty() && !correct.empty() )
165 {
166 String ms = misspelled;
167 String cs = correct;
168
169 std::sort( ms.begin(), ms.end() );
170 std::sort( cs.begin(), cs.end() );
171
172 return ( ms == cs );
173 }
174 else
175 return false;
176}
177
178template< typename T = void >
180 static const String c_string;
181};
182
183template< typename T >
185
186} /* namespace details */
187
188} /* namespace Args */
189
190#endif // ARGS__UTILS_HPP__INCLUDED
constexpr std::add_const< T >::type & asConst(T &t) noexcept
Adds const to non-const objects.
Definition utils.hpp:72
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
String::value_type Char
Char type.
Definition types.hpp:327
static const String c_string
Definition utils.hpp:180
#define SL(str)
Definition types.hpp:338