args-parser 6.3.6
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
2/*
3 SPDX-FileCopyrightText: 2026 Igor Mironchik <igor.mironchik@gmail.com>
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef ARGS__UTILS_HPP__INCLUDED
8#define ARGS__UTILS_HPP__INCLUDED
9
10// Args include.
11#include "types.hpp"
12
13// C++ include.
14#include <algorithm>
15#include <type_traits>
16
17namespace Args
18{
19
20//
21// DISABLE_COPY
22//
23
25#define DISABLE_COPY(Class) \
26 Class(const Class &) = delete; \
27 Class &operator=(const Class &) = delete;
28
29//
30// UNUSED
31//
32
34#define UNUSED(Var) (void)Var;
35
36namespace details
37{
38
39//
40// asConst
41//
42
44template<typename T>
45constexpr typename std::add_const<T>::type &asConst(T &t) noexcept
46{
47 return t;
48}
49
50template<typename T>
51void asConst(const T &&) = delete;
52
53//
54// isArgument
55//
56
58static inline bool isArgument(const String &word)
59{
60 return (word.find(SL("--")) == 0);
61} // isArgument
62
63//
64// isFlag
65//
66
68static inline bool isFlag(const String &word)
69{
70 if (!isArgument(word)) {
71 if (word.find(SL('-')) == 0) {
72 return true;
73 }
74 }
75
76 return false;
77} // isFlag
78
79//
80// isCorrectFlag
81//
82
84static inline bool isCorrectFlag(const String &flag)
85{
86 if (flag.empty() || flag.length() > 1) {
87 return false;
88 }
89
90 static const String availableSymbols(
91 SL("0123456789"
92 "abcdefghijklmnopqrstuvwxyz"
93 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
94
95 return (availableSymbols.find(flag) != String::npos);
96} // isCorrectFlag
97
98//
99// isCorrectName
100//
101
103static inline bool isCorrectName(const String &name)
104{
105 if (name.empty()) {
106 return false;
107 }
108
109 static const String availableSymbols(
110 SL("0123456789"
111 "abcdefghijklmnopqrstuvwxyz"
112 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"));
113
114 for (const Char &c : asConst(name)) {
115 if (availableSymbols.find(c) == String::npos) {
116 return false;
117 }
118 }
119
120 return true;
121} // isCorrectName
122
123//
124// isMisspelledName
125//
126
128static inline bool isMisspelledName(const String &misspelled,
129 const String &correct)
130{
131 if (!misspelled.empty() && !correct.empty()) {
132 String ms = misspelled;
133 String cs = correct;
134
135 std::sort(ms.begin(), ms.end());
136 std::sort(cs.begin(), cs.end());
137
138 return (ms == cs);
139 } else {
140 return false;
141 }
142}
143
144template<typename T = void>
146 static const String c_string;
147};
148
149template<typename T>
151
152} /* namespace details */
153
154} /* namespace Args */
155
156#endif // ARGS__UTILS_HPP__INCLUDED
constexpr std::add_const< T >::type & asConst(T &t) noexcept
Adds const to non-const objects.
Definition utils.hpp:45
Definition api.hpp:18
std::string String
String type.
Definition types.hpp:314
String::value_type Char
Char type.
Definition types.hpp:317
static const String c_string
Definition utils.hpp:146
#define SL(str)
Definition types.hpp:328