Nostrduino
Loading...
Searching...
No Matches
NostrNumeric.h
Go to the documentation of this file.
1#ifndef NOSTR_NUMERIC_H
2#define NOSTR_NUMERIC_H
3
4#include "NostrString.h"
5#include <cerrno>
6#include <cctype>
7#include <cmath>
8#include <cstdint>
9#include <cstdlib>
10#include <limits>
11
12namespace nostr {
13namespace detail {
14
15inline bool parseIntegerStrict(const NostrString &value, int64_t &out) {
16 const char *text = NostrString_toChars(value);
17 if (text == nullptr || *text == '\0' ||
18 std::isspace(static_cast<unsigned char>(*text))) return false;
19 errno = 0;
20 char *end = nullptr;
21 const long long parsed = std::strtoll(text, &end, 10);
22 if (errno == ERANGE || end == nullptr || end == text || *end != '\0' ||
23 parsed < static_cast<long long>(std::numeric_limits<int64_t>::min()) ||
24 parsed > static_cast<long long>(std::numeric_limits<int64_t>::max())) return false;
25 out = static_cast<int64_t>(parsed);
26 return true;
27}
28
29inline bool parseFloatStrict(const NostrString &value, double &out) {
30 const char *text = NostrString_toChars(value);
31 if (text == nullptr || *text == '\0' ||
32 std::isspace(static_cast<unsigned char>(*text))) return false;
33 errno = 0;
34 char *end = nullptr;
35 const double parsed = std::strtod(text, &end);
36 if (errno == ERANGE || end == nullptr || end == text || *end != '\0' ||
37 !std::isfinite(parsed)) return false;
38 out = parsed;
39 return true;
40}
41
42} // namespace detail
43} // namespace nostr
44
45#endif
const char * NostrString_toChars(const std::string &str)
Definition NostrString.cpp:188
#define NostrString
Definition NostrString.h:10
Definition Nip47.h:44
bool parseFloatStrict(const NostrString &value, double &out)
Definition NostrNumeric.h:29
bool parseIntegerStrict(const NostrString &value, int64_t &out)
Definition NostrNumeric.h:15
Definition CryptoLock.h:6