Maipa's Standard Library Extension 1.5.6
mstd
Loading...
Searching...
No Matches
isstrnum.hpp
1/*
2 * mstd - Maipa's Standard Library
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/mstd/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _MSTD_ISSTRNUM_HPP_
12 #define _MSTD_ISSTRNUM_HPP_
13
14 #include <mstd/config.hpp>
15
17_MSTD_WARNING("this is only available for c++17 and greater!");
18 #else
19
20 #include <mstd/string_types.hpp>
21
22namespace mstd {
23 inline bool isstrhex(const std::string_view str) {
24 if (str.size() <= 2) { return false; }
25 if (str[0] != '0' || str[1] != 'x') { return false; }
26
27 size_t i = 2;
28 for (; i != str.size(); ++i) {
29 if ((str[i] < '0' || str[i] > '9') && (str[i] < 'a' || str[i] > 'f') && (str[i] < 'A' || str[i] > 'F')) {
30 return false;
31 }
32 }
33
34 return true;
35 }
36
37 inline bool isstroct(const std::string_view str) {
38 if (str.size() <= 2) { return false; }
39 if (str[0] != '0' || str[1] != 'c') { return false; }
40
41 size_t i = 2;
42 for (; i != str.size(); ++i) {
43 if (str[i] < '0' || str[i] > '7') { return false; }
44 }
45
46 return true;
47 }
48
49 inline bool isstrbin(const std::string_view str) {
50 if (str.size() <= 2) { return false; }
51 if (str[0] != '0' || str[1] != 'b') { return false; }
52
53 size_t i = 2;
54 for (; i != str.size(); ++i) {
55 if (str[i] < '0' || str[i] > '1') { return false; }
56 }
57
58 return true;
59 }
60
61 inline bool isstrnum(const std::string_view str) {
62 if (str.empty()) { return false; }
63
64 if (str.size() > 2 && str[0] == '0') {
65 if (str[1] == 'b') { return isstrbin(str); }
66
67 if (str[1] == 'c') { return isstroct(str); }
68
69 if (str[1] == 'x') { return isstrhex(str); }
70 }
71
72 size_t i = 0;
73 while (str[i] == '+' || str[i] == '-') {
74 ++i;
75 if (i == str.size()) { return false; }
76 }
77
78 for (; i != str.size(); ++i) {
79 if (str[i] < '0' || str[i] > '9') { return false; }
80 }
81
82 return true;
83 }
84
85 inline bool isstrunum(const std::string_view str) {
86 if (str.empty()) { return false; }
87
88 if (str.size() > 2 && str[0] == '0') {
89 if (str[1] == 'b') { return isstrbin(str); }
90
91 if (str[1] == 'c') { return isstroct(str); }
92
93 if (str[1] == 'x') { return isstrhex(str); }
94 }
95
96 size_t i = 0;
97 while (str[i] == '+') {
98 ++i;
99 if (i == str.size()) { return false; }
100 }
101
102 for (; i != str.size(); ++i) {
103 if (str[i] < '0' || str[i] > '9') { return false; }
104 }
105
106 return true;
107 }
108
109 inline bool isstrfp(const std::string_view str) {
110 if (str.empty()) { return false; }
111
112 size_t i = 0;
113 while (str[i] == '+' || str[i] == '-') {
114 ++i;
115 if (i == str.size()) { return false; }
116 }
117
118 while (str[i] >= '0' && str[i] <= '9') {
119 ++i;
120 if (i == str.size()) { return true; }
121 }
122
123 if (str[i] == '.') {
124 ++i;
125 if (i == str.size()) { return false; }
126
127 while (str[i] >= '0' && str[i] <= '9') {
128 ++i;
129 if (i == str.size()) { return true; }
130 }
131 }
132
133 return false;
134 }
135} // namespace mstd
136
137 #endif
138#endif
#define _MSTD_HAS_CXX17
Definition config.hpp:45
Definition arithmetic_types.hpp:23
bool isstrunum(const std::string_view str)
Definition isstrnum.hpp:85
bool isstrhex(const std::string_view str)
Definition isstrnum.hpp:23
bool isstrfp(const std::string_view str)
Definition isstrnum.hpp:109
bool isstrbin(const std::string_view str)
Definition isstrnum.hpp:49
bool isstroct(const std::string_view str)
Definition isstrnum.hpp:37
bool isstrnum(const std::string_view str)
Definition isstrnum.hpp:61